2010-10-04 17:02:08 -04:00
|
|
|
require 'fog/core/collection'
|
2011-08-24 15:03:25 -04:00
|
|
|
require 'fog/local/models/storage/file'
|
2010-09-08 15:50:38 -04:00
|
|
|
|
|
|
|
module Fog
|
2011-06-15 17:26:43 -04:00
|
|
|
module Storage
|
|
|
|
class Local
|
2010-09-08 15:50:38 -04:00
|
|
|
|
|
|
|
class Files < Fog::Collection
|
|
|
|
|
|
|
|
attribute :directory
|
|
|
|
|
2011-06-15 17:26:43 -04:00
|
|
|
model Fog::Storage::Local::File
|
2010-09-08 15:50:38 -04:00
|
|
|
|
|
|
|
def all
|
|
|
|
requires :directory
|
|
|
|
if directory.collection.get(directory.key)
|
2013-05-27 11:07:26 -04:00
|
|
|
data = []
|
|
|
|
Dir.chdir(service.path_to(directory.key)) {
|
|
|
|
data = Dir.glob('**/*').reject do |file|
|
|
|
|
::File.directory?(file)
|
|
|
|
end.map do |key|
|
|
|
|
path = file_path(key)
|
|
|
|
{
|
|
|
|
:content_length => ::File.size(path),
|
|
|
|
:key => key,
|
|
|
|
:last_modified => ::File.mtime(path)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
}
|
2010-09-08 15:50:38 -04:00
|
|
|
load(data)
|
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def get(key, &block)
|
|
|
|
requires :directory
|
2011-02-15 19:53:45 -05:00
|
|
|
path = file_path(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
|
2011-02-15 19:53:45 -05:00
|
|
|
path = file_path(key)
|
2010-11-23 16:45:37 -05:00
|
|
|
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)
|
2012-12-22 18:25:11 -05:00
|
|
|
service.path_to(::File.join(directory.key, key))
|
2010-09-08 15:50:38 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|