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