Class: Fog::Storage::Local::File

Inherits:
Model
  • Object
show all
Defined in:
lib/fog/storage/models/local/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

Constructor Details

This class inherits a constructor from Fog::Model

Instance Method Details

#bodyObject



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

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

#body=(new_body) ⇒ Object



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

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

#content_typeObject



27
28
29
30
31
32
33
# File 'lib/fog/storage/models/local/file.rb', line 27

def content_type
  @content_type ||= begin
    unless (mime_types = ::MIME::Types.of(key)).empty?
      mime_types.first.content_type
    end
  end
end

#destroyObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/fog/storage/models/local/file.rb', line 39

def destroy
  requires :directory, :key
  ::File.delete(path)
  dirs = path.split(::File::SEPARATOR)[0...-1]
  dirs.length.times do |index|
    dir_path = dirs[0..-index].join(::File::SEPARATOR)
    if dir_path.empty? # path starts with ::File::SEPARATOR
      next
    end
    # don't delete the containing directory or higher
    if dir_path == connection.path_to(directory.key)
      break
    end
    pwd = Dir.pwd
    Dir.chdir(dir_path)
    if Dir.glob('*').empty?
      Dir.rmdir(dir_path)
    end
    Dir.chdir(pwd)
  end
  true
end

#directoryObject



35
36
37
# File 'lib/fog/storage/models/local/file.rb', line 35

def directory
  @directory
end

#public=(new_public) ⇒ Object



62
63
64
# File 'lib/fog/storage/models/local/file.rb', line 62

def public=(new_public)
  new_public
end

#public_urlObject



66
67
68
# File 'lib/fog/storage/models/local/file.rb', line 66

def public_url
  nil
end

#save(options = {}) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/fog/storage/models/local/file.rb', line 70

def save(options = {})
  requires :body, :directory, :key
  dirs = path.split(::File::SEPARATOR)[0...-1]
  dirs.length.times do |index|
    dir_path = dirs[0..index].join(::File::SEPARATOR)
    if dir_path.empty? # path starts with ::File::SEPARATOR
      next
    end
    # create directory if it doesn't already exist
    unless ::File.directory?(dir_path)
      Dir.mkdir(dir_path)
    end
  end
  file = ::File.new(path, 'w')
  if body.is_a?(String)
    file.write(body)
  else
    file.write(body.read)
  end
  file.close
  merge_attributes(
    :content_length => Fog::Storage.get_body_size(body),
    :last_modified  => ::File.mtime(path)
  )
  true
end