2012-05-15 14:54:20 -04:00
|
|
|
require 'fog/local/storage'
|
2011-08-31 15:52:53 -05:00
|
|
|
require 'fog/storage'
|
|
|
|
|
2010-09-08 12:50:38 -07:00
|
|
|
module Fog
|
2011-06-15 14:26:43 -07:00
|
|
|
module Storage
|
|
|
|
class Local < Fog::Service
|
2010-09-08 12:50:38 -07:00
|
|
|
|
2010-12-16 15:31:24 -08:00
|
|
|
requires :local_root
|
2012-07-22 20:43:41 -07:00
|
|
|
recognizes :endpoint, :scheme, :host, :port, :path
|
2010-09-08 12:50:38 -07:00
|
|
|
|
2011-08-24 14:07:16 -05:00
|
|
|
model_path 'fog/local/models/storage'
|
2010-09-08 12:50:38 -07:00
|
|
|
collection :directories
|
|
|
|
model :directory
|
|
|
|
model :file
|
|
|
|
collection :files
|
|
|
|
|
2012-07-22 20:43:41 -07:00
|
|
|
require 'uri'
|
|
|
|
|
2010-09-08 12:50:38 -07:00
|
|
|
class Mock
|
|
|
|
|
2012-07-22 20:43:41 -07:00
|
|
|
attr_reader :endpoint
|
|
|
|
|
2010-09-08 12:50:38 -07: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 12:50:38 -07:00
|
|
|
def initialize(options={})
|
2011-01-17 18:04:00 -08:00
|
|
|
Fog::Mock.not_implemented
|
|
|
|
|
2011-01-26 15:56:39 -08:00
|
|
|
require 'mime/types'
|
2010-09-08 12:50:38 -07:00
|
|
|
@local_root = ::File.expand_path(options[:local_root])
|
2012-07-22 20:43:41 -07:00
|
|
|
|
|
|
|
@endpoint = options[:endpoint] || build_endpoint_from_options(options)
|
2011-05-19 15:35:33 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
def data
|
|
|
|
self.class.data[@local_root]
|
2010-09-08 12:50:38 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
def local_root
|
|
|
|
@local_root
|
|
|
|
end
|
|
|
|
|
2010-12-14 11:43:59 -08:00
|
|
|
def path_to(partial)
|
|
|
|
::File.join(@local_root, partial)
|
2010-09-08 12:50:38 -07:00
|
|
|
end
|
2011-03-10 11:16:55 -08:00
|
|
|
|
|
|
|
def reset_data
|
|
|
|
self.class.data.delete(@local_root)
|
|
|
|
end
|
|
|
|
|
2012-07-22 20:43:41 -07:00
|
|
|
private
|
|
|
|
def build_endpoint_from_options(options)
|
|
|
|
return unless options[:host]
|
|
|
|
|
|
|
|
URI::Generic.build(options).to_s
|
|
|
|
end
|
2010-09-08 12:50:38 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
class Real
|
|
|
|
|
2012-07-22 20:43:41 -07:00
|
|
|
attr_reader :endpoint
|
|
|
|
|
2010-09-08 12:50:38 -07:00
|
|
|
def initialize(options={})
|
2011-01-26 15:56:39 -08:00
|
|
|
require 'mime/types'
|
2010-09-08 12:50:38 -07:00
|
|
|
@local_root = ::File.expand_path(options[:local_root])
|
2012-07-22 20:43:41 -07:00
|
|
|
|
|
|
|
@endpoint = options[:endpoint] || build_endpoint_from_options(options)
|
2010-09-08 12:50:38 -07: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 10:34:26 -07: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 20:43:41 -07:00
|
|
|
|
|
|
|
private
|
|
|
|
def build_endpoint_from_options(options)
|
|
|
|
return unless options[:host]
|
|
|
|
|
|
|
|
URI::Generic.build(options).to_s
|
|
|
|
end
|
2010-09-08 12:50:38 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|