2010-10-04 17:02:08 -04:00
|
|
|
require 'fog/core/collection'
|
2011-01-14 13:41:12 -05:00
|
|
|
require 'fog/storage/models/local/file'
|
2010-09-08 15:50:38 -04:00
|
|
|
|
|
|
|
module Fog
|
|
|
|
module Local
|
|
|
|
class Storage
|
|
|
|
|
|
|
|
class Files < Fog::Collection
|
|
|
|
|
|
|
|
attribute :directory
|
|
|
|
|
2010-09-21 13:54:15 -04:00
|
|
|
model Fog::Local::Storage::File
|
2010-09-08 15:50:38 -04:00
|
|
|
|
|
|
|
def all
|
|
|
|
requires :directory
|
|
|
|
if directory.collection.get(directory.key)
|
|
|
|
data = Dir.entries(connection.path_to(directory.key)).select do |key|
|
|
|
|
key[0...1] != '.' && !::File.directory?(connection.path_to(key))
|
|
|
|
end.map do |key|
|
|
|
|
path = file_path(key)
|
|
|
|
{
|
|
|
|
:content_length => ::File.size(path),
|
2010-11-23 16:45:37 -05:00
|
|
|
:key => CGI.unescape(key),
|
2010-09-08 15:50:38 -04:00
|
|
|
:last_modified => ::File.mtime(path)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
load(data)
|
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def get(key, &block)
|
|
|
|
requires :directory
|
2010-11-23 16:45:37 -05:00
|
|
|
path = file_path(CGI.escape(key))
|
2010-09-08 15:50:38 -04:00
|
|
|
if ::File.exists?(path)
|
|
|
|
data = {
|
|
|
|
:content_length => ::File.size(path),
|
|
|
|
:key => key,
|
|
|
|
:last_modified => ::File.mtime(path)
|
|
|
|
}
|
|
|
|
if block_given?
|
|
|
|
file = ::File.open(path)
|
|
|
|
while (chunk = file.read(Excon::CHUNK_SIZE)) && yield(chunk); end
|
|
|
|
file.close
|
|
|
|
new(data)
|
|
|
|
else
|
2010-10-13 16:20:18 -04:00
|
|
|
body = ::File.read(path)
|
2010-09-08 15:50:38 -04:00
|
|
|
new(data.merge!(:body => body))
|
|
|
|
end
|
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-11-23 16:45:37 -05:00
|
|
|
def head(key)
|
|
|
|
requires :directory
|
|
|
|
path = file_path(CGI.escape(key))
|
|
|
|
if ::File.exists?(path)
|
|
|
|
new({
|
|
|
|
:content_length => ::File.size(path),
|
|
|
|
:key => key,
|
|
|
|
:last_modified => ::File.mtime(path)
|
|
|
|
})
|
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-09-08 15:50:38 -04:00
|
|
|
def new(attributes = {})
|
|
|
|
requires :directory
|
|
|
|
super({ :directory => directory }.merge!(attributes))
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def file_path(key)
|
|
|
|
connection.path_to(::File.join(directory.key, key))
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|