1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00
fog--fog/lib/fog/storage/models/local/file.rb

112 lines
2.7 KiB
Ruby
Raw Normal View History

2010-10-04 17:02:08 -04:00
require 'fog/core/model'
module Fog
module Local
class Storage
class File < Fog::Model
identity :key, :aliases => 'Key'
2011-01-26 18:56:50 -05:00
attribute :content_length, :aliases => 'Content-Length', :type => :integer
# attribute :content_type, :aliases => 'Content-Type'
attribute :last_modified, :aliases => 'Last-Modified'
def body
attributes[:body] ||= if last_modified
collection.get(identity).body
else
''
end
end
def body=(new_body)
attributes[:body] = new_body
end
def content_type
@content_type ||= begin
unless (mime_types = ::MIME::Types.of(key)).empty?
mime_types.first.content_type
end
end
end
def directory
@directory
end
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
2010-11-18 14:18:46 -05:00
def public=(new_public)
new_public
end
def public_url
nil
end
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 => ::File.size(path),
:last_modified => ::File.mtime(path)
)
true
end
private
def directory=(new_directory)
@directory = new_directory
end
def path
connection.path_to(::File.join(directory.key, key))
end
end
end
end
end