Sha256: 4f002e7aea5a0a1cdc7341ae3800201404caa09e84c126514f9ff3ea21c85e5f

Contents?: true

Size: 1.23 KB

Versions: 19

Compression:

Stored size: 1.23 KB

Contents

module Locomotive
  module Middlewares
    class ImageThumbnail

      PATH = '_image_thumbnail'

      def initialize(app, opts = {})
        @app = app
      end

      def call(env)
        if env['PATH_INFO'] == self.class.route
          request = Rack::Request.new(env)

          image, format = request.params['image'], request.params['format']

          if thumb = make_thumb(image, format)
            respond(thumb)
          else
            respond('', 422)
          end
        else
          @app.call(env)
        end

      end

      def self.route
        Locomotive.mounted_on + '/' + PATH
      end

      private

      def make_thumb(image, format)
        return nil if image.blank? || format.blank?

        file = Locomotive::Dragonfly.app.fetch_url(image)

        begin
          if image.starts_with?('data') # base64
            file.apply.content.process!(:thumb, format).b64_data
          else
            file.thumb(format).url # url
          end
        rescue ArgumentError => e
          Locomotive.log "[image thumbnail] error = #{e.message}"
          nil
        end
      end

      def respond(response = '', status = 200)
        [status, { 'Content-Type' => 'text/plain' }, [response]]
      end

    end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
locomotivecms-3.4.1 lib/locomotive/middlewares/image_thumbnail.rb
locomotivecms-3.4.0 lib/locomotive/middlewares/image_thumbnail.rb
locomotivecms-3.3.0 lib/locomotive/middlewares/image_thumbnail.rb
locomotivecms-3.3.0.rc3 lib/locomotive/middlewares/image_thumbnail.rb
locomotivecms-3.3.0.rc2 lib/locomotive/middlewares/image_thumbnail.rb
locomotivecms-3.1.2 lib/locomotive/middlewares/image_thumbnail.rb
locomotivecms-3.2.1 lib/locomotive/middlewares/image_thumbnail.rb
locomotivecms-3.3.0.rc1 lib/locomotive/middlewares/image_thumbnail.rb
locomotivecms-3.2.0 lib/locomotive/middlewares/image_thumbnail.rb
locomotivecms-3.2.0.rc2 lib/locomotive/middlewares/image_thumbnail.rb
locomotivecms-3.2.0.rc1 lib/locomotive/middlewares/image_thumbnail.rb
locomotivecms-3.1.1 lib/locomotive/middlewares/image_thumbnail.rb
locomotivecms-3.1.0 lib/locomotive/middlewares/image_thumbnail.rb
locomotivecms-3.1.0.rc3 lib/locomotive/middlewares/image_thumbnail.rb
locomotivecms-3.1.0.rc2 lib/locomotive/middlewares/image_thumbnail.rb
locomotivecms-3.1.0.rc1 lib/locomotive/middlewares/image_thumbnail.rb
locomotivecms-3.0.1 lib/locomotive/middlewares/image_thumbnail.rb
locomotivecms-3.0.0 lib/locomotive/middlewares/image_thumbnail.rb
locomotivecms-3.0.0.rc7 lib/locomotive/middlewares/image_thumbnail.rb