Sha256: 009ff53f222c88e4d0e7a1d823d35463940399375284659ff55a5b99813d8e62

Contents?: true

Size: 1.23 KB

Versions: 13

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
          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

13 entries across 13 versions & 1 rubygems

Version Path
locomotivecms-4.2.0.alpha2 lib/locomotive/middlewares/image_thumbnail.rb
locomotivecms-4.2.0.alpha1 lib/locomotive/middlewares/image_thumbnail.rb
locomotivecms-4.1.1 lib/locomotive/middlewares/image_thumbnail.rb
locomotivecms-4.1.0 lib/locomotive/middlewares/image_thumbnail.rb
locomotivecms-4.1.0.rc1 lib/locomotive/middlewares/image_thumbnail.rb
locomotivecms-4.0.3 lib/locomotive/middlewares/image_thumbnail.rb
locomotivecms-4.0.2 lib/locomotive/middlewares/image_thumbnail.rb
locomotivecms-4.0.1 lib/locomotive/middlewares/image_thumbnail.rb
locomotivecms-4.0.0 lib/locomotive/middlewares/image_thumbnail.rb
locomotivecms-4.0.0.rc0 lib/locomotive/middlewares/image_thumbnail.rb
locomotivecms-4.0.0.alpha3 lib/locomotive/middlewares/image_thumbnail.rb
locomotivecms-4.0.0.alpha2 lib/locomotive/middlewares/image_thumbnail.rb
locomotivecms-4.0.0.alpha1 lib/locomotive/middlewares/image_thumbnail.rb