2012-05-15 14:54:20 -04:00
|
|
|
require 'fog/local/storage'
|
2011-08-31 16:52:53 -04:00
|
|
|
require 'fog/storage'
|
|
|
|
|
2010-09-08 15:50:38 -04:00
|
|
|
module Fog
|
2011-06-15 17:26:43 -04:00
|
|
|
module Storage
|
|
|
|
class Local < Fog::Service
|
2010-09-08 15:50:38 -04:00
|
|
|
|
2010-12-16 18:31:24 -05:00
|
|
|
requires :local_root
|
2012-07-22 23:43:41 -04:00
|
|
|
recognizes :endpoint, :scheme, :host, :port, :path
|
2010-09-08 15:50:38 -04:00
|
|
|
|
2011-08-24 15:07:16 -04:00
|
|
|
model_path 'fog/local/models/storage'
|
2010-09-08 15:50:38 -04:00
|
|
|
collection :directories
|
|
|
|
model :directory
|
|
|
|
model :file
|
|
|
|
collection :files
|
|
|
|
|
2012-07-22 23:43:41 -04:00
|
|
|
require 'uri'
|
|
|
|
|
2010-09-08 15:50:38 -04:00
|
|
|
class Mock
|
|
|
|
|
2012-07-22 23:43:41 -04:00
|
|
|
attr_reader :endpoint
|
|
|
|
|
2010-09-08 15:50:38 -04:00
|
|
|
def self.data
|
|
|
|
@data ||= Hash.new do |hash, key|
|
|
|
|
hash[key] = {}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-05-19 10:05:33 -04:00
|
|
|
def self.reset
|
|
|
|
@data = nil
|
|
|
|
end
|
|
|
|
|
2010-09-08 15:50:38 -04:00
|
|
|
def initialize(options={})
|
2011-01-17 21:04:00 -05:00
|
|
|
Fog::Mock.not_implemented
|
|
|
|
|
2010-09-08 15:50:38 -04:00
|
|
|
@local_root = ::File.expand_path(options[:local_root])
|
2012-07-22 23:43:41 -04:00
|
|
|
|
|
|
|
@endpoint = options[:endpoint] || build_endpoint_from_options(options)
|
2011-05-19 18:35:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def data
|
|
|
|
self.class.data[@local_root]
|
2010-09-08 15:50:38 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def local_root
|
|
|
|
@local_root
|
|
|
|
end
|
|
|
|
|
2010-12-14 14:43:59 -05:00
|
|
|
def path_to(partial)
|
|
|
|
::File.join(@local_root, partial)
|
2010-09-08 15:50:38 -04:00
|
|
|
end
|
2011-03-10 14:16:55 -05:00
|
|
|
|
|
|
|
def reset_data
|
|
|
|
self.class.data.delete(@local_root)
|
|
|
|
end
|
|
|
|
|
2012-07-22 23:43:41 -04:00
|
|
|
private
|
|
|
|
def build_endpoint_from_options(options)
|
|
|
|
return unless options[:host]
|
|
|
|
|
|
|
|
URI::Generic.build(options).to_s
|
|
|
|
end
|
2010-09-08 15:50:38 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
class Real
|
|
|
|
|
2012-07-22 23:43:41 -04:00
|
|
|
attr_reader :endpoint
|
|
|
|
|
2010-09-08 15:50:38 -04:00
|
|
|
def initialize(options={})
|
|
|
|
@local_root = ::File.expand_path(options[:local_root])
|
2012-07-22 23:43:41 -04:00
|
|
|
|
|
|
|
@endpoint = options[:endpoint] || build_endpoint_from_options(options)
|
2010-09-08 15:50:38 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def local_root
|
|
|
|
@local_root
|
|
|
|
end
|
|
|
|
|
|
|
|
def path_to(partial)
|
|
|
|
::File.join(@local_root, partial)
|
|
|
|
end
|
2012-02-16 14:34:45 -05:00
|
|
|
|
|
|
|
def copy_object(source_directory_name, source_object_name, target_directory_name, target_object_name, options={})
|
|
|
|
require 'fileutils'
|
|
|
|
source_path = path_to(::File.join(source_directory_name, source_object_name))
|
|
|
|
target_path = path_to(::File.join(target_directory_name, target_object_name))
|
2012-07-06 13:34:26 -04:00
|
|
|
::FileUtils.mkdir_p(::File.dirname(target_path))
|
2012-02-16 14:34:45 -05:00
|
|
|
::FileUtils.copy_file(source_path, target_path)
|
|
|
|
end
|
2012-07-22 23:43:41 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
def build_endpoint_from_options(options)
|
|
|
|
return unless options[:host]
|
|
|
|
|
|
|
|
URI::Generic.build(options).to_s
|
|
|
|
end
|
2010-09-08 15:50:38 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|