From 61ff93af32b76009651092cbd297a271b182b97e Mon Sep 17 00:00:00 2001 From: Adam Tanner Date: Sun, 22 Jul 2012 20:43:41 -0700 Subject: [PATCH] Local storage support for #public_url. --- lib/fog/local/models/storage/file.rb | 13 ++++++++- lib/fog/local/storage.rb | 24 +++++++++++++++++ tests/local/models/file_tests.rb | 40 ++++++++++++++++++++++++++++ tests/local/storage_tests.rb | 37 +++++++++++++++++++++++++ 4 files changed, 113 insertions(+), 1 deletion(-) create mode 100644 tests/local/models/file_tests.rb create mode 100644 tests/local/storage_tests.rb diff --git a/lib/fog/local/models/storage/file.rb b/lib/fog/local/models/storage/file.rb index 312328cb5..3140f72b0 100644 --- a/lib/fog/local/models/storage/file.rb +++ b/lib/fog/local/models/storage/file.rb @@ -12,6 +12,8 @@ module Fog # attribute :content_type, :aliases => 'Content-Type' attribute :last_modified, :aliases => 'Last-Modified' + require 'uri' + def body attributes[:body] ||= if last_modified collection.get(identity).body @@ -73,7 +75,16 @@ module Fog end def public_url - nil + requires :directory, :key + + if connection.endpoint + escaped_directory = URI.escape(directory.key) + escaped_key = URI.escape(key) + + ::File.join(connection.endpoint, escaped_directory, escaped_key) + else + nil + end end def save(options = {}) diff --git a/lib/fog/local/storage.rb b/lib/fog/local/storage.rb index 154cc3915..63555b3a1 100644 --- a/lib/fog/local/storage.rb +++ b/lib/fog/local/storage.rb @@ -6,6 +6,7 @@ module Fog class Local < Fog::Service requires :local_root + recognizes :endpoint, :scheme, :host, :port, :path model_path 'fog/local/models/storage' collection :directories @@ -13,8 +14,12 @@ module Fog model :file collection :files + require 'uri' + class Mock + attr_reader :endpoint + def self.data @data ||= Hash.new do |hash, key| hash[key] = {} @@ -30,6 +35,8 @@ module Fog require 'mime/types' @local_root = ::File.expand_path(options[:local_root]) + + @endpoint = options[:endpoint] || build_endpoint_from_options(options) end def data @@ -48,13 +55,23 @@ module Fog self.class.data.delete(@local_root) end + private + def build_endpoint_from_options(options) + return unless options[:host] + + URI::Generic.build(options).to_s + end end class Real + attr_reader :endpoint + def initialize(options={}) require 'mime/types' @local_root = ::File.expand_path(options[:local_root]) + + @endpoint = options[:endpoint] || build_endpoint_from_options(options) end def local_root @@ -72,6 +89,13 @@ module Fog ::FileUtils.mkdir_p(::File.dirname(target_path)) ::FileUtils.copy_file(source_path, target_path) end + + private + def build_endpoint_from_options(options) + return unless options[:host] + + URI::Generic.build(options).to_s + end end end diff --git a/tests/local/models/file_tests.rb b/tests/local/models/file_tests.rb new file mode 100644 index 000000000..f4dc5d3d1 --- /dev/null +++ b/tests/local/models/file_tests.rb @@ -0,0 +1,40 @@ +Shindo.tests('Storage[:local] | file', [:local]) do + before do + @options = { :local_root => '~/.fog' } + end + + tests('#public_url') do + tests('when connection has an endpoint'). + returns('http://example.com/files/directory/file.txt') do + @options[:endpoint] = 'http://example.com/files' + + connection = Fog::Storage::Local.new(@options) + directory = connection.directories.new(:key => 'directory') + file = directory.files.new(:key => 'file.txt') + + file.public_url + end + + tests('when connection has no endpoint'). + returns(nil) do + @options[:endpoint] = nil + + connection = Fog::Storage::Local.new(@options) + directory = connection.directories.new(:key => 'directory') + file = directory.files.new(:key => 'file.txt') + + file.public_url + end + + tests('when file path has escapable characters'). + returns('http://example.com/files/my%20directory/my%20file.txt') do + @options[:endpoint] = 'http://example.com/files' + + connection = Fog::Storage::Local.new(@options) + directory = connection.directories.new(:key => 'my directory') + file = directory.files.new(:key => 'my file.txt') + + file.public_url + end + end +end \ No newline at end of file diff --git a/tests/local/storage_tests.rb b/tests/local/storage_tests.rb new file mode 100644 index 000000000..e1755bd0b --- /dev/null +++ b/tests/local/storage_tests.rb @@ -0,0 +1,37 @@ +Shindo.tests('Local | storage') do + before do + @options = { :local_root => "~/.fog" } + end + + tests('#endpoint') do + tests('when no endpoint is provided'). + returns(nil) do + Fog::Storage::Local.new(@options).endpoint + end + + tests('when no host is provided'). + returns(nil) do + @options[:scheme] = 'http' + @options[:path] = '/files' + @options[:port] = 80 + + Fog::Storage::Local.new(@options).endpoint + end + + tests('when endpoint is provided'). + returns('http://example.com/files') do + @options[:endpoint] = 'http://example.com/files' + + Fog::Storage::Local.new(@options).endpoint + end + + tests('when at least host option is provided'). + returns('http://example.com/files') do + @options[:scheme] = 'http' + @options[:host] = 'example.com' + @options[:path] = '/files' + + Fog::Storage::Local.new(@options).endpoint + end + end +end \ No newline at end of file