Module: ImageUploaderHelper

Defined in:
app/helpers/image_uploader_helper.rb

Instance Method Summary collapse

Instance Method Details

#clean_tempfileObject



33
34
35
36
37
38
# File 'app/helpers/image_uploader_helper.rb', line 33

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

#image_paramsObject



4
5
6
7
# File 'app/helpers/image_uploader_helper.rb', line 4

def image_params
  params[:image_path] = parse_image_data(params[:image]) if params[:image]
  params
end

#parse_image_data(base64_image) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/helpers/image_uploader_helper.rb', line 9

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