This repository has been archived by the owner on Dec 29, 2021. It is now read-only.
forked from thoughtbot/paperclip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
processor.rb
58 lines (53 loc) · 2.07 KB
/
processor.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
module Paperclip
# Paperclip processors allow you to modify attached files when they are
# attached in any way you are able. Paperclip itself uses command-line
# programs for its included Thumbnail processor, but custom processors
# are not required to follow suit.
#
# Processors are required to be defined inside the Paperclip module and
# are also required to be a subclass of Paperclip::Processor. There is
# only one method you *must* implement to properly be a subclass:
# #make, but #initialize may also be of use. Both methods accept 3
# arguments: the file that will be operated on (which is an instance of
# File), a hash of options that were defined in has_attached_file's
# style hash, and the Paperclip::Attachment itself.
#
# All #make needs to return is an instance of File (Tempfile is
# acceptable) which contains the results of the processing.
#
# See Paperclip.run for more information about using command-line
# utilities from within Processors.
class Processor
attr_accessor :file, :options, :attachment
def initialize file, options = {}, attachment = nil
@file = file
@options = options
@attachment = attachment
end
def make
end
def self.make file, options = {}, attachment = nil
new(file, options, attachment).make
end
end
# Due to how ImageMagick handles its image format conversion and how Tempfile
# handles its naming scheme, it is necessary to override how Tempfile makes
# its names so as to allow for file extensions. Idea taken from the comments
# on this blog post:
# http://marsorange.com/archives/of-mogrify-ruby-tempfile-dynamic-class-definitions
class Tempfile < ::Tempfile
# This is Ruby 1.8.7's implementation.
if RUBY_VERSION <= "1.8.6"
def make_tmpname(basename, n)
case basename
when Array
prefix, suffix = *basename
else
prefix, suffix = basename, ''
end
t = Time.now.strftime("%y%m%d")
path = "#{prefix}#{t}-#{$$}-#{rand(0x100000000).to_s(36)}-#{n}#{suffix}"
end
end
end
end