Class: Spraycan::Workers::Previews

Inherits:
Bottle::Foreman
  • Object
show all
Defined in:
lib/spraycan/workers/previews.rb

Instance Method Summary collapse

Instance Method Details

#get_image(image_path) ⇒ Object



39
40
41
42
43
# File 'lib/spraycan/workers/previews.rb', line 39

def get_image(image_path)
  Publisher::S3::Download.file_content(image_path)
rescue
  raise "Failed to download #{image_path} from S3"
end

#process(payload) ⇒ Object



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
# File 'lib/spraycan/workers/previews.rb', line 5

def process(payload)
  puts "Starting Previews#process. Payload - #{payload.inspect}\n"

  return failure("Image payload expected a hash, got #{payload.class.to_s}.") unless payload.is_a?(Hash)

  s3_path  = payload[:s3_path]
  previews = payload[:previews]

  return failure('s3_path must be specified') unless s3_path
  return failure('previews is not a hash') unless previews.is_a?(Hash)

  image = get_image(s3_path)

  response = Rmagick.new.previews(image, previews)

  sizes = {}
  response.each do |k, v|
    sizes[k] = [v[:width], v[:height]]

    next if k == :original
    save_to_cloud(response[k][:image], response[k][:s3_path])
    v.delete :image
  end

  GC.start #free the memory
  success({ :sizes => sizes })
rescue => e
  failure("Fatal error handling image: #{e.message}")
end

#save_to_cloud(image, s3_path) ⇒ Object



35
36
37
# File 'lib/spraycan/workers/previews.rb', line 35

def save_to_cloud(image, s3_path)
  Publisher::S3::Upload.upload(image, s3_path)
end