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

[ninefold|storage] Nested directories

This commit is contained in:
Lincoln Stoll 2011-07-09 16:55:55 +10:00
parent c69e231c10
commit 083f7b9038
3 changed files with 65 additions and 9 deletions

View file

@ -7,13 +7,18 @@ module Fog
class Directories < Fog::Collection
attribute :directory
model Fog::Storage::Ninefold::Directory
def all
data = connection.get_namespace.body[:DirectoryList]
directory ? ns = directory.key : ns = ''
data = connection.get_namespace(ns).body[:DirectoryList]
data = {:DirectoryEntry => []} if data.kind_of? String
data[:DirectoryEntry] = [data[:DirectoryEntry]] if data[:DirectoryEntry].kind_of? Hash
load(data[:DirectoryEntry])
dirs = data[:DirectoryEntry].select {|de| de[:FileType] == 'directory'}
dirs.each {|d| d[:Filename] = ns + '/' + d[:Filename] if directory}
load(data[:DirectoryEntry].select {|de| de[:FileType] == 'directory'})
end
def get(key, options = {})
@ -21,11 +26,16 @@ module Fog
res = connection.get_namespace key
emc_meta = res.headers['x-emc-meta']
obj_id = emc_meta.scan(/objectid=(\w+),/).flatten[0]
new(:id => obj_id, :filename => key, :type => 'directory')
new(:objectid => obj_id, :key => key)
rescue Fog::Storage::Ninefold::NotFound
nil
end
def new(attributes ={})
attributes = {:directory => directory}.merge(attributes) if directory
super(attributes)
end
end
end

View file

@ -6,17 +6,38 @@ module Fog
class Directory < Fog::Model
identity :filename, :aliases => [:'Filename', :'key']
attribute :id, :aliases => :'ObjectID'
attribute :type, :aliases => :'FileType'
identity :key, :aliases => :Filename
attribute :objectid, :aliases => :ObjectID
def files
@files ||= begin
Fog::Storage::Ninefold::Files.new(
:directory => self,
:connection => connection
)
end
end
def directories
@directories ||= begin
Fog::Storage::Ninefold::Directories.new(
:directory => self,
:connection => connection
)
end
end
def save
res = connection.post_namespace filename
self.key = attributes[:directory].key + '/' + key if attributes[:directory]
res = connection.post_namespace key
reload
end
def destroy
connection.delete_namespace filename
def destroy(opts={})
if opts[:recursive]
directories.each { |d| d.destroy(opts) }
end
connection.delete_namespace key
end

View file

@ -0,0 +1,25 @@
if storage_providers.keys.include? :ninefold
for provider, config in storage_providers
Shindo.tests("Storage[:ninefold] | nested directories", [provider]) do
ninefold = Fog::Storage[:ninefold]
tests("create a directory with a / character").succeeds do
ninefold.directories.create(:key => 'sub/path')
end
tests("List of top directory returns sub dir").returns(1) do
ninefold.directories.get('sub').directories.count
end
tests("create a directory in a sub dir").returns('sub/path/newdir') do
ninefold.directories.get('sub/path').directories.create(:key => 'newdir').identity
end
tests("Recursively destroy parent dir").succeeds do
ninefold.directories.get('sub').destroy(:recursive => true)
end
end
end
end