Sha256: 568677a39328b42e5e30c7cc6a531ed6590f933f4963a661bce7deb86a5fbcee

Contents?: true

Size: 1.18 KB

Versions: 1

Compression:

Stored size: 1.18 KB

Contents

require "paperclip"
require "paperclip/smart_data_uri_adapter/version"
require "base64" # this should have been required by Paperclip

module Paperclip
  class SmartDataUriAdapter < DataUriAdapter

    class << self

      def replace_original_adapter
        data_uri_test = find_and_delete_original_adapter
        Paperclip.io_adapters.register Paperclip::SmartDataUriAdapter, &data_uri_test
      end

      private

      def find_and_delete_original_adapter
        data_uri_test, * = registered_handlers.find &original_adapter?
        registered_handlers.delete_if &original_adapter?
        data_uri_test
      end

      def registered_handlers
        Paperclip.io_adapters.registered_handlers
      end

      def original_adapter?
        @original_adapter_evaluator ||= ->(args) { test, adapter = args
          adapter == Paperclip::DataUriAdapter
        }
      end

    end

    def initialize(*)
      super

      self.original_filename = "file.#{guessed_file_ext}"
    end

    private

    def guessed_file_ext
      MIME::Types[content_type].first.extensions.first || "jpeg"
    end

  end
end

Paperclip::SmartDataUriAdapter.replace_original_adapter # replace Paperclip::DataUriAdapter

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
paperclip-smart_data_uri_adapter-1.0.0 lib/paperclip/smart_data_uri_adapter.rb