This repository has been archived by the owner on Jul 13, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
225 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1,2 +1,3 @@ | |||
require "paperclip/storage/filesystem" | require "paperclip/storage/filesystem" | ||
require "paperclip/storage/fog" | |||
require "paperclip/storage/s3" | require "paperclip/storage/s3" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,98 @@ | |||
module Paperclip | |||
module Storage | |||
|
|||
module Fog | |||
def self.extended base | |||
begin | |||
require 'fog' | |||
rescue LoadError => e | |||
e.message << " (You may need to install the fog gem)" | |||
raise e | |||
end | |||
|
|||
base.instance_eval do | |||
@fog_directory = @options[:fog_directory] | |||
@fog_credentials = @options[:fog_credentials] | |||
@fog_host = @options[:fog_host] | |||
@fog_public = @options[:fog_public] | |||
|
|||
@url = ':fog_public_url' | |||
Paperclip.interpolates(:fog_public_url) do |attachment, style| | |||
attachment.public_url(style) | |||
end | |||
end | |||
end | |||
|
|||
def exists?(style = default_style) | |||
if original_filename | |||
!!directory.files.head(path(style)) | |||
else | |||
false | |||
end | |||
end | |||
|
|||
def flush_writes | |||
for style, file in @queued_for_write do | |||
log("saving #{path(style)}") | |||
directory.files.create( | |||
:body => file, | |||
:key => path(style), | |||
:public => @fog_public | |||
) | |||
end | |||
@queued_for_write = {} | |||
end | |||
|
|||
def flush_deletes | |||
for path in @queued_for_delete do | |||
log("deleting #{path}") | |||
directory.files.new(:key => path).destroy | |||
end | |||
@queued_for_delete = [] | |||
end | |||
|
|||
# Returns representation of the data of the file assigned to the given | |||
# style, in the format most representative of the current storage. | |||
def to_file(style = default_style) | |||
if @queued_for_write[style] | |||
@queued_for_write[style] | |||
else | |||
body = directory.files.get(path(style)).body | |||
filename = path(style) | |||
extname = File.extname(filename) | |||
basename = File.basename(filename, extname) | |||
file = Tempfile.new([basename, extname]) | |||
file.binmode | |||
file.write(body) | |||
file.rewind | |||
file | |||
end | |||
end | |||
|
|||
def public_url(style = default_style) | |||
if @fog_host | |||
"#{@fog_host}/#{path(style)}" | |||
else | |||
directory.files.new(:key => path(style)).public_url | |||
end | |||
end | |||
|
|||
private | |||
|
|||
def connection | |||
@connection ||= ::Fog::Storage.new(@fog_credentials) | |||
end | |||
|
|||
def directory | |||
@directory ||= begin | |||
connection.directories.get(@fog_directory) || connection.directories.create( | |||
:key => @fog_directory, | |||
:public => @fog_public | |||
) | |||
end | |||
end | |||
|
|||
end | |||
|
|||
end | |||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,107 @@ | |||
require './test/helper' | |||
require 'fog' | |||
|
|||
Fog.mock! | |||
|
|||
class FogTest < Test::Unit::TestCase | |||
context "" do | |||
|
|||
setup do | |||
@fog_directory = 'papercliptests' | |||
|
|||
@credentials = { | |||
:provider => 'AWS', | |||
:aws_access_key_id => 'ID', | |||
:aws_secret_access_key => 'SECRET' | |||
} | |||
|
|||
@connection = Fog::Storage.new(@credentials) | |||
|
|||
rebuild_model( | |||
:fog_directory => @fog_directory, | |||
:fog_credentials => @credentials, | |||
:fog_host => nil, | |||
:fog_public => true, | |||
:path => ":attachment/:basename.:extension", | |||
:storage => :fog | |||
) | |||
end | |||
|
|||
should "be extended by the Fog module" do | |||
assert Dummy.new.avatar.is_a?(Paperclip::Storage::Fog) | |||
end | |||
|
|||
context "when assigned" do | |||
setup do | |||
@file = File.new(File.join(File.dirname(__FILE__), 'fixtures', '5k.png'), 'rb') | |||
@dummy = Dummy.new | |||
@dummy.avatar = @file | |||
end | |||
|
|||
teardown do | |||
@file.close | |||
directory = @connection.directories.new(:key => @fog_directory) | |||
directory.files.each {|file| file.destroy} | |||
directory.destroy | |||
end | |||
|
|||
context "without a bucket" do | |||
should "succeed" do | |||
assert @dummy.save | |||
end | |||
end | |||
|
|||
context "with a bucket" do | |||
setup do | |||
@connection.directories.create(:key => @fog_directory) | |||
end | |||
|
|||
should "succeed" do | |||
assert @dummy.save | |||
end | |||
end | |||
|
|||
context "without a fog_host" do | |||
setup do | |||
rebuild_model( | |||
:fog_directory => @fog_directory, | |||
:fog_credentials => @credentials, | |||
:fog_host => nil, | |||
:fog_public => true, | |||
:path => ":attachment/:basename.:extension", | |||
:storage => :fog | |||
) | |||
@dummy = Dummy.new | |||
@dummy.avatar = StringIO.new('.') | |||
@dummy.save | |||
end | |||
|
|||
should "provide a public url" do | |||
assert !@dummy.avatar.url.nil? | |||
end | |||
end | |||
|
|||
context "with a fog_host" do | |||
setup do | |||
rebuild_model( | |||
:fog_directory => @fog_directory, | |||
:fog_credentials => @credentials, | |||
:fog_host => 'http://example.com', | |||
:fog_public => true, | |||
:path => ":attachment/:basename.:extension", | |||
:storage => :fog | |||
) | |||
@dummy = Dummy.new | |||
@dummy.avatar = StringIO.new('.') | |||
@dummy.save | |||
end | |||
|
|||
should "provide a public url" do | |||
assert @dummy.avatar.url =~ /^http:\/\/example\.com\/avatars\/stringio\.txt\?\d*$/ | |||
end | |||
end | |||
|
|||
end | |||
|
|||
end | |||
end |