/
patch.rb
217 lines (199 loc) · 8.55 KB
/
patch.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# AttachmentFuPatch
require 'fileutils'
module Technoweenie # :nodoc:
module AttachmentFu # :nodoc:
module ClassMethods
@@content_types = Image::VALID_MIME_TYPES
@@supported_raw_formats = ['cr2', 'crw', 'nef', 'dng', 'raf']
mattr_reader :content_types, :supported_raw_formats
def convert_if_necessary(file, filename, rotation)
ext = File.extname(filename) # on purpose not using file since tempfile may loose extension.
ext = ext[1...ext.size].downcase if !ext.blank?
if supported_raw_formats.include? ext
begin
# Log the failure to load the image. This should match ::Magick::ImageMagickError
# but that would cause acts_as_attachment to require rmagick.
if rotation.nil?
message = `ufraw-batch '#{file}' --overwrite '--out-path=#{Technoweenie::AttachmentFu.tempfile_path}'` # add --rotate=angle
else
message = `ufraw-batch '#{file}' --overwrite '--out-path=#{Technoweenie::AttachmentFu.tempfile_path}' --rotate=#{rotation}` # add --rotate=angle
end
base_name = "#{File.basename(file,'.*')}.ppm"
original_temp = File.expand_path(File.join(Technoweenie::AttachmentFu.tempfile_path, base_name))
raise "Format for #{self.filename} could not be processed: #{message}" unless File.exists?(original_temp)
new_temp = copy_to_temp_file(original_temp, "#{rand Time.now.to_i}_#{base_name}")
FileUtils::rm original_temp
return new_temp
rescue
logger.debug("Exception working with image: #{$!}")
return file
end
else
if rotation.nil?
return file
else
original_temp = File.expand_path(File.join(Technoweenie::AttachmentFu.tempfile_path, filename))
Magick::Image.read(file).first.rotate(rotation).write(original_temp)
raise "Could not rotate #{filename}: #{message}" unless File.exists?(original_temp)
new_temp = copy_to_temp_file(original_temp, "#{rand Time.now.to_i}_#{filename}")
FileUtils::rm original_temp
return new_temp
end
end
end
end
module InstanceMethods
# This method handles the uploaded file object. If you set the field name to uploaded_data, you don't need
# any special code in your controller.
#
# <% form_for :attachment, :html => { :multipart => true } do |f| -%>
# <p><%= f.file_field :uploaded_data %></p>
# <p><%= submit_tag :Save %>
# <% end -%>
#
# @attachment = Attachment.create! params[:attachment]
#
# TODO: Allow it to work with Merb tempfiles too.
alias_method :__uploaded_data=, :uploaded_data=
def uploaded_data=(file_data)
self.__uploaded_data=file_data
if self.content_type=='application/octet-stream'
self.content_type = Image::VALID_TYPES[FilenameUtils.extension_without_dot(self.filename).downcase]
end
end
def temp_path=(file_data)
self.temp_paths.unshift file_data
end
# Gets the thumbnail name for a filename. 'foo.jpg' becomes 'foo_thumbnail.jpg'
def thumbnail_name_for(thumbnail = nil)
return filename if thumbnail.blank?
"#{FilenameUtils.basename(filename)}_#{thumbnail}.jpg"
end
def convert_if_necessary(file, rotation=nil)
self.class.convert_if_necessary(file, self.filename, rotation)
end
def create_or_update_thumbnail(temp_file, file_name_suffix, size)
thumbnailable? || raise(ThumbnailError.new("Can't create a thumbnail if the content type is not an image or there is no parent_id column"))
find_or_initialize_thumbnail(file_name_suffix).tap do |thumb|
thumb.temp_paths.unshift temp_file
thumb.send(:assign_attributes, {
:content_type => content_type,
:filename => thumbnail_name_for(file_name_suffix),
:thumbnail_resize_options => size
}, :without_protection => true)
callback_with_args :before_thumbnail_saved, thumb
thumb.save!
end
end
def update_thumbnails(rotation = nil)
if respond_to?(:process_attachment_with_processing, true) && thumbnailable? && !attachment_options[:thumbnails].blank? && parent_id.nil?
temp_file = convert_if_necessary(temp_path || create_temp_file, rotation)
attachment_options[:thumbnails].each { |suffix, size|
if size.is_a?(Symbol)
parent_type = polymorphic_parent_type
next unless parent_type && [parent_type, parent_type.tableize].include?(suffix.to_s) && respond_to?(size)
size = send(size)
#end
#if size.is_a?(Hash)
# parent_type = polymorphic_parent_type
# next unless parent_type && [parent_type, parent_type.tableize].include?(suffix.to_s)
# size.each { |ppt_suffix, ppt_size|
# create_or_update_thumbnail(temp_file, ppt_suffix, *ppt_size)
# }
else
create_or_update_thumbnail(temp_file, suffix, size)
end
}
end
@temp_paths.clear
@saved_attachment = nil
end
# Cleans up after processing. Thumbnails are created, the attachment is stored to the backend, and the temp_paths are cleared.
def after_process_attachment
if @saved_attachment
save_to_storage
update_thumbnails
callback_with_args :after_attachment_saved, nil
end
end
def date_time_original
date_time_str = properties['exif:DateTimeOriginal']
date_time_str = properties['tiff:timestamp'] if date_time_str.nil?
if !date_time_str.blank?
date = nil
begin
date = DateTime.parse(date_time_str.split(":",3).join("-"))
rescue Exception => exc
return date_time_str
end
return date
end
return nil
end
def make
make_str = properties['exif:Make']
make_str = properties['tiff:make'] if make_str.nil?
return make_str
end
def model
model_str = properties['exif:Model']
model_str = properties['tiff:model'] if model_str.nil?
return model_str
end
private
def properties
@properties ||= nil
if @properties.nil?
with_image{|img| @properties = self.class.properties(img) }
@properties = {} if @properties.nil?
end
return @properties
end
end
module Processors
module RmagickProcessor
module ClassMethods
def properties(img)
img.get_exif_by_entry
return img.properties
end
end
protected
# Performs the actual resizing operation for a thumbnail
def resize_image(img, size_options)
if size_options.is_a?(Hash)
size = size_options[:geometry]
density = size_options[:density]
quality = size_options[:quality]
else
size = size_options
end
size = size.first if size.is_a?(Array) && size.length == 1 && !size.first.is_a?(Fixnum)
if size.is_a?(Fixnum) || (size.is_a?(Array) && size.first.is_a?(Fixnum))
size = [size, size] if size.is_a?(Fixnum)
img.thumbnail!(*size)
elsif size.is_a?(String) && size =~ /^c.*$/ # Image cropping - example geometry string: c75x75
dimensions = size[1..size.size].split("x")
img.crop_resized!(dimensions[0].to_i, dimensions[1].to_i)
else
img.change_geometry(size.to_s) { |cols, rows, image|
image.resize!(cols<1 ? 1 : cols, rows<1 ? 1 : rows)
}
end
self.width = img.columns if respond_to?(:width)
self.height = img.rows if respond_to?(:height)
img = img.sharpen if attachment_options[:sharpen_on_resize] && img.changed?
img.strip! unless attachment_options[:keep_profile]
quality = img.format.to_s[/JPEG/] && get_jpeg_quality if quality.nil?
data = img.to_blob do
self.quality = quality if !quality.nil?
self.density = density if !density.nil?
self.format = 'JPG'
end
self.temp_path = write_to_temp_file(data)
self.size = File.size(self.temp_path)
end
end
end
end
end