Sha256: 2ed25bb16ec20054e75b2fa51af094bc2c609eba5ccd0020ddb0b755be766cf3

Contents?: true

Size: 1.49 KB

Versions: 20

Compression:

Stored size: 1.49 KB

Contents

module Locomotive
  module Dragonfly

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

    def self.thumbnail_pdf(source, resize_string)
      if file = self.fetch_file(source)
        file.thumb(resize_string, format: 'png', frame: 0).encode('png').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)

        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.strip!

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

  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
locomotivecms-3.1.2 lib/locomotive/dragonfly.rb
locomotivecms-3.2.1 lib/locomotive/dragonfly.rb
locomotivecms-3.2.0 lib/locomotive/dragonfly.rb
locomotivecms-3.2.0.rc2 lib/locomotive/dragonfly.rb
locomotivecms-3.2.0.rc1 lib/locomotive/dragonfly.rb
locomotivecms-3.1.1 lib/locomotive/dragonfly.rb
locomotivecms-3.1.0 lib/locomotive/dragonfly.rb
locomotivecms-3.1.0.rc3 lib/locomotive/dragonfly.rb
locomotivecms-3.1.0.rc2 lib/locomotive/dragonfly.rb
locomotivecms-3.1.0.rc1 lib/locomotive/dragonfly.rb
locomotivecms-3.0.1 lib/locomotive/dragonfly.rb
locomotivecms-3.0.0 lib/locomotive/dragonfly.rb
locomotivecms-3.0.0.rc7 lib/locomotive/dragonfly.rb
locomotivecms-3.0.0.rc6 lib/locomotive/dragonfly.rb
locomotivecms-3.0.0.rc5 lib/locomotive/dragonfly.rb
locomotivecms-3.0.0.rc4 lib/locomotive/dragonfly.rb
locomotivecms-3.0.0.rc3 lib/locomotive/dragonfly.rb
locomotivecms-3.0.0.rc2 lib/locomotive/dragonfly.rb
locomotivecms-3.0.0.rc1 lib/locomotive/dragonfly.rb
locomotivecms-3.0.0.pre.beta.1 lib/locomotive/dragonfly.rb