cloudfuji_paperclip 2.4.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +22 -0
- data/.travis.yml +13 -0
- data/Appraisals +14 -0
- data/CONTRIBUTING.md +38 -0
- data/Gemfile +5 -0
- data/Gemfile.lock +137 -0
- data/LICENSE +26 -0
- data/README.md +444 -0
- data/Rakefile +41 -0
- data/cucumber/paperclip_steps.rb +6 -0
- data/features/basic_integration.feature +46 -0
- data/features/rake_tasks.feature +63 -0
- data/features/step_definitions/attachment_steps.rb +65 -0
- data/features/step_definitions/html_steps.rb +15 -0
- data/features/step_definitions/rails_steps.rb +182 -0
- data/features/step_definitions/s3_steps.rb +14 -0
- data/features/step_definitions/web_steps.rb +209 -0
- data/features/support/env.rb +8 -0
- data/features/support/fakeweb.rb +3 -0
- data/features/support/fixtures/.boot_config.rb.swo +0 -0
- data/features/support/fixtures/boot_config.txt +15 -0
- data/features/support/fixtures/gemfile.txt +5 -0
- data/features/support/fixtures/preinitializer.txt +20 -0
- data/features/support/paths.rb +28 -0
- data/features/support/rails.rb +46 -0
- data/features/support/selectors.rb +19 -0
- data/gemfiles/rails2.gemfile +9 -0
- data/gemfiles/rails3.gemfile +9 -0
- data/gemfiles/rails3_1.gemfile +9 -0
- data/generators/paperclip/USAGE +5 -0
- data/generators/paperclip/paperclip_generator.rb +27 -0
- data/generators/paperclip/templates/paperclip_migration.rb.erb +19 -0
- data/init.rb +4 -0
- data/lib/generators/paperclip/USAGE +8 -0
- data/lib/generators/paperclip/paperclip_generator.rb +33 -0
- data/lib/generators/paperclip/templates/paperclip_migration.rb.erb +19 -0
- data/lib/paperclip/attachment.rb +468 -0
- data/lib/paperclip/callback_compatibility.rb +61 -0
- data/lib/paperclip/geometry.rb +120 -0
- data/lib/paperclip/interpolations.rb +174 -0
- data/lib/paperclip/iostream.rb +45 -0
- data/lib/paperclip/matchers/have_attached_file_matcher.rb +57 -0
- data/lib/paperclip/matchers/validate_attachment_content_type_matcher.rb +81 -0
- data/lib/paperclip/matchers/validate_attachment_presence_matcher.rb +54 -0
- data/lib/paperclip/matchers/validate_attachment_size_matcher.rb +95 -0
- data/lib/paperclip/matchers.rb +64 -0
- data/lib/paperclip/missing_attachment_styles.rb +87 -0
- data/lib/paperclip/processor.rb +58 -0
- data/lib/paperclip/railtie.rb +31 -0
- data/lib/paperclip/schema.rb +39 -0
- data/lib/paperclip/storage/filesystem.rb +81 -0
- data/lib/paperclip/storage/fog.rb +174 -0
- data/lib/paperclip/storage/s3.rb +333 -0
- data/lib/paperclip/storage.rb +3 -0
- data/lib/paperclip/style.rb +103 -0
- data/lib/paperclip/thumbnail.rb +105 -0
- data/lib/paperclip/upfile.rb +62 -0
- data/lib/paperclip/url_generator.rb +64 -0
- data/lib/paperclip/version.rb +3 -0
- data/lib/paperclip.rb +487 -0
- data/lib/tasks/paperclip.rake +101 -0
- data/paperclip.gemspec +41 -0
- data/rails/init.rb +2 -0
- data/shoulda_macros/paperclip.rb +124 -0
- data/test/.gitignore +1 -0
- data/test/attachment_test.rb +1116 -0
- data/test/database.yml +4 -0
- data/test/fixtures/12k.png +0 -0
- data/test/fixtures/50x50.png +0 -0
- data/test/fixtures/5k.png +0 -0
- data/test/fixtures/animated.gif +0 -0
- data/test/fixtures/bad.png +1 -0
- data/test/fixtures/fog.yml +8 -0
- data/test/fixtures/question?mark.png +0 -0
- data/test/fixtures/s3.yml +8 -0
- data/test/fixtures/spaced file.png +0 -0
- data/test/fixtures/text.txt +1 -0
- data/test/fixtures/twopage.pdf +0 -0
- data/test/fixtures/uppercase.PNG +0 -0
- data/test/geometry_test.rb +206 -0
- data/test/helper.rb +177 -0
- data/test/integration_test.rb +654 -0
- data/test/interpolations_test.rb +216 -0
- data/test/iostream_test.rb +71 -0
- data/test/matchers/have_attached_file_matcher_test.rb +24 -0
- data/test/matchers/validate_attachment_content_type_matcher_test.rb +87 -0
- data/test/matchers/validate_attachment_presence_matcher_test.rb +26 -0
- data/test/matchers/validate_attachment_size_matcher_test.rb +51 -0
- data/test/paperclip_missing_attachment_styles_test.rb +80 -0
- data/test/paperclip_test.rb +390 -0
- data/test/processor_test.rb +10 -0
- data/test/schema_test.rb +98 -0
- data/test/storage/filesystem_test.rb +56 -0
- data/test/storage/fog_test.rb +219 -0
- data/test/storage/s3_live_test.rb +138 -0
- data/test/storage/s3_test.rb +943 -0
- data/test/style_test.rb +209 -0
- data/test/support/mock_attachment.rb +22 -0
- data/test/support/mock_interpolator.rb +24 -0
- data/test/support/mock_model.rb +2 -0
- data/test/support/mock_url_generator_builder.rb +27 -0
- data/test/thumbnail_test.rb +383 -0
- data/test/upfile_test.rb +53 -0
- data/test/url_generator_test.rb +187 -0
- metadata +404 -0
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
|
|
2
|
+
require 'set'
|
|
3
|
+
module Paperclip
|
|
4
|
+
|
|
5
|
+
class << self
|
|
6
|
+
attr_accessor :classes_with_attachments
|
|
7
|
+
attr_writer :registered_attachments_styles_path
|
|
8
|
+
def registered_attachments_styles_path
|
|
9
|
+
@registered_attachments_styles_path ||= Rails.root.join('public/system/paperclip_attachments.yml').to_s
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
self.classes_with_attachments = Set.new
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
# Get list of styles saved on previous deploy (running rake paperclip:refresh:missing_styles)
|
|
17
|
+
def self.get_registered_attachments_styles
|
|
18
|
+
YAML.load_file(Paperclip.registered_attachments_styles_path)
|
|
19
|
+
rescue Errno::ENOENT
|
|
20
|
+
nil
|
|
21
|
+
end
|
|
22
|
+
private_class_method :get_registered_attachments_styles
|
|
23
|
+
|
|
24
|
+
def self.save_current_attachments_styles!
|
|
25
|
+
File.open(Paperclip.registered_attachments_styles_path, 'w') do |f|
|
|
26
|
+
YAML.dump(current_attachments_styles, f)
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# Returns hash with styles for all classes using Paperclip.
|
|
31
|
+
# Unfortunately current version does not work with lambda styles:(
|
|
32
|
+
# {
|
|
33
|
+
# :User => {:avatar => [:small, :big]},
|
|
34
|
+
# :Book => {
|
|
35
|
+
# :cover => [:thumb, :croppable]},
|
|
36
|
+
# :sample => [:thumb, :big]},
|
|
37
|
+
# }
|
|
38
|
+
# }
|
|
39
|
+
def self.current_attachments_styles
|
|
40
|
+
Hash.new.tap do |current_styles|
|
|
41
|
+
Paperclip.classes_with_attachments.each do |klass_name|
|
|
42
|
+
klass = Paperclip.class_for(klass_name)
|
|
43
|
+
klass.attachment_definitions.each do |attachment_name, attachment_attributes|
|
|
44
|
+
# TODO: is it even possible to take into account Procs?
|
|
45
|
+
next if attachment_attributes[:styles].kind_of?(Proc)
|
|
46
|
+
attachment_attributes[:styles].try(:keys).try(:each) do |style_name|
|
|
47
|
+
klass_sym = klass.to_s.to_sym
|
|
48
|
+
current_styles[klass_sym] ||= Hash.new
|
|
49
|
+
current_styles[klass_sym][attachment_name.to_sym] ||= Array.new
|
|
50
|
+
current_styles[klass_sym][attachment_name.to_sym] << style_name.to_sym
|
|
51
|
+
current_styles[klass_sym][attachment_name.to_sym].map!(&:to_s).sort!.map!(&:to_sym).uniq!
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
private_class_method :current_attachments_styles
|
|
58
|
+
|
|
59
|
+
# Returns hash with styles missing from recent run of rake paperclip:refresh:missing_styles
|
|
60
|
+
# {
|
|
61
|
+
# :User => {:avatar => [:big]},
|
|
62
|
+
# :Book => {
|
|
63
|
+
# :cover => [:croppable]},
|
|
64
|
+
# }
|
|
65
|
+
# }
|
|
66
|
+
def self.missing_attachments_styles
|
|
67
|
+
current_styles = current_attachments_styles
|
|
68
|
+
registered_styles = get_registered_attachments_styles
|
|
69
|
+
|
|
70
|
+
Hash.new.tap do |missing_styles|
|
|
71
|
+
current_styles.each do |klass, attachment_definitions|
|
|
72
|
+
attachment_definitions.each do |attachment_name, styles|
|
|
73
|
+
registered = registered_styles[klass][attachment_name] rescue []
|
|
74
|
+
missed = styles - registered
|
|
75
|
+
if missed.present?
|
|
76
|
+
klass_sym = klass.to_s.to_sym
|
|
77
|
+
missing_styles[klass_sym] ||= Hash.new
|
|
78
|
+
missing_styles[klass_sym][attachment_name.to_sym] ||= Array.new
|
|
79
|
+
missing_styles[klass_sym][attachment_name.to_sym].concat(missed.to_a)
|
|
80
|
+
missing_styles[klass_sym][attachment_name.to_sym].map!(&:to_s).sort!.map!(&:to_sym).uniq!
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
end
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
module Paperclip
|
|
2
|
+
# Paperclip processors allow you to modify attached files when they are
|
|
3
|
+
# attached in any way you are able. Paperclip itself uses command-line
|
|
4
|
+
# programs for its included Thumbnail processor, but custom processors
|
|
5
|
+
# are not required to follow suit.
|
|
6
|
+
#
|
|
7
|
+
# Processors are required to be defined inside the Paperclip module and
|
|
8
|
+
# are also required to be a subclass of Paperclip::Processor. There is
|
|
9
|
+
# only one method you *must* implement to properly be a subclass:
|
|
10
|
+
# #make, but #initialize may also be of use. Both methods accept 3
|
|
11
|
+
# arguments: the file that will be operated on (which is an instance of
|
|
12
|
+
# File), a hash of options that were defined in has_attached_file's
|
|
13
|
+
# style hash, and the Paperclip::Attachment itself.
|
|
14
|
+
#
|
|
15
|
+
# All #make needs to return is an instance of File (Tempfile is
|
|
16
|
+
# acceptable) which contains the results of the processing.
|
|
17
|
+
#
|
|
18
|
+
# See Paperclip.run for more information about using command-line
|
|
19
|
+
# utilities from within Processors.
|
|
20
|
+
class Processor
|
|
21
|
+
attr_accessor :file, :options, :attachment
|
|
22
|
+
|
|
23
|
+
def initialize file, options = {}, attachment = nil
|
|
24
|
+
@file = file
|
|
25
|
+
@options = options
|
|
26
|
+
@attachment = attachment
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def make
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def self.make file, options = {}, attachment = nil
|
|
33
|
+
new(file, options, attachment).make
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# Due to how ImageMagick handles its image format conversion and how Tempfile
|
|
38
|
+
# handles its naming scheme, it is necessary to override how Tempfile makes
|
|
39
|
+
# its names so as to allow for file extensions. Idea taken from the comments
|
|
40
|
+
# on this blog post:
|
|
41
|
+
# http://marsorange.com/archives/of-mogrify-ruby-tempfile-dynamic-class-definitions
|
|
42
|
+
class Tempfile < ::Tempfile
|
|
43
|
+
# This is Ruby 1.8.7's implementation.
|
|
44
|
+
if RUBY_VERSION <= "1.8.6" || RUBY_PLATFORM =~ /java/
|
|
45
|
+
def make_tmpname(basename, n)
|
|
46
|
+
case basename
|
|
47
|
+
when Array
|
|
48
|
+
prefix, suffix = *basename
|
|
49
|
+
else
|
|
50
|
+
prefix, suffix = basename, ''
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
t = Time.now.strftime("%y%m%d")
|
|
54
|
+
path = "#{prefix}#{t}-#{$$}-#{rand(0x100000000).to_s(36)}-#{n}#{suffix}"
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
end
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
require 'paperclip'
|
|
2
|
+
require 'paperclip/schema'
|
|
3
|
+
|
|
4
|
+
module Paperclip
|
|
5
|
+
if defined? Rails::Railtie
|
|
6
|
+
require 'rails'
|
|
7
|
+
class Railtie < Rails::Railtie
|
|
8
|
+
initializer 'paperclip.insert_into_active_record' do
|
|
9
|
+
ActiveSupport.on_load :active_record do
|
|
10
|
+
Paperclip::Railtie.insert
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
rake_tasks do
|
|
14
|
+
load "tasks/paperclip.rake"
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
class Railtie
|
|
20
|
+
def self.insert
|
|
21
|
+
ActiveRecord::Base.send(:include, Paperclip::Glue)
|
|
22
|
+
File.send(:include, Paperclip::Upfile)
|
|
23
|
+
|
|
24
|
+
Paperclip.options[:logger] = defined?(ActiveRecord) ? ActiveRecord::Base.logger : Rails.logger
|
|
25
|
+
|
|
26
|
+
ActiveRecord::ConnectionAdapters::AbstractAdapter.send(:include, Paperclip::Schema)
|
|
27
|
+
ActiveRecord::ConnectionAdapters::Table.send(:include, Paperclip::Schema)
|
|
28
|
+
ActiveRecord::ConnectionAdapters::TableDefinition.send(:include, Paperclip::Schema)
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
module Paperclip
|
|
2
|
+
# Provides two helpers that can be used in migrations.
|
|
3
|
+
#
|
|
4
|
+
# In order to use this module, the target class should implement a
|
|
5
|
+
# +column+ method that takes the column name and type, both as symbols,
|
|
6
|
+
# as well as a +remove_column+ method that takes a table and column name,
|
|
7
|
+
# also both symbols.
|
|
8
|
+
module Schema
|
|
9
|
+
@@columns = {:file_name => :string,
|
|
10
|
+
:content_type => :string,
|
|
11
|
+
:file_size => :integer,
|
|
12
|
+
:updated_at => :datetime}
|
|
13
|
+
|
|
14
|
+
def has_attached_file(attachment_name)
|
|
15
|
+
with_columns_for(attachment_name) do |column_name, column_type|
|
|
16
|
+
column(column_name, column_type)
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def drop_attached_file(table_name, attachment_name)
|
|
21
|
+
with_columns_for(attachment_name) do |column_name, column_type|
|
|
22
|
+
remove_column(table_name, column_name)
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
protected
|
|
27
|
+
|
|
28
|
+
def with_columns_for(attachment_name)
|
|
29
|
+
@@columns.each do |suffix, column_type|
|
|
30
|
+
column_name = full_column_name(attachment_name, suffix)
|
|
31
|
+
yield column_name, column_type
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def full_column_name(attachment_name, column_name)
|
|
36
|
+
"#{attachment_name}_#{column_name}".to_sym
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
module Paperclip
|
|
2
|
+
module Storage
|
|
3
|
+
# The default place to store attachments is in the filesystem. Files on the local
|
|
4
|
+
# filesystem can be very easily served by Apache without requiring a hit to your app.
|
|
5
|
+
# They also can be processed more easily after they've been saved, as they're just
|
|
6
|
+
# normal files. There is one Filesystem-specific option for has_attached_file.
|
|
7
|
+
# * +path+: The location of the repository of attachments on disk. This can (and, in
|
|
8
|
+
# almost all cases, should) be coordinated with the value of the +url+ option to
|
|
9
|
+
# allow files to be saved into a place where Apache can serve them without
|
|
10
|
+
# hitting your app. Defaults to
|
|
11
|
+
# ":rails_root/public/:attachment/:id/:style/:basename.:extension"
|
|
12
|
+
# By default this places the files in the app's public directory which can be served
|
|
13
|
+
# directly. If you are using capistrano for deployment, a good idea would be to
|
|
14
|
+
# make a symlink to the capistrano-created system directory from inside your app's
|
|
15
|
+
# public directory.
|
|
16
|
+
# See Paperclip::Attachment#interpolate for more information on variable interpolaton.
|
|
17
|
+
# :path => "/var/app/attachments/:class/:id/:style/:basename.:extension"
|
|
18
|
+
module Filesystem
|
|
19
|
+
def self.extended base
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def exists?(style_name = default_style)
|
|
23
|
+
if original_filename
|
|
24
|
+
File.exist?(path(style_name))
|
|
25
|
+
else
|
|
26
|
+
false
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# Returns representation of the data of the file assigned to the given
|
|
31
|
+
# style, in the format most representative of the current storage.
|
|
32
|
+
def to_file style_name = default_style
|
|
33
|
+
@queued_for_write[style_name] || (File.new(path(style_name), 'rb') if exists?(style_name))
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def flush_writes #:nodoc:
|
|
37
|
+
@queued_for_write.each do |style_name, file|
|
|
38
|
+
file.close
|
|
39
|
+
FileUtils.mkdir_p(File.dirname(path(style_name)))
|
|
40
|
+
log("saving #{path(style_name)}")
|
|
41
|
+
begin
|
|
42
|
+
FileUtils.mv(file.path, path(style_name))
|
|
43
|
+
rescue SystemCallError
|
|
44
|
+
FileUtils.cp(file.path, path(style_name))
|
|
45
|
+
FileUtils.rm(file.path)
|
|
46
|
+
end
|
|
47
|
+
FileUtils.chmod(0666&~File.umask, path(style_name))
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
after_flush_writes # allows attachment to clean up temp files
|
|
51
|
+
|
|
52
|
+
@queued_for_write = {}
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def flush_deletes #:nodoc:
|
|
56
|
+
@queued_for_delete.each do |path|
|
|
57
|
+
begin
|
|
58
|
+
log("deleting #{path}")
|
|
59
|
+
FileUtils.rm(path) if File.exist?(path)
|
|
60
|
+
rescue Errno::ENOENT => e
|
|
61
|
+
# ignore file-not-found, let everything else pass
|
|
62
|
+
end
|
|
63
|
+
begin
|
|
64
|
+
while(true)
|
|
65
|
+
path = File.dirname(path)
|
|
66
|
+
FileUtils.rmdir(path)
|
|
67
|
+
break if File.exists?(path) # Ruby 1.9.2 does not raise if the removal failed.
|
|
68
|
+
end
|
|
69
|
+
rescue Errno::EEXIST, Errno::ENOTEMPTY, Errno::ENOENT, Errno::EINVAL, Errno::ENOTDIR, Errno::EACCES
|
|
70
|
+
# Stop trying to remove parent directories
|
|
71
|
+
rescue SystemCallError => e
|
|
72
|
+
log("There was an unexpected error while deleting directories: #{e.class}")
|
|
73
|
+
# Ignore it
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
@queued_for_delete = []
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
end
|
|
81
|
+
end
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
module Paperclip
|
|
2
|
+
module Storage
|
|
3
|
+
# fog is a modern and versatile cloud computing library for Ruby.
|
|
4
|
+
# Among others, it supports Amazon S3 to store your files. In
|
|
5
|
+
# contrast to the outdated AWS-S3 gem it is actively maintained and
|
|
6
|
+
# supports multiple locations.
|
|
7
|
+
# Amazon's S3 file hosting service is a scalable, easy place to
|
|
8
|
+
# store files for distribution. You can find out more about it at
|
|
9
|
+
# http://aws.amazon.com/s3 There are a few fog-specific options for
|
|
10
|
+
# has_attached_file, which will be explained using S3 as an example:
|
|
11
|
+
# * +fog_credentials+: Takes a Hash with your credentials. For S3,
|
|
12
|
+
# you can use the following format:
|
|
13
|
+
# aws_access_key_id: '<your aws_access_key_id>'
|
|
14
|
+
# aws_secret_access_key: '<your aws_secret_access_key>'
|
|
15
|
+
# provider: 'AWS'
|
|
16
|
+
# region: 'eu-west-1'
|
|
17
|
+
# * +fog_directory+: This is the name of the S3 bucket that will
|
|
18
|
+
# store your files. Remember that the bucket must be unique across
|
|
19
|
+
# all of Amazon S3. If the bucket does not exist, Paperclip will
|
|
20
|
+
# attempt to create it.
|
|
21
|
+
# * +path+: This is the key under the bucket in which the file will
|
|
22
|
+
# be stored. The URL will be constructed from the bucket and the
|
|
23
|
+
# path. This is what you will want to interpolate. Keys should be
|
|
24
|
+
# unique, like filenames, and despite the fact that S3 (strictly
|
|
25
|
+
# speaking) does not support directories, you can still use a / to
|
|
26
|
+
# separate parts of your file name.
|
|
27
|
+
# * +fog_public+: (optional, defaults to true) Should the uploaded
|
|
28
|
+
# files be public or not? (true/false)
|
|
29
|
+
# * +fog_host+: (optional) The fully-qualified domain name (FQDN)
|
|
30
|
+
# that is the alias to the S3 domain of your bucket, e.g.
|
|
31
|
+
# 'http://images.example.com'. This can also be used in
|
|
32
|
+
# conjunction with Cloudfront (http://aws.amazon.com/cloudfront)
|
|
33
|
+
|
|
34
|
+
module Fog
|
|
35
|
+
def self.extended base
|
|
36
|
+
begin
|
|
37
|
+
require 'fog'
|
|
38
|
+
rescue LoadError => e
|
|
39
|
+
e.message << " (You may need to install the fog gem)"
|
|
40
|
+
raise e
|
|
41
|
+
end unless defined?(Fog)
|
|
42
|
+
|
|
43
|
+
base.instance_eval do
|
|
44
|
+
unless @options[:url].to_s.match(/^:fog.*url$/)
|
|
45
|
+
@options[:path] = @options[:path].gsub(/:url/, @options[:url])
|
|
46
|
+
@options[:url] = ':fog_public_url'
|
|
47
|
+
end
|
|
48
|
+
Paperclip.interpolates(:fog_public_url) do |attachment, style|
|
|
49
|
+
attachment.public_url(style)
|
|
50
|
+
end unless Paperclip::Interpolations.respond_to? :fog_public_url
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
AWS_BUCKET_SUBDOMAIN_RESTRICTON_REGEX = /^(?:[a-z]|\d(?!\d{0,2}(?:\.\d{1,3}){3}$))(?:[a-z0-9]|\.(?![\.\-])|\-(?![\.])){1,61}[a-z0-9]$/
|
|
55
|
+
|
|
56
|
+
def exists?(style = default_style)
|
|
57
|
+
if original_filename
|
|
58
|
+
!!directory.files.head(path(style))
|
|
59
|
+
else
|
|
60
|
+
false
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def fog_credentials
|
|
65
|
+
@fog_credentials ||= parse_credentials(@options[:fog_credentials])
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def fog_file
|
|
69
|
+
@fog_file ||= @options[:fog_file] || {}
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def fog_public
|
|
73
|
+
return @fog_public if defined?(@fog_public)
|
|
74
|
+
@fog_public = defined?(@options[:fog_public]) ? @options[:fog_public] : true
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def flush_writes
|
|
78
|
+
for style, file in @queued_for_write do
|
|
79
|
+
log("saving #{path(style)}")
|
|
80
|
+
retried = false
|
|
81
|
+
begin
|
|
82
|
+
directory.files.create(fog_file.merge(
|
|
83
|
+
:body => file,
|
|
84
|
+
:key => path(style),
|
|
85
|
+
:public => fog_public
|
|
86
|
+
))
|
|
87
|
+
rescue Excon::Errors::NotFound
|
|
88
|
+
raise if retried
|
|
89
|
+
retried = true
|
|
90
|
+
directory.save
|
|
91
|
+
retry
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
after_flush_writes # allows attachment to clean up temp files
|
|
96
|
+
|
|
97
|
+
@queued_for_write = {}
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
def flush_deletes
|
|
101
|
+
for path in @queued_for_delete do
|
|
102
|
+
log("deleting #{path}")
|
|
103
|
+
directory.files.new(:key => path).destroy
|
|
104
|
+
end
|
|
105
|
+
@queued_for_delete = []
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
# Returns representation of the data of the file assigned to the given
|
|
109
|
+
# style, in the format most representative of the current storage.
|
|
110
|
+
def to_file(style = default_style)
|
|
111
|
+
if @queued_for_write[style]
|
|
112
|
+
@queued_for_write[style]
|
|
113
|
+
else
|
|
114
|
+
body = directory.files.get(path(style)).body
|
|
115
|
+
filename = path(style)
|
|
116
|
+
extname = File.extname(filename)
|
|
117
|
+
basename = File.basename(filename, extname)
|
|
118
|
+
file = Tempfile.new([basename, extname])
|
|
119
|
+
file.binmode
|
|
120
|
+
file.write(body)
|
|
121
|
+
file.rewind
|
|
122
|
+
file
|
|
123
|
+
end
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
def public_url(style = default_style)
|
|
127
|
+
if @options[:fog_host]
|
|
128
|
+
host = (@options[:fog_host] =~ /%d/) ? @options[:fog_host] % (path(style).hash % 4) : @options[:fog_host]
|
|
129
|
+
"#{host}/#{path(style)}"
|
|
130
|
+
else
|
|
131
|
+
if fog_credentials[:provider] == 'AWS'
|
|
132
|
+
if @options[:fog_directory].to_s =~ Fog::AWS_BUCKET_SUBDOMAIN_RESTRICTON_REGEX
|
|
133
|
+
"https://#{@options[:fog_directory]}.s3.amazonaws.com/#{path(style)}"
|
|
134
|
+
else
|
|
135
|
+
# directory is not a valid subdomain, so use path style for access
|
|
136
|
+
"https://s3.amazonaws.com/#{@options[:fog_directory]}/#{path(style)}"
|
|
137
|
+
end
|
|
138
|
+
else
|
|
139
|
+
directory.files.new(:key => path(style)).public_url
|
|
140
|
+
end
|
|
141
|
+
end
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
def parse_credentials(creds)
|
|
145
|
+
creds = find_credentials(creds).stringify_keys
|
|
146
|
+
env = Object.const_defined?(:Rails) ? Rails.env : nil
|
|
147
|
+
(creds[env] || creds).symbolize_keys
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
private
|
|
151
|
+
|
|
152
|
+
def find_credentials(creds)
|
|
153
|
+
case creds
|
|
154
|
+
when File
|
|
155
|
+
YAML::load(ERB.new(File.read(creds.path)).result)
|
|
156
|
+
when String, Pathname
|
|
157
|
+
YAML::load(ERB.new(File.read(creds)).result)
|
|
158
|
+
when Hash
|
|
159
|
+
creds
|
|
160
|
+
else
|
|
161
|
+
raise ArgumentError, "Credentials are not a path, file, or hash."
|
|
162
|
+
end
|
|
163
|
+
end
|
|
164
|
+
|
|
165
|
+
def connection
|
|
166
|
+
@connection ||= ::Fog::Storage.new(fog_credentials)
|
|
167
|
+
end
|
|
168
|
+
|
|
169
|
+
def directory
|
|
170
|
+
@directory ||= connection.directories.new(:key => @options[:fog_directory])
|
|
171
|
+
end
|
|
172
|
+
end
|
|
173
|
+
end
|
|
174
|
+
end
|