Class: Fog::Storage::Ninefold::File

Inherits:
Model
  • Object
show all
Defined in:
lib/fog/ninefold/models/storage/file.rb

Instance Attribute Summary

Attributes inherited from Model

#collection, #connection

Instance Method Summary collapse

Methods inherited from Model

#initialize, #inspect, #reload, #to_json, #wait_for

Methods included from Attributes::ClassMethods

#_load, #aliases, #attribute, #attributes, #identity, #ignore_attributes, #ignored_attributes

Methods included from Attributes::InstanceMethods

#_dump, #attributes, #dup, #identity, #identity=, #merge_attributes, #new_record?, #requires, #requires_one

Constructor Details

This class inherits a constructor from Fog::Model

Instance Method Details

#bodyObject



15
16
17
18
19
20
21
# File 'lib/fog/ninefold/models/storage/file.rb', line 15

def body
  attributes[:body] ||= if objectid
    collection.get(identity).body
  else
    ''
  end
end

#body=(new_body) ⇒ Object



23
24
25
# File 'lib/fog/ninefold/models/storage/file.rb', line 23

def body=(new_body)
  attributes[:body] = new_body
end

#destroyObject



31
32
33
34
35
# File 'lib/fog/ninefold/models/storage/file.rb', line 31

def destroy
  requires :directory, :key
  connection.delete_namespace([directory.key, key].join('/'))
  true
end

#directoryObject



27
28
29
# File 'lib/fog/ninefold/models/storage/file.rb', line 27

def directory
  @directory
end

#public=(new_public) ⇒ Object

def owner=(new_owner)

if new_owner
  attributes[:owner] = {
    :display_name => new_owner['DisplayName'],
    :id           => new_owner['ID']
  }
end

end



46
47
48
# File 'lib/fog/ninefold/models/storage/file.rb', line 46

def public=(new_public)
  # NOOP - we don't need to flag files as public, getting the public URL for a file handles it.
end

#public_url(expires = (Time.now + 5 * 365 * 24 * 60 * 60)) ⇒ Object

By default, expire in 5 years



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/fog/ninefold/models/storage/file.rb', line 51

def public_url(expires = (Time.now + 5 * 365 * 24 * 60 * 60))
  requires :objectid
  # TODO - more efficient method to get this?
  storage = Fog::Storage.new(:provider => 'Ninefold')
  uri = URI::HTTP.build(:scheme => Fog::Storage::Ninefold::STORAGE_SCHEME, :host => Fog::Storage::Ninefold::STORAGE_HOST, :port => Fog::Storage::Ninefold::STORAGE_PORT.to_i, :path => "/rest/objects/#{objectid}" )
  Fog::Storage.new(:provider => 'Ninefold').uid


  sb = "GET\n"
  sb += uri.path.downcase + "\n"
  sb += storage.uid + "\n"
  sb += String(expires.to_i())

  signature = storage.sign( sb )
  uri.query = "uid=#{CGI::escape(storage.uid)}&expires=#{expires.to_i()}&signature=#{CGI::escape(signature)}"
  uri.to_s
end

#save(options = {}) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/fog/ninefold/models/storage/file.rb', line 69

def save(options = {})
  requires :body, :directory, :key
  directory.kind_of?(Directory) ? ns = directory.key : ns = directory
  ns += key
  options[:headers] ||= {}
  options[:headers]['Content-Type'] = content_type if content_type
  options[:body] = body
  begin
    data = connection.post_namespace(ns, options)
    self.objectid = data.headers['location'].split('/')[-1]
  rescue => error
    if error.message =~ /The resource you are trying to create already exists./
      data = connection.put_namespace(ns, options)
    else
      raise error
    end
  end
  # merge_attributes(data.headers)
  true
end