[local] rename files to storage for consistency
This commit is contained in:
parent
a637504d18
commit
24897e9c1e
|
@ -1,57 +1,17 @@
|
||||||
module Fog
|
module Fog
|
||||||
class Local < Fog::Service
|
module Local
|
||||||
|
|
||||||
requires :local_root
|
extend Fog::Provider
|
||||||
|
|
||||||
model_path 'fog/local/models'
|
service_path 'fog/local'
|
||||||
collection :directories
|
service 'storage'
|
||||||
model :directory
|
|
||||||
model :file
|
|
||||||
collection :files
|
|
||||||
|
|
||||||
class Mock
|
def self.new(attributes = {})
|
||||||
include Collections
|
location = caller.first
|
||||||
|
warning = "[yellow][WARN] Fog::Local#new is deprecated, use Fog::Local::Storage#new instead[/]"
|
||||||
def self.data
|
warning << " [light_black](" << location << ")[/] "
|
||||||
@data ||= Hash.new do |hash, key|
|
Formatador.display_line(warning)
|
||||||
hash[key] = {}
|
Fog::Local::Storage.new(attributes)
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def self.reset_data(keys=data.keys)
|
|
||||||
for key in [*keys]
|
|
||||||
data.delete(key)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def initialize(options={})
|
|
||||||
@local_root = ::File.expand_path(options[:local_root])
|
|
||||||
@data = self.class.data[@local_root]
|
|
||||||
end
|
|
||||||
|
|
||||||
def local_root
|
|
||||||
@local_root
|
|
||||||
end
|
|
||||||
|
|
||||||
def path(partial)
|
|
||||||
partial
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
class Real
|
|
||||||
include Collections
|
|
||||||
|
|
||||||
def initialize(options={})
|
|
||||||
@local_root = ::File.expand_path(options[:local_root])
|
|
||||||
end
|
|
||||||
|
|
||||||
def local_root
|
|
||||||
@local_root
|
|
||||||
end
|
|
||||||
|
|
||||||
def path_to(partial)
|
|
||||||
::File.join(@local_root, partial)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -9,21 +9,21 @@ module Local
|
||||||
def [](service)
|
def [](service)
|
||||||
@@connections ||= Hash.new do |hash, key|
|
@@connections ||= Hash.new do |hash, key|
|
||||||
hash[key] = case key
|
hash[key] = case key
|
||||||
when :files
|
when :storage
|
||||||
Fog::Local.new
|
Fog::Local::Storage.new
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@connections[service]
|
@@connections[service]
|
||||||
end
|
end
|
||||||
|
|
||||||
def services
|
def services
|
||||||
[:files]
|
[:storage]
|
||||||
end
|
end
|
||||||
|
|
||||||
for collection in Fog::Local.collections
|
for collection in Fog::Local::Storage.collections
|
||||||
module_eval <<-EOS, __FILE__, __LINE__
|
module_eval <<-EOS, __FILE__, __LINE__
|
||||||
def #{collection}
|
def #{collection}
|
||||||
self[:files].#{collection}
|
self[:storage].#{collection}
|
||||||
end
|
end
|
||||||
EOS
|
EOS
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,31 +0,0 @@
|
||||||
require 'fog/collection'
|
|
||||||
require 'fog/local/models/directory'
|
|
||||||
|
|
||||||
module Fog
|
|
||||||
class Local
|
|
||||||
|
|
||||||
class Directories < Fog::Collection
|
|
||||||
|
|
||||||
model Fog::Local::Directory
|
|
||||||
|
|
||||||
def all
|
|
||||||
data = Dir.entries(connection.local_root).select do |entry|
|
|
||||||
entry[0...1] != '.' && ::File.directory?(connection.path_to(entry))
|
|
||||||
end.map do |entry|
|
|
||||||
{:key => entry}
|
|
||||||
end
|
|
||||||
load(data)
|
|
||||||
end
|
|
||||||
|
|
||||||
def get(key)
|
|
||||||
if ::File.directory?(connection.path_to(key))
|
|
||||||
new(:key => key)
|
|
||||||
else
|
|
||||||
nil
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
|
||||||
end
|
|
|
@ -1,50 +0,0 @@
|
||||||
require 'fog/model'
|
|
||||||
# require 'fog/local/models/files'
|
|
||||||
|
|
||||||
module Fog
|
|
||||||
class Local
|
|
||||||
|
|
||||||
class Directory < Fog::Model
|
|
||||||
extend Fog::Deprecation
|
|
||||||
deprecate(:name, :key)
|
|
||||||
deprecate(:name=, :key=)
|
|
||||||
|
|
||||||
identity :key
|
|
||||||
|
|
||||||
def destroy
|
|
||||||
requires :key
|
|
||||||
|
|
||||||
if ::File.directory?(path)
|
|
||||||
Dir.rmdir(path)
|
|
||||||
true
|
|
||||||
else
|
|
||||||
false
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def files
|
|
||||||
@files ||= begin
|
|
||||||
Fog::Local::Files.new(
|
|
||||||
:directory => self,
|
|
||||||
:connection => connection
|
|
||||||
)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def save
|
|
||||||
requires :key
|
|
||||||
|
|
||||||
Dir.mkdir(path)
|
|
||||||
true
|
|
||||||
end
|
|
||||||
|
|
||||||
private
|
|
||||||
|
|
||||||
def path
|
|
||||||
connection.path_to(key)
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
|
||||||
end
|
|
|
@ -1,58 +0,0 @@
|
||||||
require 'fog/model'
|
|
||||||
|
|
||||||
module Fog
|
|
||||||
class Local
|
|
||||||
|
|
||||||
class File < Fog::Model
|
|
||||||
|
|
||||||
identity :key, 'Key'
|
|
||||||
|
|
||||||
attr_accessor :body
|
|
||||||
attribute :content_length, :aliases => 'Content-Length'
|
|
||||||
# attribute :content_type, :aliases => 'Content-Type'
|
|
||||||
attribute :last_modified, :aliases => 'Last-Modified'
|
|
||||||
|
|
||||||
def body
|
|
||||||
@body ||= if last_modified
|
|
||||||
collection.get(identity).body
|
|
||||||
else
|
|
||||||
''
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def directory
|
|
||||||
@directory
|
|
||||||
end
|
|
||||||
|
|
||||||
def destroy
|
|
||||||
requires :directory, :key
|
|
||||||
::File.delete(path)
|
|
||||||
true
|
|
||||||
end
|
|
||||||
|
|
||||||
def save(options = {})
|
|
||||||
requires :body, :directory, :key
|
|
||||||
file = ::File.new(path, 'w')
|
|
||||||
file.write(body)
|
|
||||||
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
|
|
|
@ -1,71 +0,0 @@
|
||||||
require 'fog/collection'
|
|
||||||
require 'fog/local/models/file'
|
|
||||||
|
|
||||||
module Fog
|
|
||||||
class Local
|
|
||||||
|
|
||||||
class Files < Fog::Collection
|
|
||||||
|
|
||||||
attribute :directory
|
|
||||||
|
|
||||||
model Fog::Local::File
|
|
||||||
|
|
||||||
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),
|
|
||||||
:key => key,
|
|
||||||
:last_modified => ::File.mtime(path)
|
|
||||||
}
|
|
||||||
end
|
|
||||||
load(data)
|
|
||||||
else
|
|
||||||
nil
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def get(key, &block)
|
|
||||||
requires :directory
|
|
||||||
path = file_path(key)
|
|
||||||
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
|
|
||||||
body = nil
|
|
||||||
::File.open(path) do |file|
|
|
||||||
body = file.read
|
|
||||||
end
|
|
||||||
new(data.merge!(:body => body))
|
|
||||||
end
|
|
||||||
else
|
|
||||||
nil
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
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
|
|
|
@ -0,0 +1,33 @@
|
||||||
|
require 'fog/collection'
|
||||||
|
require 'fog/local/models/directory'
|
||||||
|
|
||||||
|
module Fog
|
||||||
|
module Local
|
||||||
|
class Storage
|
||||||
|
|
||||||
|
class Directories < Fog::Collection
|
||||||
|
|
||||||
|
model Fog::Local::Directory
|
||||||
|
|
||||||
|
def all
|
||||||
|
data = Dir.entries(connection.local_root).select do |entry|
|
||||||
|
entry[0...1] != '.' && ::File.directory?(connection.path_to(entry))
|
||||||
|
end.map do |entry|
|
||||||
|
{:key => entry}
|
||||||
|
end
|
||||||
|
load(data)
|
||||||
|
end
|
||||||
|
|
||||||
|
def get(key)
|
||||||
|
if ::File.directory?(connection.path_to(key))
|
||||||
|
new(:key => key)
|
||||||
|
else
|
||||||
|
nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -0,0 +1,52 @@
|
||||||
|
require 'fog/model'
|
||||||
|
# require 'fog/local/models/files'
|
||||||
|
|
||||||
|
module Fog
|
||||||
|
module Local
|
||||||
|
class Storage
|
||||||
|
|
||||||
|
class Directory < Fog::Model
|
||||||
|
extend Fog::Deprecation
|
||||||
|
deprecate(:name, :key)
|
||||||
|
deprecate(:name=, :key=)
|
||||||
|
|
||||||
|
identity :key
|
||||||
|
|
||||||
|
def destroy
|
||||||
|
requires :key
|
||||||
|
|
||||||
|
if ::File.directory?(path)
|
||||||
|
Dir.rmdir(path)
|
||||||
|
true
|
||||||
|
else
|
||||||
|
false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def files
|
||||||
|
@files ||= begin
|
||||||
|
Fog::Local::Files.new(
|
||||||
|
:directory => self,
|
||||||
|
:connection => connection
|
||||||
|
)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def save
|
||||||
|
requires :key
|
||||||
|
|
||||||
|
Dir.mkdir(path)
|
||||||
|
true
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def path
|
||||||
|
connection.path_to(key)
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -0,0 +1,60 @@
|
||||||
|
require 'fog/model'
|
||||||
|
|
||||||
|
module Fog
|
||||||
|
module Local
|
||||||
|
class Storage
|
||||||
|
|
||||||
|
class File < Fog::Model
|
||||||
|
|
||||||
|
identity :key, 'Key'
|
||||||
|
|
||||||
|
attr_accessor :body
|
||||||
|
attribute :content_length, :aliases => 'Content-Length'
|
||||||
|
# attribute :content_type, :aliases => 'Content-Type'
|
||||||
|
attribute :last_modified, :aliases => 'Last-Modified'
|
||||||
|
|
||||||
|
def body
|
||||||
|
@body ||= if last_modified
|
||||||
|
collection.get(identity).body
|
||||||
|
else
|
||||||
|
''
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def directory
|
||||||
|
@directory
|
||||||
|
end
|
||||||
|
|
||||||
|
def destroy
|
||||||
|
requires :directory, :key
|
||||||
|
::File.delete(path)
|
||||||
|
true
|
||||||
|
end
|
||||||
|
|
||||||
|
def save(options = {})
|
||||||
|
requires :body, :directory, :key
|
||||||
|
file = ::File.new(path, 'w')
|
||||||
|
file.write(body)
|
||||||
|
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
|
|
@ -0,0 +1,73 @@
|
||||||
|
require 'fog/collection'
|
||||||
|
require 'fog/local/models/file'
|
||||||
|
|
||||||
|
module Fog
|
||||||
|
module Local
|
||||||
|
class Storage
|
||||||
|
|
||||||
|
class Files < Fog::Collection
|
||||||
|
|
||||||
|
attribute :directory
|
||||||
|
|
||||||
|
model Fog::Local::File
|
||||||
|
|
||||||
|
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),
|
||||||
|
:key => key,
|
||||||
|
:last_modified => ::File.mtime(path)
|
||||||
|
}
|
||||||
|
end
|
||||||
|
load(data)
|
||||||
|
else
|
||||||
|
nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def get(key, &block)
|
||||||
|
requires :directory
|
||||||
|
path = file_path(key)
|
||||||
|
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
|
||||||
|
body = nil
|
||||||
|
::File.open(path) do |file|
|
||||||
|
body = file.read
|
||||||
|
end
|
||||||
|
new(data.merge!(:body => body))
|
||||||
|
end
|
||||||
|
else
|
||||||
|
nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
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
|
|
@ -0,0 +1,60 @@
|
||||||
|
module Fog
|
||||||
|
module Local
|
||||||
|
class Storage < Fog::Service
|
||||||
|
|
||||||
|
requires :local_root
|
||||||
|
|
||||||
|
model_path 'fog/local/models/storage'
|
||||||
|
collection :directories
|
||||||
|
model :directory
|
||||||
|
model :file
|
||||||
|
collection :files
|
||||||
|
|
||||||
|
class Mock
|
||||||
|
include Collections
|
||||||
|
|
||||||
|
def self.data
|
||||||
|
@data ||= Hash.new do |hash, key|
|
||||||
|
hash[key] = {}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.reset_data(keys=data.keys)
|
||||||
|
for key in [*keys]
|
||||||
|
data.delete(key)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def initialize(options={})
|
||||||
|
@local_root = ::File.expand_path(options[:local_root])
|
||||||
|
@data = self.class.data[@local_root]
|
||||||
|
end
|
||||||
|
|
||||||
|
def local_root
|
||||||
|
@local_root
|
||||||
|
end
|
||||||
|
|
||||||
|
def path(partial)
|
||||||
|
partial
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class Real
|
||||||
|
include Collections
|
||||||
|
|
||||||
|
def initialize(options={})
|
||||||
|
@local_root = ::File.expand_path(options[:local_root])
|
||||||
|
end
|
||||||
|
|
||||||
|
def local_root
|
||||||
|
@local_root
|
||||||
|
end
|
||||||
|
|
||||||
|
def path_to(partial)
|
||||||
|
::File.join(@local_root, partial)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in New Issue