mender_paperclip 2.4.3
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/LICENSE +26 -0
- data/README.md +402 -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/attachment.rb +454 -0
- data/lib/paperclip/callback_compatibility.rb +61 -0
- data/lib/paperclip/geometry.rb +120 -0
- data/lib/paperclip/interpolations.rb +181 -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 +33 -0
- data/lib/paperclip/missing_attachment_styles.rb +87 -0
- data/lib/paperclip/options.rb +79 -0
- data/lib/paperclip/processor.rb +58 -0
- data/lib/paperclip/railtie.rb +26 -0
- data/lib/paperclip/storage/filesystem.rb +81 -0
- data/lib/paperclip/storage/fog.rb +162 -0
- data/lib/paperclip/storage/s3.rb +262 -0
- data/lib/paperclip/storage.rb +3 -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/paperclip.rb +478 -0
- data/lib/tasks/paperclip.rake +97 -0
- data/rails/init.rb +2 -0
- data/shoulda_macros/paperclip.rb +124 -0
- data/test/attachment_test.rb +1120 -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/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 +191 -0
- data/test/geometry_test.rb +206 -0
- data/test/helper.rb +152 -0
- data/test/integration_test.rb +654 -0
- data/test/interpolations_test.rb +195 -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 +68 -0
- data/test/paperclip_missing_attachment_styles_test.rb +80 -0
- data/test/paperclip_test.rb +329 -0
- data/test/processor_test.rb +10 -0
- data/test/storage/filesystem_test.rb +52 -0
- data/test/storage/s3_live_test.rb +51 -0
- data/test/storage/s3_test.rb +633 -0
- data/test/style_test.rb +180 -0
- data/test/thumbnail_test.rb +383 -0
- data/test/upfile_test.rb +53 -0
- metadata +243 -0
@@ -0,0 +1,454 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'uri'
|
3
|
+
|
4
|
+
module Paperclip
|
5
|
+
# The Attachment class manages the files for a given attachment. It saves
|
6
|
+
# when the model saves, deletes when the model is destroyed, and processes
|
7
|
+
# the file upon assignment.
|
8
|
+
class Attachment
|
9
|
+
include IOStream
|
10
|
+
|
11
|
+
def self.default_options
|
12
|
+
@default_options ||= {
|
13
|
+
:url => "/system/:attachment/:id/:style/:filename",
|
14
|
+
:path => ":rails_root/public:url",
|
15
|
+
:styles => {},
|
16
|
+
:only_process => [],
|
17
|
+
:processors => [:thumbnail],
|
18
|
+
:convert_options => {},
|
19
|
+
:source_file_options => {},
|
20
|
+
:default_url => "/:attachment/:style/missing.png",
|
21
|
+
:default_style => :original,
|
22
|
+
:storage => :filesystem,
|
23
|
+
:use_timestamp => true,
|
24
|
+
:whiny => Paperclip.options[:whiny] || Paperclip.options[:whiny_thumbnails],
|
25
|
+
:use_default_time_zone => true,
|
26
|
+
:hash_digest => "SHA1",
|
27
|
+
:hash_data => ":class/:attachment/:id/:style/:updated_at",
|
28
|
+
:preserve_files => false
|
29
|
+
}
|
30
|
+
end
|
31
|
+
|
32
|
+
attr_reader :name, :instance, :default_style, :convert_options, :queued_for_write, :whiny, :options, :interpolator
|
33
|
+
attr_accessor :post_processing
|
34
|
+
|
35
|
+
# Creates an Attachment object. +name+ is the name of the attachment,
|
36
|
+
# +instance+ is the ActiveRecord object instance it's attached to, and
|
37
|
+
# +options+ is the same as the hash passed to +has_attached_file+.
|
38
|
+
#
|
39
|
+
# Options include:
|
40
|
+
#
|
41
|
+
# +url+ - a relative URL of the attachment. This is interpolated using +interpolator+
|
42
|
+
# +path+ - where on the filesystem to store the attachment. This is interpolated using +interpolator+
|
43
|
+
# +styles+ - a hash of options for processing the attachment. See +has_attached_file+ for the details
|
44
|
+
# +only_process+ - style args to be run through the post-processor. This defaults to the empty list
|
45
|
+
# +default_url+ - a URL for the missing image
|
46
|
+
# +default_style+ - the style to use when don't specify an argument to e.g. #url, #path
|
47
|
+
# +storage+ - the storage mechanism. Defaults to :filesystem
|
48
|
+
# +use_timestamp+ - whether to append an anti-caching timestamp to image URLs. Defaults to true
|
49
|
+
# +whiny+, +whiny_thumbnails+ - whether to raise when thumbnailing fails
|
50
|
+
# +use_default_time_zone+ - related to +use_timestamp+. Defaults to true
|
51
|
+
# +hash_digest+ - a string representing a class that will be used to hash URLs for obfuscation
|
52
|
+
# +hash_data+ - the relative URL for the hash data. This is interpolated using +interpolator+
|
53
|
+
# +hash_secret+ - a secret passed to the +hash_digest+
|
54
|
+
# +convert_options+ - flags passed to the +convert+ command for processing
|
55
|
+
# +source_file_options+ - flags passed to the +convert+ command that controls how the file is read
|
56
|
+
# +processors+ - classes that transform the attachment. Defaults to [:thumbnail]
|
57
|
+
# +preserve_files+ - whether to keep files on the filesystem when deleting to clearing the attachment. Defaults to false
|
58
|
+
# +interpolator+ - the object used to interpolate filenames and URLs. Defaults to Paperclip::Interpolations
|
59
|
+
def initialize name, instance, options = {}
|
60
|
+
@name = name
|
61
|
+
@instance = instance
|
62
|
+
|
63
|
+
options = self.class.default_options.merge(options)
|
64
|
+
|
65
|
+
@options = Paperclip::Options.new(self, options)
|
66
|
+
@post_processing = true
|
67
|
+
@queued_for_delete = []
|
68
|
+
@queued_for_write = {}
|
69
|
+
@errors = {}
|
70
|
+
@dirty = false
|
71
|
+
@interpolator = (options[:interpolator] || Paperclip::Interpolations)
|
72
|
+
|
73
|
+
initialize_storage
|
74
|
+
end
|
75
|
+
|
76
|
+
# [:url, :path, :only_process, :normalized_styles, :default_url, :default_style,
|
77
|
+
# :storage, :use_timestamp, :whiny, :use_default_time_zone, :hash_digest, :hash_secret,
|
78
|
+
# :convert_options, :preserve_files].each do |field|
|
79
|
+
# define_method field do
|
80
|
+
# @options.send(field)
|
81
|
+
# end
|
82
|
+
# end
|
83
|
+
|
84
|
+
# What gets called when you call instance.attachment = File. It clears
|
85
|
+
# errors, assigns attributes, and processes the file. It
|
86
|
+
# also queues up the previous file for deletion, to be flushed away on
|
87
|
+
# #save of its host. In addition to form uploads, you can also assign
|
88
|
+
# another Paperclip attachment:
|
89
|
+
# new_user.avatar = old_user.avatar
|
90
|
+
def assign uploaded_file
|
91
|
+
ensure_required_accessors!
|
92
|
+
|
93
|
+
if uploaded_file.is_a?(Paperclip::Attachment)
|
94
|
+
uploaded_filename = uploaded_file.original_filename
|
95
|
+
uploaded_file = uploaded_file.to_file(:original)
|
96
|
+
close_uploaded_file = uploaded_file.respond_to?(:close)
|
97
|
+
end
|
98
|
+
|
99
|
+
return nil unless valid_assignment?(uploaded_file)
|
100
|
+
|
101
|
+
uploaded_file.binmode if uploaded_file.respond_to? :binmode
|
102
|
+
self.clear
|
103
|
+
|
104
|
+
return nil if uploaded_file.nil?
|
105
|
+
|
106
|
+
uploaded_filename ||= uploaded_file.original_filename
|
107
|
+
uploaded_content_type = extract_content_type(uploaded_file)
|
108
|
+
uploaded_filename = rewrite_extension(uploaded_filename, uploaded_content_type)
|
109
|
+
@queued_for_write[:original] = to_tempfile(uploaded_file)
|
110
|
+
instance_write(:file_name, uploaded_filename.strip)
|
111
|
+
instance_write(:content_type, uploaded_content_type)
|
112
|
+
instance_write(:file_size, uploaded_file.size.to_i)
|
113
|
+
instance_write(:fingerprint, generate_fingerprint(uploaded_file))
|
114
|
+
instance_write(:updated_at, Time.now)
|
115
|
+
|
116
|
+
@dirty = true
|
117
|
+
|
118
|
+
post_process(*@options.only_process) if post_processing
|
119
|
+
|
120
|
+
# Reset the file size if the original file was reprocessed.
|
121
|
+
instance_write(:file_size, @queued_for_write[:original].size.to_i)
|
122
|
+
instance_write(:fingerprint, generate_fingerprint(@queued_for_write[:original]))
|
123
|
+
ensure
|
124
|
+
uploaded_file.close if close_uploaded_file
|
125
|
+
end
|
126
|
+
|
127
|
+
# Returns the public URL of the attachment, with a given style. Note that
|
128
|
+
# this does not necessarily need to point to a file that your web server
|
129
|
+
# can access and can point to an action in your app, if you need fine
|
130
|
+
# grained security. This is not recommended if you don't need the
|
131
|
+
# security, however, for performance reasons. Set use_timestamp to false
|
132
|
+
# if you want to stop the attachment update time appended to the url
|
133
|
+
def url(style_name = default_style, use_timestamp = @options.use_timestamp)
|
134
|
+
default_url = @options.default_url.is_a?(Proc) ? @options.default_url.call(self) : @options.default_url
|
135
|
+
url = original_filename.nil? ? interpolate(default_url, style_name) : interpolate(@options.url, style_name)
|
136
|
+
URI.escape(use_timestamp && updated_at ? [url, updated_at].compact.join(url.include?("?") ? "&" : "?") : url)
|
137
|
+
end
|
138
|
+
|
139
|
+
# Returns the path of the attachment as defined by the :path option. If the
|
140
|
+
# file is stored in the filesystem the path refers to the path of the file
|
141
|
+
# on disk. If the file is stored in S3, the path is the "key" part of the
|
142
|
+
# URL, and the :bucket option refers to the S3 bucket.
|
143
|
+
def path(style_name = default_style)
|
144
|
+
original_filename.nil? ? nil : interpolate(@options.path, style_name)
|
145
|
+
end
|
146
|
+
|
147
|
+
# Alias to +url+
|
148
|
+
def to_s style_name = default_style
|
149
|
+
url(style_name)
|
150
|
+
end
|
151
|
+
|
152
|
+
def default_style
|
153
|
+
@options.default_style
|
154
|
+
end
|
155
|
+
|
156
|
+
def styles
|
157
|
+
@options.styles
|
158
|
+
end
|
159
|
+
|
160
|
+
# Returns an array containing the errors on this attachment.
|
161
|
+
def errors
|
162
|
+
@errors
|
163
|
+
end
|
164
|
+
|
165
|
+
# Returns true if there are changes that need to be saved.
|
166
|
+
def dirty?
|
167
|
+
@dirty
|
168
|
+
end
|
169
|
+
|
170
|
+
# Saves the file, if there are no errors. If there are, it flushes them to
|
171
|
+
# the instance's errors and returns false, cancelling the save.
|
172
|
+
def save
|
173
|
+
flush_deletes
|
174
|
+
flush_writes
|
175
|
+
@dirty = false
|
176
|
+
true
|
177
|
+
end
|
178
|
+
|
179
|
+
# Clears out the attachment. Has the same effect as previously assigning
|
180
|
+
# nil to the attachment. Does NOT save. If you wish to clear AND save,
|
181
|
+
# use #destroy.
|
182
|
+
def clear
|
183
|
+
queue_existing_for_delete
|
184
|
+
@queued_for_write = {}
|
185
|
+
@errors = {}
|
186
|
+
end
|
187
|
+
|
188
|
+
# Destroys the attachment. Has the same effect as previously assigning
|
189
|
+
# nil to the attachment *and saving*. This is permanent. If you wish to
|
190
|
+
# wipe out the existing attachment but not save, use #clear.
|
191
|
+
def destroy
|
192
|
+
unless @options.preserve_files
|
193
|
+
clear
|
194
|
+
save
|
195
|
+
end
|
196
|
+
end
|
197
|
+
|
198
|
+
# Returns the name of the file as originally assigned, and lives in the
|
199
|
+
# <attachment>_file_name attribute of the model.
|
200
|
+
def original_filename
|
201
|
+
instance_read(:file_name)
|
202
|
+
end
|
203
|
+
|
204
|
+
# Returns the size of the file as originally assigned, and lives in the
|
205
|
+
# <attachment>_file_size attribute of the model.
|
206
|
+
def size
|
207
|
+
instance_read(:file_size) || (@queued_for_write[:original] && @queued_for_write[:original].size)
|
208
|
+
end
|
209
|
+
|
210
|
+
# Returns the hash of the file as originally assigned, and lives in the
|
211
|
+
# <attachment>_fingerprint attribute of the model.
|
212
|
+
def fingerprint
|
213
|
+
instance_read(:fingerprint) || (@queued_for_write[:original] && generate_fingerprint(@queued_for_write[:original]))
|
214
|
+
end
|
215
|
+
|
216
|
+
# Returns the content_type of the file as originally assigned, and lives
|
217
|
+
# in the <attachment>_content_type attribute of the model.
|
218
|
+
def content_type
|
219
|
+
instance_read(:content_type)
|
220
|
+
end
|
221
|
+
|
222
|
+
# Returns the last modified time of the file as originally assigned, and
|
223
|
+
# lives in the <attachment>_updated_at attribute of the model.
|
224
|
+
def updated_at
|
225
|
+
time = instance_read(:updated_at)
|
226
|
+
time && time.to_f.to_i
|
227
|
+
end
|
228
|
+
|
229
|
+
# The time zone to use for timestamp interpolation. Using the default
|
230
|
+
# time zone ensures that results are consistent across all threads.
|
231
|
+
def time_zone
|
232
|
+
@options.use_default_time_zone ? Time.zone_default : Time.zone
|
233
|
+
end
|
234
|
+
|
235
|
+
# Returns a unique hash suitable for obfuscating the URL of an otherwise
|
236
|
+
# publicly viewable attachment.
|
237
|
+
def hash(style_name = default_style)
|
238
|
+
raise ArgumentError, "Unable to generate hash without :hash_secret" unless @options.hash_secret
|
239
|
+
require 'openssl' unless defined?(OpenSSL)
|
240
|
+
data = interpolate(@options.hash_data, style_name)
|
241
|
+
OpenSSL::HMAC.hexdigest(OpenSSL::Digest.const_get(@options.hash_digest).new, @options.hash_secret, data)
|
242
|
+
end
|
243
|
+
|
244
|
+
def generate_fingerprint(source)
|
245
|
+
if source.respond_to?(:path) && source.path && !source.path.blank?
|
246
|
+
Digest::MD5.file(source.path).to_s
|
247
|
+
else
|
248
|
+
data = source.read
|
249
|
+
source.rewind if source.respond_to?(:rewind)
|
250
|
+
Digest::MD5.hexdigest(data)
|
251
|
+
end
|
252
|
+
end
|
253
|
+
|
254
|
+
def extract_content_type(source)
|
255
|
+
if @options.use_file_command && source.respond_to?(:type_from_file_command)
|
256
|
+
source.type_from_file_command
|
257
|
+
elsif source.respond_to?(:content_type)
|
258
|
+
source.content_type
|
259
|
+
else
|
260
|
+
nil
|
261
|
+
end
|
262
|
+
end
|
263
|
+
|
264
|
+
# Paths and URLs can have a number of variables interpolated into them
|
265
|
+
# to vary the storage location based on name, id, style, class, etc.
|
266
|
+
# This method is a deprecated access into supplying and retrieving these
|
267
|
+
# interpolations. Future access should use either Paperclip.interpolates
|
268
|
+
# or extend the Paperclip::Interpolations module directly.
|
269
|
+
def self.interpolations
|
270
|
+
warn('[DEPRECATION] Paperclip::Attachment.interpolations is deprecated ' +
|
271
|
+
'and will be removed from future versions. ' +
|
272
|
+
'Use Paperclip.interpolates instead')
|
273
|
+
Paperclip::Interpolations
|
274
|
+
end
|
275
|
+
|
276
|
+
# This method really shouldn't be called that often. It's expected use is
|
277
|
+
# in the paperclip:refresh rake task and that's it. It will regenerate all
|
278
|
+
# thumbnails forcefully, by reobtaining the original file and going through
|
279
|
+
# the post-process again.
|
280
|
+
def reprocess!(*style_args)
|
281
|
+
new_original = Tempfile.new("paperclip-reprocess")
|
282
|
+
new_original.binmode
|
283
|
+
if old_original = to_file(:original)
|
284
|
+
new_original.write( old_original.respond_to?(:get) ? old_original.get : old_original.read )
|
285
|
+
new_original.rewind
|
286
|
+
|
287
|
+
@queued_for_write = { :original => new_original }
|
288
|
+
instance_write(:updated_at, Time.now)
|
289
|
+
post_process(*style_args)
|
290
|
+
|
291
|
+
old_original.close if old_original.respond_to?(:close)
|
292
|
+
old_original.unlink if old_original.respond_to?(:unlink)
|
293
|
+
|
294
|
+
save
|
295
|
+
else
|
296
|
+
true
|
297
|
+
end
|
298
|
+
rescue Errno::EACCES => e
|
299
|
+
warn "#{e} - skipping file"
|
300
|
+
false
|
301
|
+
end
|
302
|
+
|
303
|
+
# Returns true if a file has been assigned.
|
304
|
+
def file?
|
305
|
+
!original_filename.blank?
|
306
|
+
end
|
307
|
+
|
308
|
+
alias :present? :file?
|
309
|
+
|
310
|
+
# Writes the attachment-specific attribute on the instance. For example,
|
311
|
+
# instance_write(:file_name, "me.jpg") will write "me.jpg" to the instance's
|
312
|
+
# "avatar_file_name" field (assuming the attachment is called avatar).
|
313
|
+
def instance_write(attr, value)
|
314
|
+
setter = :"#{name}_#{attr}="
|
315
|
+
responds = instance.respond_to?(setter)
|
316
|
+
self.instance_variable_set("@_#{setter.to_s.chop}", value)
|
317
|
+
instance.send(setter, value) if responds || attr.to_s == "file_name"
|
318
|
+
end
|
319
|
+
|
320
|
+
# Reads the attachment-specific attribute on the instance. See instance_write
|
321
|
+
# for more details.
|
322
|
+
def instance_read(attr)
|
323
|
+
getter = :"#{name}_#{attr}"
|
324
|
+
responds = instance.respond_to?(getter)
|
325
|
+
cached = self.instance_variable_get("@_#{getter}")
|
326
|
+
return cached if cached
|
327
|
+
instance.send(getter) if responds || attr.to_s == "file_name"
|
328
|
+
end
|
329
|
+
|
330
|
+
private
|
331
|
+
|
332
|
+
def rewrite_extension(original_filename, content_type)
|
333
|
+
original_extension = File.extname(original_filename)
|
334
|
+
original_basename = File.basename(original_filename, original_extension)
|
335
|
+
original_extension = original_extension.sub(/^\.+/, '')
|
336
|
+
|
337
|
+
mime_type = MIME::Types[content_type]
|
338
|
+
extensions = mime_type.empty? ? [] : mime_type.first.extensions
|
339
|
+
extension = if extensions.include?(original_extension)
|
340
|
+
original_extension
|
341
|
+
elsif extensions.present?
|
342
|
+
extensions.first
|
343
|
+
else
|
344
|
+
%r{/([^/]*)$}.match(content_type)[1]
|
345
|
+
end
|
346
|
+
puts "#{original_filename} #{content_type} #{extension}"
|
347
|
+
|
348
|
+
if extension.present? && original_extension != extension
|
349
|
+
original_basename + '.' + extension
|
350
|
+
else
|
351
|
+
original_filename
|
352
|
+
end
|
353
|
+
end
|
354
|
+
|
355
|
+
def ensure_required_accessors! #:nodoc:
|
356
|
+
%w(file_name).each do |field|
|
357
|
+
unless @instance.respond_to?("#{name}_#{field}") && @instance.respond_to?("#{name}_#{field}=")
|
358
|
+
raise PaperclipError.new("#{@instance.class} model missing required attr_accessor for '#{name}_#{field}'")
|
359
|
+
end
|
360
|
+
end
|
361
|
+
end
|
362
|
+
|
363
|
+
def log message #:nodoc:
|
364
|
+
Paperclip.log(message)
|
365
|
+
end
|
366
|
+
|
367
|
+
def valid_assignment? file #:nodoc:
|
368
|
+
file.nil? || (file.respond_to?(:original_filename) && file.respond_to?(:content_type))
|
369
|
+
end
|
370
|
+
|
371
|
+
def initialize_storage #:nodoc:
|
372
|
+
storage_class_name = @options.storage.to_s.downcase.camelize
|
373
|
+
begin
|
374
|
+
storage_module = Paperclip::Storage.const_get(storage_class_name)
|
375
|
+
rescue NameError
|
376
|
+
raise StorageMethodNotFound, "Cannot load storage module '#{storage_class_name}'"
|
377
|
+
end
|
378
|
+
self.extend(storage_module)
|
379
|
+
end
|
380
|
+
|
381
|
+
def extra_options_for(style) #:nodoc:
|
382
|
+
all_options = @options.convert_options[:all]
|
383
|
+
all_options = all_options.call(instance) if all_options.respond_to?(:call)
|
384
|
+
style_options = @options.convert_options[style]
|
385
|
+
style_options = style_options.call(instance) if style_options.respond_to?(:call)
|
386
|
+
|
387
|
+
[ style_options, all_options ].compact.join(" ")
|
388
|
+
end
|
389
|
+
|
390
|
+
def extra_source_file_options_for(style) #:nodoc:
|
391
|
+
all_options = @options.source_file_options[:all]
|
392
|
+
all_options = all_options.call(instance) if all_options.respond_to?(:call)
|
393
|
+
style_options = @options.source_file_options[style]
|
394
|
+
style_options = style_options.call(instance) if style_options.respond_to?(:call)
|
395
|
+
|
396
|
+
[ style_options, all_options ].compact.join(" ")
|
397
|
+
end
|
398
|
+
|
399
|
+
def post_process(*style_args) #:nodoc:
|
400
|
+
return if @queued_for_write[:original].nil?
|
401
|
+
instance.run_paperclip_callbacks(:post_process) do
|
402
|
+
instance.run_paperclip_callbacks(:"#{name}_post_process") do
|
403
|
+
post_process_styles(*style_args)
|
404
|
+
end
|
405
|
+
end
|
406
|
+
end
|
407
|
+
|
408
|
+
def post_process_styles(*style_args) #:nodoc:
|
409
|
+
@options.styles.each do |name, style|
|
410
|
+
begin
|
411
|
+
if style_args.empty? || style_args.include?(name)
|
412
|
+
raise RuntimeError.new("Style #{name} has no processors defined.") if style.processors.blank?
|
413
|
+
@queued_for_write[name] = style.processors.inject(@queued_for_write[:original]) do |file, processor|
|
414
|
+
Paperclip.processor(processor).make(file, style.processor_options, self)
|
415
|
+
end
|
416
|
+
end
|
417
|
+
rescue PaperclipError => e
|
418
|
+
log("An error was received while processing: #{e.inspect}")
|
419
|
+
(@errors[:processing] ||= []) << e.message if @options.whiny
|
420
|
+
end
|
421
|
+
end
|
422
|
+
end
|
423
|
+
|
424
|
+
def interpolate(pattern, style_name = default_style) #:nodoc:
|
425
|
+
interpolator.interpolate(pattern, self, style_name)
|
426
|
+
end
|
427
|
+
|
428
|
+
def queue_existing_for_delete #:nodoc:
|
429
|
+
return if @options.preserve_files || !file?
|
430
|
+
@queued_for_delete += [:original, *@options.styles.keys].uniq.map do |style|
|
431
|
+
path(style) if exists?(style)
|
432
|
+
end.compact
|
433
|
+
instance_write(:file_name, nil)
|
434
|
+
instance_write(:content_type, nil)
|
435
|
+
instance_write(:file_size, nil)
|
436
|
+
instance_write(:updated_at, nil)
|
437
|
+
end
|
438
|
+
|
439
|
+
def flush_errors #:nodoc:
|
440
|
+
@errors.each do |error, message|
|
441
|
+
[message].flatten.each {|m| instance.errors.add(name, m) }
|
442
|
+
end
|
443
|
+
end
|
444
|
+
|
445
|
+
# called by storage after the writes are flushed and before @queued_for_writes is cleared
|
446
|
+
def after_flush_writes
|
447
|
+
@queued_for_write.each do |style, file|
|
448
|
+
file.close unless file.closed?
|
449
|
+
file.unlink if file.respond_to?(:unlink) && file.path.present? && File.exist?(file.path)
|
450
|
+
end
|
451
|
+
end
|
452
|
+
|
453
|
+
end
|
454
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
module Paperclip
|
2
|
+
module CallbackCompatability
|
3
|
+
module Rails21
|
4
|
+
def self.included(base)
|
5
|
+
base.extend(Defining)
|
6
|
+
base.send(:include, Running)
|
7
|
+
end
|
8
|
+
|
9
|
+
module Defining
|
10
|
+
def define_paperclip_callbacks(*args)
|
11
|
+
args.each do |callback|
|
12
|
+
define_callbacks("before_#{callback}")
|
13
|
+
define_callbacks("after_#{callback}")
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
module Running
|
19
|
+
def run_paperclip_callbacks(callback, opts = nil, &blk)
|
20
|
+
# The overall structure of this isn't ideal since after callbacks run even if
|
21
|
+
# befores return false. But this is how rails 3's callbacks work, unfortunately.
|
22
|
+
if run_callbacks(:"before_#{callback}"){ |result, object| result == false } != false
|
23
|
+
blk.call
|
24
|
+
end
|
25
|
+
run_callbacks(:"after_#{callback}"){ |result, object| result == false }
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
module Rails3
|
31
|
+
def self.included(base)
|
32
|
+
base.extend(Defining)
|
33
|
+
base.send(:include, Running)
|
34
|
+
end
|
35
|
+
|
36
|
+
module Defining
|
37
|
+
def define_paperclip_callbacks(*callbacks)
|
38
|
+
define_callbacks *[callbacks, {:terminator => "result == false"}].flatten
|
39
|
+
callbacks.each do |callback|
|
40
|
+
eval <<-end_callbacks
|
41
|
+
def before_#{callback}(*args, &blk)
|
42
|
+
set_callback(:#{callback}, :before, *args, &blk)
|
43
|
+
end
|
44
|
+
def after_#{callback}(*args, &blk)
|
45
|
+
set_callback(:#{callback}, :after, *args, &blk)
|
46
|
+
end
|
47
|
+
end_callbacks
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
module Running
|
53
|
+
def run_paperclip_callbacks(callback, opts = nil, &block)
|
54
|
+
run_callbacks(callback, opts, &block)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,120 @@
|
|
1
|
+
module Paperclip
|
2
|
+
|
3
|
+
# Defines the geometry of an image.
|
4
|
+
class Geometry
|
5
|
+
attr_accessor :height, :width, :modifier
|
6
|
+
|
7
|
+
# Gives a Geometry representing the given height and width
|
8
|
+
def initialize width = nil, height = nil, modifier = nil
|
9
|
+
@height = height.to_f
|
10
|
+
@width = width.to_f
|
11
|
+
@modifier = modifier
|
12
|
+
end
|
13
|
+
|
14
|
+
# Uses ImageMagick to determing the dimensions of a file, passed in as either a
|
15
|
+
# File or path.
|
16
|
+
# NOTE: (race cond) Do not reassign the 'file' variable inside this method as it is likely to be
|
17
|
+
# a Tempfile object, which would be eligible for file deletion when no longer referenced.
|
18
|
+
def self.from_file file
|
19
|
+
file_path = file.respond_to?(:path) ? file.path : file
|
20
|
+
raise(Paperclip::NotIdentifiedByImageMagickError.new("Cannot find the geometry of a file with a blank name")) if file_path.blank?
|
21
|
+
geometry = begin
|
22
|
+
Paperclip.run("identify", "-format %wx%h :file", :file => "#{file_path}[0]")
|
23
|
+
rescue Cocaine::ExitStatusError
|
24
|
+
""
|
25
|
+
rescue Cocaine::CommandNotFoundError => e
|
26
|
+
raise Paperclip::CommandNotFoundError.new("Could not run the `identify` command. Please install ImageMagick.")
|
27
|
+
end
|
28
|
+
parse(geometry) ||
|
29
|
+
raise(NotIdentifiedByImageMagickError.new("#{file_path} is not recognized by the 'identify' command."))
|
30
|
+
end
|
31
|
+
|
32
|
+
# Parses a "WxH" formatted string, where W is the width and H is the height.
|
33
|
+
def self.parse string
|
34
|
+
if match = (string && string.match(/\b(\d*)x?(\d*)\b([\>\<\#\@\%^!])?/i))
|
35
|
+
Geometry.new(*match[1,3])
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
# True if the dimensions represent a square
|
40
|
+
def square?
|
41
|
+
height == width
|
42
|
+
end
|
43
|
+
|
44
|
+
# True if the dimensions represent a horizontal rectangle
|
45
|
+
def horizontal?
|
46
|
+
height < width
|
47
|
+
end
|
48
|
+
|
49
|
+
# True if the dimensions represent a vertical rectangle
|
50
|
+
def vertical?
|
51
|
+
height > width
|
52
|
+
end
|
53
|
+
|
54
|
+
# The aspect ratio of the dimensions.
|
55
|
+
def aspect
|
56
|
+
width / height
|
57
|
+
end
|
58
|
+
|
59
|
+
# Returns the larger of the two dimensions
|
60
|
+
def larger
|
61
|
+
[height, width].max
|
62
|
+
end
|
63
|
+
|
64
|
+
# Returns the smaller of the two dimensions
|
65
|
+
def smaller
|
66
|
+
[height, width].min
|
67
|
+
end
|
68
|
+
|
69
|
+
# Returns the width and height in a format suitable to be passed to Geometry.parse
|
70
|
+
def to_s
|
71
|
+
s = ""
|
72
|
+
s << width.to_i.to_s if width > 0
|
73
|
+
s << "x#{height.to_i}" if height > 0
|
74
|
+
s << modifier.to_s
|
75
|
+
s
|
76
|
+
end
|
77
|
+
|
78
|
+
# Same as to_s
|
79
|
+
def inspect
|
80
|
+
to_s
|
81
|
+
end
|
82
|
+
|
83
|
+
# Returns the scaling and cropping geometries (in string-based ImageMagick format)
|
84
|
+
# neccessary to transform this Geometry into the Geometry given. If crop is true,
|
85
|
+
# then it is assumed the destination Geometry will be the exact final resolution.
|
86
|
+
# In this case, the source Geometry is scaled so that an image containing the
|
87
|
+
# destination Geometry would be completely filled by the source image, and any
|
88
|
+
# overhanging image would be cropped. Useful for square thumbnail images. The cropping
|
89
|
+
# is weighted at the center of the Geometry.
|
90
|
+
def transformation_to dst, crop = false
|
91
|
+
if crop
|
92
|
+
ratio = Geometry.new( dst.width / self.width, dst.height / self.height )
|
93
|
+
scale_geometry, scale = scaling(dst, ratio)
|
94
|
+
crop_geometry = cropping(dst, ratio, scale)
|
95
|
+
else
|
96
|
+
scale_geometry = dst.to_s
|
97
|
+
end
|
98
|
+
|
99
|
+
[ scale_geometry, crop_geometry ]
|
100
|
+
end
|
101
|
+
|
102
|
+
private
|
103
|
+
|
104
|
+
def scaling dst, ratio
|
105
|
+
if ratio.horizontal? || ratio.square?
|
106
|
+
[ "%dx" % dst.width, ratio.width ]
|
107
|
+
else
|
108
|
+
[ "x%d" % dst.height, ratio.height ]
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
def cropping dst, ratio, scale
|
113
|
+
if ratio.horizontal? || ratio.square?
|
114
|
+
"%dx%d+%d+%d" % [ dst.width, dst.height, 0, (self.height * scale - dst.height) / 2 ]
|
115
|
+
else
|
116
|
+
"%dx%d+%d+%d" % [ dst.width, dst.height, (self.width * scale - dst.width) / 2, 0 ]
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|