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

Local storage support for #public_url.

This commit is contained in:
Adam Tanner 2012-07-22 20:43:41 -07:00
parent 8e3ee75555
commit 61ff93af32
4 changed files with 113 additions and 1 deletions

View file

@ -12,6 +12,8 @@ module Fog
# attribute :content_type, :aliases => 'Content-Type' # attribute :content_type, :aliases => 'Content-Type'
attribute :last_modified, :aliases => 'Last-Modified' attribute :last_modified, :aliases => 'Last-Modified'
require 'uri'
def body def body
attributes[:body] ||= if last_modified attributes[:body] ||= if last_modified
collection.get(identity).body collection.get(identity).body
@ -73,7 +75,16 @@ module Fog
end end
def public_url 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 end
def save(options = {}) def save(options = {})

View file

@ -6,6 +6,7 @@ module Fog
class Local < Fog::Service class Local < Fog::Service
requires :local_root requires :local_root
recognizes :endpoint, :scheme, :host, :port, :path
model_path 'fog/local/models/storage' model_path 'fog/local/models/storage'
collection :directories collection :directories
@ -13,8 +14,12 @@ module Fog
model :file model :file
collection :files collection :files
require 'uri'
class Mock class Mock
attr_reader :endpoint
def self.data def self.data
@data ||= Hash.new do |hash, key| @data ||= Hash.new do |hash, key|
hash[key] = {} hash[key] = {}
@ -30,6 +35,8 @@ module Fog
require 'mime/types' require 'mime/types'
@local_root = ::File.expand_path(options[:local_root]) @local_root = ::File.expand_path(options[:local_root])
@endpoint = options[:endpoint] || build_endpoint_from_options(options)
end end
def data def data
@ -48,13 +55,23 @@ module Fog
self.class.data.delete(@local_root) self.class.data.delete(@local_root)
end end
private
def build_endpoint_from_options(options)
return unless options[:host]
URI::Generic.build(options).to_s
end
end end
class Real class Real
attr_reader :endpoint
def initialize(options={}) def initialize(options={})
require 'mime/types' require 'mime/types'
@local_root = ::File.expand_path(options[:local_root]) @local_root = ::File.expand_path(options[:local_root])
@endpoint = options[:endpoint] || build_endpoint_from_options(options)
end end
def local_root def local_root
@ -72,6 +89,13 @@ module Fog
::FileUtils.mkdir_p(::File.dirname(target_path)) ::FileUtils.mkdir_p(::File.dirname(target_path))
::FileUtils.copy_file(source_path, target_path) ::FileUtils.copy_file(source_path, target_path)
end end
private
def build_endpoint_from_options(options)
return unless options[:host]
URI::Generic.build(options).to_s
end
end end
end end

View file

@ -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

View file

@ -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