Sha256: 03928c1501ca61494c57a575d2c64c16a3f3922d5e20c92e286d5ba437b84857

Contents?: true

Size: 1.95 KB

Versions: 21

Compression:

Stored size: 1.95 KB

Contents

# encoding: utf-8
require 'digest/sha1'
require 'mime/types'

module Ckeditor
  module Http
    # Create tempfile from hash
    class UploadedFile
      attr_accessor :original_filename, :content_type, :tempfile, :headers

      def initialize(hash)
        @original_filename = hash[:filename]
        @content_type      = hash[:type]
        @headers           = hash[:head]
        @tempfile          = hash[:tempfile]
        raise(ArgumentError, ':tempfile is required') unless @tempfile
      end

      def open
        @tempfile.open
      end

      def path
        @tempfile.path
      end

      def read(*args)
        @tempfile.read(*args)
      end

      def rewind
        @tempfile.rewind
      end

      def size
        @tempfile.size
      end
    end

    # Usage (paperclip example)
    # @asset.data = QqFile.new(params[:qqfile], request)
    class QqFile < ::Tempfile

      def initialize(filename, request, tmpdir = Dir::tmpdir)
        @original_filename  = filename
        @request = request

        super Digest::SHA1.hexdigest(filename), tmpdir
        binmode
        fetch
      end

      def fetch
        self.write(body)
        self.rewind
        self
      end

      def original_filename
        @original_filename
      end

      def content_type
        types = MIME::Types.type_for(original_filename)
        types.empty? ? @request.content_type : types.first.to_s
      end

      def body
        if @request.raw_post.respond_to?(:force_encoding)
          @request.raw_post.force_encoding("UTF-8")
        else
          @request.raw_post
        end
      end
    end

    # Convert nested Hash to HashWithIndifferentAccess and replace
    # file upload hash with UploadedFile objects
    def self.normalize_param(*args)
      value = args.first
      if Hash === value && value.has_key?(:tempfile)
        UploadedFile.new(value)
      elsif value.is_a?(String)
        QqFile.new(*args)
      else
        value
      end
    end
  end
end

Version data entries

21 entries across 21 versions & 2 rubygems

Version Path
glebtv-ckeditor-4.4.7.4 lib/ckeditor/http.rb
glebtv-ckeditor-4.4.7.3 lib/ckeditor/http.rb
glebtv-ckeditor-4.4.7.2 lib/ckeditor/http.rb
glebtv-ckeditor-4.4.7.1 lib/ckeditor/http.rb
glebtv-ckeditor-4.4.7 lib/ckeditor/http.rb
glebtv-ckeditor-4.4.6 lib/ckeditor/http.rb
glebtv-ckeditor-4.4.3.4 lib/ckeditor/http.rb
glebtv-ckeditor-4.4.3.3 lib/ckeditor/http.rb
glebtv-ckeditor-4.4.3.2 lib/ckeditor/http.rb
glebtv-ckeditor-4.4.3.1 lib/ckeditor/http.rb
glebtv-ckeditor-4.4.3.0 lib/ckeditor/http.rb
glebtv-ckeditor-4.3.2.6 lib/ckeditor/http.rb
ckeditor-4.1.0 lib/ckeditor/http.rb
glebtv-ckeditor-4.3.2.5 lib/ckeditor/http.rb
ckeditor-4.0.11 lib/ckeditor/http.rb
glebtv-ckeditor-4.3.2.4 lib/ckeditor/http.rb
glebtv-ckeditor-4.3.2.3 lib/ckeditor/http.rb
glebtv-ckeditor-4.3.2.2 lib/ckeditor/http.rb
glebtv-ckeditor-4.3.2.1 lib/ckeditor/http.rb
glebtv-ckeditor-4.3.2.0 lib/ckeditor/http.rb