-
Notifications
You must be signed in to change notification settings - Fork 25
/
image.rb
72 lines (60 loc) · 2.23 KB
/
image.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
require 'mini_magick'
module Hydra
module Derivatives
class Image < Processor
class_attribute :timeout
def process
timeout ? process_with_timeout : process_without_timeout
end
def process_with_timeout
status = Timeout::timeout(timeout) do
process_without_timeout
end
rescue Timeout::Error => ex
raise Hydra::Derivatives::TimeoutError, "Unable to process image derivative\nThe command took longer than #{timeout} seconds to execute"
end
def process_without_timeout
directives.each do |name, args|
opts = args.kind_of?(Hash) ? args : {size: args}
format = opts.fetch(:format, 'png')
destination_name = output_filename_for(name, opts)
create_resized_image(destination_name, opts[:size], format)
end
end
def output_filename_for(name, opts = {})
if opts.has_key? :datastream
Deprecation.warn Hydra::Derivatives::Image, 'The :datastream option is deprecated and will be removed in a future release.'
end
opts.fetch(:datastream, output_file_id(name))
end
protected
def new_mime_type(format)
MIME::Types.type_for(format).first.to_s
end
def create_resized_image(destination_name, size, format, quality=nil)
create_image(destination_name, format, quality) do |xfrm|
xfrm.resize(size) if size.present?
end
end
def create_image(destination_name, format, quality=nil)
xfrm = load_image_transformer
yield(xfrm) if block_given?
xfrm.format(format)
xfrm.quality(quality.to_s) if quality
write_image(destination_name, format, xfrm)
end
def write_image(destination_name, format, xfrm)
stream = StringIO.new
xfrm.write(stream)
stream.rewind
mime_type = new_mime_type(format)
output_file_service.call(object, stream, destination_name, mime_type: mime_type)
end
# Override this method if you want a different transformer, or need to load the
# raw image from a different source (e.g. external file)
def load_image_transformer
MiniMagick::Image.read(source_file.content)
end
end
end
end