Sha256: e323e080b22e9016482c0ac1fd4f264e13125607d9457bac64a5c289199242b9

Contents?: true

Size: 1.72 KB

Versions: 5

Compression:

Stored size: 1.72 KB

Contents

require 'locomotive/dragonfly/processors/smart_thumb'

module Locomotive
  module Dragonfly

    def self.resize_url(source, resize_string, timestamp = nil)
      if file = self.fetch_file(source)
        url = file.thumb(resize_string).url
        timestamp ? url + "?#{timestamp}" : url
      else
        Locomotive.log :error, "Unable to resize on the fly: #{source.inspect}"
        return
      end
    end

    def self.thumbnail_pdf(source, resize_string, timestamp = nil)
      if file = self.fetch_file(source)
        url = file.thumb(resize_string, format: 'png', frame: 0).encode('png').url
        timestamp ? url + "?#{timestamp}" : url
      else
        Locomotive.log :error, "Unable to convert the pdf: #{source.inspect}"
        return
      end
    end

    def self.fetch_file(source)
      file = nil

      if source.is_a?(String) || source.is_a?(Hash) # simple string or drop
        source = source['url'] if source.is_a?(Hash)

        source = clean_source(source)

        if source =~ /^http/
          file = self.app.fetch_url(source)
        else
          file = self.app.fetch_file(File.join('public', source))
        end

      elsif source.respond_to?(:url) # carrierwave uploader
        if source.path.first == '/'
          file = self.app.fetch_file(source.path)
        else
          file = self.app.fetch_url(source.url)
        end
      end

      file
    end

    def self.app
      ::Dragonfly.app(:engine)
    end

    protected

    def self.clean_source(source)
      # remove the leading / trailing whitespaces
      _source = source.strip

      # remove the query part (usually, timestamp) if local file
      _source = _source.sub(/(\?.*)$/, '') unless _source =~ /^http/

      _source
    end

  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
locomotivecms-4.2.0.alpha2 lib/locomotive/dragonfly.rb
locomotivecms-4.2.0.alpha1 lib/locomotive/dragonfly.rb
locomotivecms-4.1.1 lib/locomotive/dragonfly.rb
locomotivecms-4.1.0 lib/locomotive/dragonfly.rb
locomotivecms-4.1.0.rc1 lib/locomotive/dragonfly.rb