1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00

[local|storage] rewrite to use nested directories

This replaces CGI.escape for key names
NOTE: this change is not backwards compatible

Closes #169
This commit is contained in:
geemus 2011-02-15 16:53:45 -08:00
parent 347d38efcd
commit cebfc4f91f
2 changed files with 37 additions and 6 deletions

View file

@ -39,6 +39,23 @@ module Fog
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
@ -52,6 +69,17 @@ module Fog
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)
@ -73,7 +101,7 @@ module Fog
end
def path
connection.path_to(::File.join(directory.key, CGI.escape(key)))
connection.path_to(::File.join(directory.key, key))
end
end

View file

@ -14,16 +14,19 @@ module Fog
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))
pwd = Dir.pwd
Dir.chdir(connection.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 => CGI.unescape(key),
:key => key,
:last_modified => ::File.mtime(path)
}
end
Dir.chdir(pwd)
load(data)
else
nil
@ -32,7 +35,7 @@ module Fog
def get(key, &block)
requires :directory
path = file_path(CGI.escape(key))
path = file_path(key)
if ::File.exists?(path)
data = {
:content_length => ::File.size(path),
@ -55,7 +58,7 @@ module Fog
def head(key)
requires :directory
path = file_path(CGI.escape(key))
path = file_path(key)
if ::File.exists?(path)
new({
:content_length => ::File.size(path),