Rails Upload a File - ruby-on-rails

I'm using rails 3.2.13, and i need to upload a file manually (not using paperclip) and then convert it base 64.
The problem i have is that i think my file is not uploaded completly.
I am trying to upload a jpg.
Here is the code : the form
= form_tag send_kyc_docs_bookings_path , :multipart => true do
= file_field_tag :kyc_doc
= submit_tag "Submit"
My controller
#encoded_file = Base64.encode64(File.open(params[:kyc_doc].tempfile,"r").read)
What i get is
"/9j/4AAQSkZJRgABAQEAtAC0AAD/4R5kRXhpZgAASUkqAAgAAAAQAA8BAgAG\nAAAAzgAAABABAgAVAAAA1AAAABIBAwABAAAAAQAAAA==\n"
Now if i go on http://www.base64-image.de/step-2.php
I upload my file and get a string beginning like mine above but 1000 times longer.
So i came to the point that my file was not uploaded completly and here is my question:
how can i upload it completly?
Just for you to underqtand if i do
File.open(params[:kyc_doc].tempfile,"r").read
i get
"\xFF\xD8\xFF\xE0\u0000\u0010JFIF\u0000\u0001\u0001\u0001\u0000\xB4\u0000\xB4\u0000\u0000\xFF\xE1\u001EdExif\u0000\u0000II*\u0000\b\u0000\u0000\u0000\u0010\u0000\u000F\u0001\u0002\u0000\u0006\u0000\u0000\u0000\xCE\u0000\u0000\u0000\u0010\u0001\u0002\u0000\u0015\u0000\u0000\u0000\xD4\u0000\u0000\u0000\u0012\u0001\u0003\u0000\u0001\u0000\u0000\u0000\u0001\u0000\u0000\u0000"
Now if i do
params[:kyc_doc].tempfile.read
i get
"\xFF\xD8\xFF\xE0\x00\x10JFIF\x00\x01\x01\x01\x00\xB4\x00\xB4\x00\x00\xFF\xE1\x1EdExif\x00\x00II*\x00\b\x00\x00\x00\x10\x00\x0F\x01\x02\x00\x06\x00\x00\x00\xCE\x00\x00\x00\x10\x01\x02\x00\x15\x00\x00\x00\xD4\x00\x00\x00\x12\x01\x03\x00\x01\x00\x00\x00\x01\x00\x00\x00\x1A\x01\x05\x00\x01\x00\x00\x00\xEA\x00\x00\x00\e\x01\x05\x00\x01\x00\x00\x00\xF2\x00\x00\x00(\x01\x03\x00\x01\x00\x00\x00\x02\x00\x00\x002\x01\x02\x00\x14\x00\x00\x00\xFA\x00\x00\x00\x13\x02\x03\x00\x01\x00\x00\x00\x01\x00\x00\x00\x01\x10\x03\x00\x01\x00\x00\x00\x00\b\x00\x00\x02\x10\x03\x00\x01\x00\x00\x00\x00\x06\x00\x00i\x87\x04\x00\x01\x00\x00\x00\x16\x01\x00\x00\x01\xA4\x03\x00\x01\x00\x00\x00\x00\x00\x00\x00\x02\xA4\x03\x00\x01\x00\x00\x00\x00\x00\x00\x00\x03\xA4\x03\x00\x01\x00\x00\x00\x00\x00\x00\x00\x04\xA4\x05\x00\x01\x00\x00\x00\x0E\x01\x00\x00\x06\xA4\x03\x00\x01\x00\x00\x00\x00\x00\x00\x00\xBC\x03\x00\x00Canon\x00Canon PowerShot A610\x00\x00\xB4\x00\x00\x00\x01\x00\x00\x00\xB4\x00\x00\x00\x01\x00\x00\x002009:03:18 16:30:39\x00 \n\x00\x00 .... (1000 times longer)
however doing
Base64.encode64(params[:kyc_doc].tempfile.read)
return an empty string
Update :
File.open(params[:kyc_doc].tempfile,"r").size
=> 241202
>> File.open(params[:kyc_doc].tempfile,"r").read.size
=> 76
has anyone an idea on the subject?
Thanks
ANSWER
Ok so i figured it out, in fact when you do
File.open(params[:kyc_doc].tempfile,"r")
It only see the first caracteres of the file (maybe because of the buffer size?)
so you need to iterate through the all sets of caracteres containing in the doc by doing
File.open(params[:kyc_doc].tempfile, "rb") {|io| io.read}
this will output all the document content
and now you can properly encode it
#encoded_file =
Base64.encode64(File.open(params[:kyc_doc].tempfile,"rb") {|io|
io.read})
If anyone can explain it better, i would be glad to hear it ^^

Related

Rails: Received string from web service should be converted to a pdf image to be displayed

When using a web service I receive a string that looks like this:
JVBERi0xLjIgDQoxIDAgb2JqDQo8PA0KL1R5cGUgL0NhdGFsb2cNCi9QYWdlcyAzIDAgUg0KL1BhZ2VNb2RlIC9Vc2VOb25lDQovUGFnZUxheW91dCAvU2luZ2xlUGFnZQ0KPj4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c3RhcnR4cmVmDQo2ODg4DQolJUVPRg0K
Sorry for that.
Anyway, this should be a pdf because that was one of the request parameters. (I could also have chosen for .gif, or .jpg in different qualities.)
I tried reading the content, like this:
#label_binary = #response_label[:generate_label_response][:response_shipments][:response_shipment][:labels][:label][:content]
binary_data = Base64.decode64(#label_binary)
#label = File.open('file_name.pdf', 'wb') {|f| f.write(Base64.decode64(binary_data))}
But what this prints then as #label is just a number (three of four digits). Same when I use urlsafe_decode64.
Not very helpful.
Preferred end-result would be a pdf image file that I can immediately display (in this case a barcode on top of an invoice for sending a package).
Could I for this purpose save the file temporarily? Meaning: every time I need to display I rebuild this image. I think that would be best.
EDIT
I am making the call now directly to a 600dpi .jpg image and then:
base_64_binary_data = #response_label[:generate_label_response][:response_shipments][:response_shipment][:labels][:label][:content]
#jpg_file = File.open('label.jpg', 'wb') do |file|
content = Base64.decode64 base_64_binary_data
file << content
end
This file shows up at my_apps_name/label.jpg.
In my filesystem I can see the picture crystal clear.
In my view I then put:
<%= image_tag #jpg_file.path %>
When I inspect my html in the browser I see:
<img src="/images/label.jpg" alt="Label">
I suppose this is easily fixed, suggestions are still welcome of course, but...
..more importantly: What if this call is made several times at once. Will rails manage? Should I maybe save these images (to my S3) or build some kind of chronejob to manage it?
(I am not sure of the added value of mini_magic anymore, since I can see the .jpg being already there.)
Then on a complete side note, maybe better left for a separate question: what if the API on the other side fails?
Step one
decode http response
require "base64"
BASE_64_BINARY_DATA = "PASTE YOUR BIG STRING HERE"
pdf_file = File.open('your_pdf.pdf', 'wb') do |file|
content = Base64.decode64 BASE_64_BINARY_DATA
file << content
end
Step two
convert pdf file to an image
add to gemfile 'gem 'mini_magick' and use 'bundle install' command
put somewhere in your code
require 'mini_magick'
pdf = MiniMagick::Image.new(pdf_file.path)
pdf.pages.first.write("preview.png")
image saved as preview.png
To display pdf image put in the view
<%= image_tag ("path_to_image/preview.jpg")%>

Converting .txt file to .xml ruby on rails

I have a custom file type (.map) that I am unzipping from a Gzip file and then writing to a new file in order to pass it along to Paperclip and upload to s3. My problem is that the type should be an xml, while it is saving as a text document instead with all xml formatting correct. I need a way to convert it into the xml type and then save that as a new File object before passing it along.
Here is my code so far:
Zlib::GzipReader.open(f.tempfile) do |gz|
puts gz.orig_name
#tmp = File.new(gz.orig_name, "w+")
while chunk = gz.read(16*1024) do
#tmp.write(chunk)
end
end
reader = #tmp.read
s = location.posts.create!(
subject: #email.subject,
from: #email.from[:email],
mapfile: #tmp)
}
ideally i will be passing mapfile the xml type file, but right now it seems to be text
Update:
So now I have set my post.rb to do this, and added a name attribute to post containing the original file name:
has_attached_file :mapfile,
:preserve_files => "true",
:path =>
':rails_root/non-public/system/:id/:basename.:extension',
:url => '/:class/:id/mapfiles',
:s3_headers => lambda { |attachment| {'Content-Disposition' => "attachment; filename = #{attachment.name}"}}
And this works perfectly!
It sounds like you are talking about `Content-Type'. When you save a file to the filesystem, you can't set this: the system will decide what to use based on the file extension and possibly by analysing the contents, if it's a text file.
However, you can set the content type when you serve a file to someone's browser, and when you upload it to S3. See here for the latter Setting the Content-Type in direct to S3 upload using Rails and jQuery File Upload

File upload to S3 from Rails returns no implicit conversion of Hash into String

I'm trying to upload a file through a form to AWS S3. Here is my controller right now:
s3 = Aws::S3::Client.new(access_key_id: AWS_CREDS[aws_env]['access_key_id'], secret_access_key: AWS_CREDS[aws_env]['secret_access_key'], region: "us-east-1")
resource = Aws::S3::Resource.new(client: s3)
obj = resource.bucket('mybucket').object(file_name)
obj.upload_file(file: params[:upload_file], acl: :private)
This returns a no implicit conversion of Hash into String error. This looks correct based on what I'm seeing in other examples so I'm not sure where I went wrong.
How do I access the file that was uploaded? p params[:upload_file] just returns the filename, I do not see a hash. Do I need to do further processing on the file before I am able to upload it?
Form:
= form_tag(form_path, :method => "POST", enctype: 'multipart/form-data') do
%input{:name => "authenticity_token", :type => "hidden", :value => form_authenticity_token}
.form-group
.col-sm-3
%label Document name or date
= text_field_tag('name', nil, class: 'form-control')
- if #errors and #errors['name']
%span{:class => "form-error"}
= #errors['name']
.col-sm-3
%label Document type
= select_tag('type', options_for_select(['1', '1'], ['2', '2']), class: 'form-control')
.col-sm-3
%label Select file
= file_field_tag 'upload_file'
- if #errors and #errors['upload_file']
%span{:class => "form-error"}
= #errors['upload_file']
.col-sm-3
%br
%input.btn.btn-primary.btn-sm{:type => "submit"}
First, you didn't tell us what params[:upload_file] is but if you're attempting to upload a file through a form and pass it along to the AWS upload via the params object it won't work. You have to give upload_file a path to the file you want to upload, that means it has to be on same filesystem as your Rails server. If you're currently in a development environment this may unintentionally work for you since it's on your local machine, but it will fail on a production server.
Second, you said want to access the file that was uploaded but p params[:upload_file] is printing filename? params[:upload_file] is the input, it won't contain any information about the uploaded file. If you want to access a successfully uploaded file you need to look at the url on the obj. Try calling obj.public_url. Of course, you will need to set acl: :private to acl:'public-read'.
Take a look at this example, it might help you.
EDIT:
params[:upload_file] is a not the uploaded file. It is StringIO? object, you have to finish the uploading job in the controller. This SO answer will help you properly do file uploads. Once you have successfully uploaded a file via Rails, you can take the path of that new file and pass it to the AWS upload.
I strongly advise not trying to get the Rails form file upload AND the AWS upload working in one giant sweep. Get one working, then the other. You can test AWS upload by reading my original answer and instead of trying to pass your params[:upload_file], just pass a string path to a known file on your local machine. For example, put a test text file in your /tmp directory, then pass that path /tmp/test_file to your upload_file call.
There is no need to save the file before you upload that to S3 Bucket.
Once you're in controller, your file is already stored in a temp directory.
So, to upload the file to S3 on AWS-SDK v 2.x you can do:
obj = S3_BUCKET.object('file_name')
obj.upload_file(File.expand_path(params[:file].tempfile))
Posting your file named 'file', controller will store that automatically in a temp folder.
params[:file].tempfile
Is a reference to a temporary file, and we need to get the PATH to the file by:
File.expand_path(params[:file].tempfile)
That is it ;)

Tracking Upload Progress of File to S3 Using Ruby aws-sdk

Firstly, I am aware that there are quite a few questions that are similar to this one in SO. I have read most, if not all of them, over the past week. But I still can't make this work for me.
I am developing a Ruby on Rails app that allows users to upload mp3 files to Amazon S3. The upload itself works perfectly, but a progress bar would greatly improve user experience on the website.
I am using the aws-sdk gem which is the official one from Amazon. I have looked everywhere in its documentation for callbacks during the upload process, but I couldn't find anything.
The files are uploaded one at a time directly to S3 so it doesn't need to load it into memory. No multiple file upload necessary either.
I figured that I may need to use JQuery to make this work and I am fine with that.
I found this that looked very promising: https://github.com/blueimp/jQuery-File-Upload
And I even tried following the example here: https://github.com/ncri/s3_uploader_example
But I just could not make it work for me.
The documentation for aws-sdk also BRIEFLY describes streaming uploads with a block:
obj.write do |buffer, bytes|
# writing fewer than the requested number of bytes to the buffer
# will cause write to stop yielding to the block
end
But this is barely helpful. How does one "write to the buffer"? I tried a few intuitive options that would always result in timeouts. And how would I even update the browser based on the buffering?
Is there a better or simpler solution to this?
Thank you in advance.
I would appreciate any help on this subject.
The "buffer" object yielded when passing a block to #write is an instance of StringIO. You can write to the buffer using #write or #<<. Here is an example that uses the block form to upload a file.
file = File.open('/path/to/file', 'r')
obj = s3.buckets['my-bucket'].objects['object-key']
obj.write(:content_length => file.size) do |buffer, bytes|
buffer.write(file.read(bytes))
# you could do some interesting things here to track progress
end
file.close
After read the source code of the AWS gem, I've adapted (or mostly copy) the multipart upload method to yield the current progress based on how many chunks have been uploaded
s3 = AWS::S3.new.buckets['your_bucket']
file = File.open(filepath, 'r', encoding: 'BINARY')
file_to_upload = "#{s3_dir}/#{filename}"
upload_progress = 0
opts = {
content_type: mime_type,
cache_control: 'max-age=31536000',
estimated_content_length: file.size,
}
part_size = self.compute_part_size(opts)
parts_number = (file.size.to_f / part_size).ceil.to_i
obj = s3.objects[file_to_upload]
begin
obj.multipart_upload(opts) do |upload|
until file.eof? do
break if (abort_upload = upload.aborted?)
upload.add_part(file.read(part_size))
upload_progress += 1.0/parts_number
# Yields the Float progress and the String filepath from the
# current file that's being uploaded
yield(upload_progress, upload) if block_given?
end
end
end
The compute_part_size method is defined here and I've modified it to this:
def compute_part_size options
max_parts = 10000
min_size = 5242880 #5 MB
estimated_size = options[:estimated_content_length]
[(estimated_size.to_f / max_parts).ceil, min_size].max.to_i
end
This code was tested on Ruby 2.0.0p0

"latex-rails" gem generates no output

I have a problem with "latex-rails" gem. I`m trying to make function which will generate a pdf. This is my code:
code = "\\documentclass[12pt]{article}
\\begin{document}
Don't forget to include examples of topicalization.
\\end{document}"
#latex_config={:command => 'xelatex',:parse_twice => true}
LatexToPdf.generate_pdf(code, #latex_config, parse_twice = true)
In a log file I can see that "Output written on input.pdf (1 page).", but there is no input.pdf and I have no clue what is wrong.
For the sake of having an answer posted here instead of in the comments...
The LatexToPdf.generate_pdf method returns the pdf binary itself, which you'll need to write to a file. Here's one way to accomplish this:
code = "\\documentclass[12pt]{article} \\begin{document} Test \\end{document}"
latex_config = {command: 'xelatex', parse_runs: 2}
result = LatexToPdf.generate_pdf(code, latex_config)
f = File.new("testfile.pdf", "w")
f.write(result)
f.close
As you noted, the log file states that the output was written to a file; however, rails-latex writes this file to a temporary directory and destroys the directory at the end of the method (hence the need to write the returned binary content to a file yourself).

Resources