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

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

Instance Method Summary collapse

Instance Method Details

#bodyObject



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

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

#body=(new_body) ⇒ Object



21
22
23
# File 'lib/fog/storage/local/models/file.rb', line 21

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

#content_typeObject



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

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

#copy(target_directory_key, target_file_key, options = {}) ⇒ Object



37
38
39
40
41
42
# File 'lib/fog/storage/local/models/file.rb', line 37

def copy(target_directory_key, target_file_key, options={})
  requires :directory, :key
  service.copy_object(directory.key, key, target_directory_key, target_file_key)
  target_directory = service.directories.new(:key => target_directory_key)
  target_directory.files.get(target_file_key)
end

#destroyObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/fog/storage/local/models/file.rb', line 44

def destroy
  requires :directory, :key
  ::File.delete(path) if ::File.exist?(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 == service.path_to(directory.key)
      break
    end
    pwd = Dir.pwd
    if ::File.exist?(dir_path) && ::File.directory?(dir_path)
      Dir.chdir(dir_path)
      if Dir.glob('*').empty?
        Dir.rmdir(dir_path)
      end
      Dir.chdir(pwd)
    end
  end
  true
end

#directoryObject



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

def directory
  @directory
end

#public=(new_public) ⇒ Object



69
70
71
# File 'lib/fog/storage/local/models/file.rb', line 69

def public=(new_public)
  new_public
end

#public_urlObject



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/fog/storage/local/models/file.rb', line 73

def public_url
  requires :directory, :key

  if service.endpoint
    escaped_directory = URI.escape(directory.key)
    escaped_key = URI.escape(key)

    ::File.join(service.endpoint, escaped_directory, escaped_key)
  else
    nil
  end
end

#save(options = {}) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/fog/storage/local/models/file.rb', line 86

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
    begin
      Dir.mkdir(dir_path)
    rescue Errno::EEXIST
      raise unless ::File.directory?(dir_path)
    end
  end
  file = ::File.new(path, 'wb')
  if body.is_a?(String)
    file.write(body)
  elsif body.kind_of? ::File and ::File.exist?(body.path)
    FileUtils.cp(body.path, path)
  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