jmcnevin-paperclip 2.4.5
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +26 -0
- data/README.md +414 -0
- data/Rakefile +86 -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.rb +480 -0
- data/lib/paperclip/attachment.rb +520 -0
- data/lib/paperclip/callback_compatibility.rb +61 -0
- data/lib/paperclip/geometry.rb +155 -0
- data/lib/paperclip/interpolations.rb +171 -0
- data/lib/paperclip/iostream.rb +45 -0
- data/lib/paperclip/matchers.rb +33 -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/missing_attachment_styles.rb +87 -0
- data/lib/paperclip/options.rb +78 -0
- data/lib/paperclip/processor.rb +58 -0
- data/lib/paperclip/railtie.rb +26 -0
- data/lib/paperclip/storage.rb +3 -0
- data/lib/paperclip/storage/filesystem.rb +81 -0
- data/lib/paperclip/storage/fog.rb +163 -0
- data/lib/paperclip/storage/s3.rb +270 -0
- data/lib/paperclip/style.rb +95 -0
- data/lib/paperclip/thumbnail.rb +105 -0
- data/lib/paperclip/upfile.rb +62 -0
- data/lib/paperclip/version.rb +3 -0
- data/lib/tasks/paperclip.rake +101 -0
- data/rails/init.rb +2 -0
- data/shoulda_macros/paperclip.rb +124 -0
- data/test/attachment_test.rb +1161 -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/double spaces in name.png +0 -0
- data/test/fixtures/fog.yml +8 -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/fog_test.rb +192 -0
- data/test/geometry_test.rb +206 -0
- data/test/helper.rb +158 -0
- data/test/integration_test.rb +781 -0
- data/test/interpolations_test.rb +202 -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/options_test.rb +75 -0
- data/test/paperclip_missing_attachment_styles_test.rb +80 -0
- data/test/paperclip_test.rb +340 -0
- data/test/processor_test.rb +10 -0
- data/test/storage/filesystem_test.rb +56 -0
- data/test/storage/s3_live_test.rb +88 -0
- data/test/storage/s3_test.rb +689 -0
- data/test/style_test.rb +180 -0
- data/test/thumbnail_test.rb +383 -0
- data/test/upfile_test.rb +53 -0
- metadata +294 -0
@@ -0,0 +1,57 @@
|
|
1
|
+
module Paperclip
|
2
|
+
module Shoulda
|
3
|
+
module Matchers
|
4
|
+
# Ensures that the given instance or class has an attachment with the
|
5
|
+
# given name.
|
6
|
+
#
|
7
|
+
# Example:
|
8
|
+
# describe User do
|
9
|
+
# it { should have_attached_file(:avatar) }
|
10
|
+
# end
|
11
|
+
def have_attached_file name
|
12
|
+
HaveAttachedFileMatcher.new(name)
|
13
|
+
end
|
14
|
+
|
15
|
+
class HaveAttachedFileMatcher
|
16
|
+
def initialize attachment_name
|
17
|
+
@attachment_name = attachment_name
|
18
|
+
end
|
19
|
+
|
20
|
+
def matches? subject
|
21
|
+
@subject = subject
|
22
|
+
@subject = @subject.class unless Class === @subject
|
23
|
+
responds? && has_column? && included?
|
24
|
+
end
|
25
|
+
|
26
|
+
def failure_message
|
27
|
+
"Should have an attachment named #{@attachment_name}"
|
28
|
+
end
|
29
|
+
|
30
|
+
def negative_failure_message
|
31
|
+
"Should not have an attachment named #{@attachment_name}"
|
32
|
+
end
|
33
|
+
|
34
|
+
def description
|
35
|
+
"have an attachment named #{@attachment_name}"
|
36
|
+
end
|
37
|
+
|
38
|
+
protected
|
39
|
+
|
40
|
+
def responds?
|
41
|
+
methods = @subject.instance_methods.map(&:to_s)
|
42
|
+
methods.include?("#{@attachment_name}") &&
|
43
|
+
methods.include?("#{@attachment_name}=") &&
|
44
|
+
methods.include?("#{@attachment_name}?")
|
45
|
+
end
|
46
|
+
|
47
|
+
def has_column?
|
48
|
+
@subject.column_names.include?("#{@attachment_name}_file_name")
|
49
|
+
end
|
50
|
+
|
51
|
+
def included?
|
52
|
+
@subject.ancestors.include?(Paperclip::InstanceMethods)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,81 @@
|
|
1
|
+
module Paperclip
|
2
|
+
module Shoulda
|
3
|
+
module Matchers
|
4
|
+
# Ensures that the given instance or class validates the content type of
|
5
|
+
# the given attachment as specified.
|
6
|
+
#
|
7
|
+
# Example:
|
8
|
+
# describe User do
|
9
|
+
# it { should validate_attachment_content_type(:icon).
|
10
|
+
# allowing('image/png', 'image/gif').
|
11
|
+
# rejecting('text/plain', 'text/xml') }
|
12
|
+
# end
|
13
|
+
def validate_attachment_content_type name
|
14
|
+
ValidateAttachmentContentTypeMatcher.new(name)
|
15
|
+
end
|
16
|
+
|
17
|
+
class ValidateAttachmentContentTypeMatcher
|
18
|
+
def initialize attachment_name
|
19
|
+
@attachment_name = attachment_name
|
20
|
+
@allowed_types = []
|
21
|
+
@rejected_types = []
|
22
|
+
end
|
23
|
+
|
24
|
+
def allowing *types
|
25
|
+
@allowed_types = types.flatten
|
26
|
+
self
|
27
|
+
end
|
28
|
+
|
29
|
+
def rejecting *types
|
30
|
+
@rejected_types = types.flatten
|
31
|
+
self
|
32
|
+
end
|
33
|
+
|
34
|
+
def matches? subject
|
35
|
+
@subject = subject
|
36
|
+
@subject = @subject.class unless Class === @subject
|
37
|
+
@allowed_types && @rejected_types &&
|
38
|
+
allowed_types_allowed? && rejected_types_rejected?
|
39
|
+
end
|
40
|
+
|
41
|
+
def failure_message
|
42
|
+
"".tap do |str|
|
43
|
+
str << "Content types #{@allowed_types.join(", ")} should be accepted" if @allowed_types.present?
|
44
|
+
str << "\n" if @allowed_types.present? && @rejected_types.present?
|
45
|
+
str << "Content types #{@rejected_types.join(", ")} should be rejected by #{@attachment_name}" if @rejected_types.present?
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def negative_failure_message
|
50
|
+
"".tap do |str|
|
51
|
+
str << "Content types #{@allowed_types.join(", ")} should be rejected" if @allowed_types.present?
|
52
|
+
str << "\n" if @allowed_types.present? && @rejected_types.present?
|
53
|
+
str << "Content types #{@rejected_types.join(", ")} should be accepted by #{@attachment_name}" if @rejected_types.present?
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def description
|
58
|
+
"validate the content types allowed on attachment #{@attachment_name}"
|
59
|
+
end
|
60
|
+
|
61
|
+
protected
|
62
|
+
|
63
|
+
def type_allowed?(type)
|
64
|
+
file = StringIO.new(".")
|
65
|
+
file.content_type = type
|
66
|
+
(subject = @subject.new).attachment_for(@attachment_name).assign(file)
|
67
|
+
subject.valid?
|
68
|
+
subject.errors[:"#{@attachment_name}_content_type"].blank?
|
69
|
+
end
|
70
|
+
|
71
|
+
def allowed_types_allowed?
|
72
|
+
@allowed_types.all? { |type| type_allowed?(type) }
|
73
|
+
end
|
74
|
+
|
75
|
+
def rejected_types_rejected?
|
76
|
+
!@rejected_types.any? { |type| type_allowed?(type) }
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
module Paperclip
|
2
|
+
module Shoulda
|
3
|
+
module Matchers
|
4
|
+
# Ensures that the given instance or class validates the presence of the
|
5
|
+
# given attachment.
|
6
|
+
#
|
7
|
+
# describe User do
|
8
|
+
# it { should validate_attachment_presence(:avatar) }
|
9
|
+
# end
|
10
|
+
def validate_attachment_presence name
|
11
|
+
ValidateAttachmentPresenceMatcher.new(name)
|
12
|
+
end
|
13
|
+
|
14
|
+
class ValidateAttachmentPresenceMatcher
|
15
|
+
def initialize attachment_name
|
16
|
+
@attachment_name = attachment_name
|
17
|
+
end
|
18
|
+
|
19
|
+
def matches? subject
|
20
|
+
@subject = subject
|
21
|
+
@subject = @subject.class unless Class === @subject
|
22
|
+
error_when_not_valid? && no_error_when_valid?
|
23
|
+
end
|
24
|
+
|
25
|
+
def failure_message
|
26
|
+
"Attachment #{@attachment_name} should be required"
|
27
|
+
end
|
28
|
+
|
29
|
+
def negative_failure_message
|
30
|
+
"Attachment #{@attachment_name} should not be required"
|
31
|
+
end
|
32
|
+
|
33
|
+
def description
|
34
|
+
"require presence of attachment #{@attachment_name}"
|
35
|
+
end
|
36
|
+
|
37
|
+
protected
|
38
|
+
|
39
|
+
def error_when_not_valid?
|
40
|
+
(subject = @subject.new).send(@attachment_name).assign(nil)
|
41
|
+
subject.valid?
|
42
|
+
not subject.errors[:"#{@attachment_name}_file_name"].blank?
|
43
|
+
end
|
44
|
+
|
45
|
+
def no_error_when_valid?
|
46
|
+
@file = StringIO.new(".")
|
47
|
+
(subject = @subject.new).send(@attachment_name).assign(@file)
|
48
|
+
subject.valid?
|
49
|
+
subject.errors[:"#{@attachment_name}_file_name"].blank?
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,95 @@
|
|
1
|
+
module Paperclip
|
2
|
+
module Shoulda
|
3
|
+
module Matchers
|
4
|
+
# Ensures that the given instance or class validates the size of the
|
5
|
+
# given attachment as specified.
|
6
|
+
#
|
7
|
+
# Examples:
|
8
|
+
# it { should validate_attachment_size(:avatar).
|
9
|
+
# less_than(2.megabytes) }
|
10
|
+
# it { should validate_attachment_size(:icon).
|
11
|
+
# greater_than(1024) }
|
12
|
+
# it { should validate_attachment_size(:icon).
|
13
|
+
# in(0..100) }
|
14
|
+
def validate_attachment_size name
|
15
|
+
ValidateAttachmentSizeMatcher.new(name)
|
16
|
+
end
|
17
|
+
|
18
|
+
class ValidateAttachmentSizeMatcher
|
19
|
+
def initialize attachment_name
|
20
|
+
@attachment_name = attachment_name
|
21
|
+
@low, @high = 0, (1.0/0)
|
22
|
+
end
|
23
|
+
|
24
|
+
def less_than size
|
25
|
+
@high = size
|
26
|
+
self
|
27
|
+
end
|
28
|
+
|
29
|
+
def greater_than size
|
30
|
+
@low = size
|
31
|
+
self
|
32
|
+
end
|
33
|
+
|
34
|
+
def in range
|
35
|
+
@low, @high = range.first, range.last
|
36
|
+
self
|
37
|
+
end
|
38
|
+
|
39
|
+
def matches? subject
|
40
|
+
@subject = subject
|
41
|
+
@subject = @subject.class unless Class === @subject
|
42
|
+
lower_than_low? && higher_than_low? && lower_than_high? && higher_than_high?
|
43
|
+
end
|
44
|
+
|
45
|
+
def failure_message
|
46
|
+
"Attachment #{@attachment_name} must be between #{@low} and #{@high} bytes"
|
47
|
+
end
|
48
|
+
|
49
|
+
def negative_failure_message
|
50
|
+
"Attachment #{@attachment_name} cannot be between #{@low} and #{@high} bytes"
|
51
|
+
end
|
52
|
+
|
53
|
+
def description
|
54
|
+
"validate the size of attachment #{@attachment_name}"
|
55
|
+
end
|
56
|
+
|
57
|
+
protected
|
58
|
+
|
59
|
+
def override_method object, method, &replacement
|
60
|
+
(class << object; self; end).class_eval do
|
61
|
+
define_method(method, &replacement)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def passes_validation_with_size(new_size)
|
66
|
+
file = StringIO.new(".")
|
67
|
+
override_method(file, :size){ new_size }
|
68
|
+
override_method(file, :to_tempfile){ file }
|
69
|
+
|
70
|
+
(subject = @subject.new).send(@attachment_name).assign(file)
|
71
|
+
subject.valid?
|
72
|
+
subject.errors[:"#{@attachment_name}_file_size"].blank?
|
73
|
+
end
|
74
|
+
|
75
|
+
def lower_than_low?
|
76
|
+
not passes_validation_with_size(@low - 1)
|
77
|
+
end
|
78
|
+
|
79
|
+
def higher_than_low?
|
80
|
+
passes_validation_with_size(@low + 1)
|
81
|
+
end
|
82
|
+
|
83
|
+
def lower_than_high?
|
84
|
+
return true if @high == (1.0/0)
|
85
|
+
passes_validation_with_size(@high - 1)
|
86
|
+
end
|
87
|
+
|
88
|
+
def higher_than_high?
|
89
|
+
return true if @high == (1.0/0)
|
90
|
+
not passes_validation_with_size(@high + 1)
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
@@ -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,78 @@
|
|
1
|
+
module Paperclip
|
2
|
+
class Options
|
3
|
+
|
4
|
+
attr_accessor :url, :path, :only_process, :normalized_styles, :default_url, :default_style,
|
5
|
+
:storage, :use_timestamp, :whiny, :use_default_time_zone, :hash_digest, :hash_secret,
|
6
|
+
:convert_options, :source_file_options, :preserve_files, :http_proxy
|
7
|
+
|
8
|
+
attr_accessor :s3_credentials, :s3_host_name, :s3_options, :s3_permissions, :s3_protocol,
|
9
|
+
:s3_headers, :s3_host_alias, :bucket
|
10
|
+
|
11
|
+
attr_accessor :fog_directory, :fog_credentials, :fog_host, :fog_public, :fog_file
|
12
|
+
|
13
|
+
def initialize(attachment, hash)
|
14
|
+
@attachment = attachment
|
15
|
+
|
16
|
+
@url = hash[:url]
|
17
|
+
@url = @url.call(@attachment) if @url.is_a?(Proc)
|
18
|
+
@path = hash[:path]
|
19
|
+
@path = @path.call(@attachment) if @path.is_a?(Proc)
|
20
|
+
@styles = hash[:styles]
|
21
|
+
@only_process = hash[:only_process]
|
22
|
+
@normalized_styles = nil
|
23
|
+
@default_url = hash[:default_url]
|
24
|
+
@default_style = hash[:default_style]
|
25
|
+
@storage = hash[:storage]
|
26
|
+
@use_timestamp = hash[:use_timestamp]
|
27
|
+
@whiny = hash[:whiny_thumbnails] || hash[:whiny]
|
28
|
+
@use_default_time_zone = hash[:use_default_time_zone]
|
29
|
+
@hash_digest = hash[:hash_digest]
|
30
|
+
@hash_data = hash[:hash_data]
|
31
|
+
@hash_secret = hash[:hash_secret]
|
32
|
+
@convert_options = hash[:convert_options]
|
33
|
+
@source_file_options = hash[:source_file_options]
|
34
|
+
@processors = hash[:processors]
|
35
|
+
@preserve_files = hash[:preserve_files]
|
36
|
+
@http_proxy = hash[:http_proxy]
|
37
|
+
|
38
|
+
#s3 options
|
39
|
+
@s3_credentials = hash[:s3_credentials]
|
40
|
+
@s3_host_name = hash[:s3_host_name]
|
41
|
+
@bucket = hash[:bucket]
|
42
|
+
@s3_options = hash[:s3_options]
|
43
|
+
@s3_permissions = hash[:s3_permissions]
|
44
|
+
@s3_protocol = hash[:s3_protocol]
|
45
|
+
@s3_headers = hash[:s3_headers]
|
46
|
+
@s3_host_alias = hash[:s3_host_alias]
|
47
|
+
|
48
|
+
#fog options
|
49
|
+
@fog_directory = hash[:fog_directory]
|
50
|
+
@fog_credentials = hash[:fog_credentials]
|
51
|
+
@fog_host = hash[:fog_host]
|
52
|
+
@fog_public = hash[:fog_public]
|
53
|
+
@fog_file = hash[:fog_file]
|
54
|
+
end
|
55
|
+
|
56
|
+
def method_missing(method, *args, &blk)
|
57
|
+
if method.to_s[-1,1] == "="
|
58
|
+
instance_variable_set("@#{method[0..-2]}", args[0])
|
59
|
+
else
|
60
|
+
instance_variable_get("@#{method}")
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def processors
|
65
|
+
@processors.respond_to?(:call) ? @processors.call(@attachment.instance) : @processors
|
66
|
+
end
|
67
|
+
|
68
|
+
def styles
|
69
|
+
if @styles.respond_to?(:call) || !@normalized_styles
|
70
|
+
@normalized_styles = ActiveSupport::OrderedHash.new
|
71
|
+
(@styles.respond_to?(:call) ? @styles.call(@attachment) : @styles).each do |name, args|
|
72
|
+
normalized_styles[name] = Paperclip::Style.new(name, args.dup, @attachment)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
@normalized_styles
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|