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:
parent
8e3ee75555
commit
61ff93af32
4 changed files with 113 additions and 1 deletions
|
@ -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 = {})
|
||||
|
|
|
@ -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
|
||||
|
|
40
tests/local/models/file_tests.rb
Normal file
40
tests/local/models/file_tests.rb
Normal 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
|
37
tests/local/storage_tests.rb
Normal file
37
tests/local/storage_tests.rb
Normal 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
|
Loading…
Reference in a new issue