Class: Fogged::Resources::AWSThumbnailJob

Inherits:
ActiveJob::Base
  • Object
show all
Defined in:
app/jobs/fogged/resources/aws_thumbnail_job.rb

Instance Method Summary collapse

Instance Method Details

#perform(resource) ⇒ Object



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
# File 'app/jobs/fogged/resources/aws_thumbnail_job.rb', line 6

def perform(resource)
  return unless Fogged.minimagick_enabled

  step = 100 / Fogged.thumbnail_sizes.size
  Fogged.thumbnail_sizes.each_with_index do |size, index|
    Tempfile.open(source_from(resource.url), :binmode => true, :encoding => "ascii-8bit") do |source|
      Tempfile.open(["thumbnail", ".png"]) do |t|
        source.write(open(resource.url).read)
        source.flush

        MiniMagick::Tool::Convert.new do |c|
          c << source.path
          c.resize(size.to_s)
          c << t.path
        end

        Fogged.resources.files.create(
          :key => resource.send(:fogged_name_for, :thumbnails, index),
          :body => File.read(t.path),
          :public => true,
          :content_type => Mime::PNG.to_s
        )
      end
    end

    resource.increment!(:encoding_progress, step)
  end
  resource.update!(:encoding_progress => 100)
end