Sha256: e7e817fad8c409946ec0dc416c438584d622098e76b96b2a7b0c62740c7f1084

Contents?: true

Size: 1.29 KB

Versions: 4

Compression:

Stored size: 1.29 KB

Contents

require "open-uri"

module Fogged
  module Resources
    class AWSThumbnailJob < ActiveJob::Base
      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

      private

      def source_from(url)
        uri = URI.parse(url)
        extension = File.extname(uri.path)
        [File.basename(uri.path, extension), extension]
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
fogged-0.0.13 app/jobs/fogged/resources/aws_thumbnail_job.rb
fogged-0.0.12 app/jobs/fogged/resources/aws_thumbnail_job.rb
fogged-0.0.11 app/jobs/fogged/resources/aws_thumbnail_job.rb
fogged-0.0.10 app/jobs/fogged/resources/aws_thumbnail_job.rb