Sha256: dc0d3c5ccfc9d0b3309a3194a6db286d0aeb9b847e0a83ea80d3df770d4b9673

Contents?: true

Size: 1.1 KB

Versions: 40

Compression:

Stored size: 1.1 KB

Contents

module ImageUploaderHelper

  #Reference: https://gist.github.com/ifightcrime/9291167a0a4367bb55a2
  def image_params
    params[:image_path] = parse_image_data(params[:image]) if params[:image]
    params
  end

  def parse_image_data(base64_image)
    filename = "upload-image"
    in_content_type, encoding, string = base64_image.split(/[:;,]/)[1..3]

    @tempfile = Tempfile.new(filename)
    @tempfile.binmode
    @tempfile.write Base64.decode64(string)
    @tempfile.rewind

    # for security we want the actual content type, not just what was passed in
    content_type = `file --mime -b #{@tempfile.path}`.split(";")[0]

    # we will also add the extension ourselves based on the above
    # if it's not gif/jpeg/png, it will fail the validation in the upload model
    extension = content_type.match(/gif|jpeg|png/).to_s
    filename += ".#{extension}" if extension

    ActionDispatch::Http::UploadedFile.new({
      tempfile: @tempfile,
      content_type: content_type,
      filename: filename
    })
  end

  def clean_tempfile
    if @tempfile
      @tempfile.close
      @tempfile.unlink
    end
  end

end

Version data entries

40 entries across 40 versions & 1 rubygems

Version Path
kuppayam-0.2.12.pre.materialize app/helpers/image_uploader_helper.rb
kuppayam-0.2.11.pre.materialize app/helpers/image_uploader_helper.rb
kuppayam-0.2.10.pre.materialize app/helpers/image_uploader_helper.rb
kuppayam-0.2.9.pre.materialize app/helpers/image_uploader_helper.rb
kuppayam-0.2.8.pre.materialize app/helpers/image_uploader_helper.rb
kuppayam-0.2.7.pre.materialize app/helpers/image_uploader_helper.rb
kuppayam-0.2.6.pre.materialize app/helpers/image_uploader_helper.rb
kuppayam-0.2.5.pre.materialize app/helpers/image_uploader_helper.rb
kuppayam-0.2.4.pre.materialize app/helpers/image_uploader_helper.rb
kuppayam-0.2.3.pre.materialize app/helpers/image_uploader_helper.rb
kuppayam-0.2.2.pre.materialize app/helpers/image_uploader_helper.rb
kuppayam-0.2.1.pre.materialize app/helpers/image_uploader_helper.rb
kuppayam-0.2.0.pre.materialize app/helpers/image_uploader_helper.rb
kuppayam-0.1.39 app/helpers/image_uploader_helper.rb
kuppayam-0.1.38 app/helpers/image_uploader_helper.rb
kuppayam-0.1.37 app/helpers/image_uploader_helper.rb
kuppayam-0.1.36 app/helpers/image_uploader_helper.rb
kuppayam-0.1.35 app/helpers/image_uploader_helper.rb
kuppayam-0.1.34 app/helpers/image_uploader_helper.rb
kuppayam-0.1.33 app/helpers/image_uploader_helper.rb