Turning binary into PDF in Ruby on Rails - ruby-on-rails

I'm integrating with a third party ID verification provider. Once the ID verification checks have run a report is generated, which I can access with a get request.
The response is binary text (screenshot attached), which I want to save to a PDF file.
Function:
def generate_pdf
resources = "applicants/61f84499b7b92f00014d5c6d/summary/report?report=applicantReport"
response = RestClient.get(request_env_url(resources), signed_header(resources, nil, 'GET', 'application/pdf'))
puts response
end
How do I take the binary response, create a new file and add the binary into that file in a friendly and readable format?

If you can access a filesystem, you might just want to write that data to a file:
File.open("thing.pdf", "wb") { |f| f.write response }

Related

How to get file stream in the output of Object.get

I a writing a Rails API, with help of aws-sdk-ruby, which retrieves a file from AWS and returns in the response of API. Can I get somehow file stream in response of object.get, which I can directly return from the Rails API.
s3 = Aws::S3::Resource.new
bucket_name = "my_bucket"
bucket = s3.bucket(bucket_name)
object = bucket.object("a/b/my.pdf")
Rails.logger.info 'Downloading file to AWS'
downloaded_data = object.get({})
send_data(downloaded_data,
:filename => "my.pdf",
:type => "mime/type"
)
But it does not return file.
One option I know is to first save the file in local using this line:
object.get(response_target: '/tmp/my.pdf')
Than I can return this file but is there a way to skip this step and directly return the response of object.get without saving in local.
I can not use this solution as my URL are not public and I am just creating a REST API.
I got screen like following when I tried this solution.
As of now what I am doing is getting a URL from the object like this:
url = object.presigned_url(:get, expires_in: 3600)
and using following code to send the response:
data = open(url)
send_data data.read, filename: file_name, type: "mime/type"

How to download a file via the Google Drive API?

I'm working to download a file via the Google Drive API using the gem google-api-client.
x = Google::Apis::DriveV2
drive = x::DriveService.new
drive.authorization = auth
files = drive.list_files
files.items.each_with_index do |file, index|
url_to_index = file.export_links.select { |k, v| v if k == 'text/plain' }
file_content = open(url_to_index["text/plain"]).read
end
The problem is file_content is returning the Google login screen not the file in text/plain format. It appears that when my Rails app opens the URL it does not have access to the text file.
What's the right way to enable my Rails app to grab the file in the text format?
Stated in Download Files
Depending on the type of download you'd like to perform — a file, a Google Document, or a content link — you'll use one of the following URLs:
Download a file — files.get with alt=media file resource
Download and export a Google Doc — files.export
Link a user to a file — webContentLink from the file resource
Downloading the file requires the user to have at least read access. Additionally, your app must be authorized with a scope that allows reading of file content.
You may go through the documentation for more information and examples.
See the log output: Two factor autentifications need to you permit access from you app to your google drive account.
In log you will see needed info to do this: link and secret_key.
tail -f log/*.log
I just realize that mimetype for download is different from the file meta.
Please check this url for available mimetype : https://developers.google.com/drive/api/v3/integrate-open
for example mimeType : 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
'

Getting file name from POST request in Spray-can

I am using Spray-can to host a REST service to which a user will be able to upload a file. The block of code that listens for incoming requests is given below:
def receive: Receive = {
case _: Http.Connected => sender ! Http.Register(self)
case req#HttpRequest(HttpMethods.POST, Uri.Path("/upload"), headers, entity, _) =>
logger.info("Received file upload request.")
// Process the uploaded data using the 'entity' object
I upload the file using this curl command:
curl --data-binary #inputFile.csv 'devserver:8998/upload?tenant=DressShop&facility=CityCenter&customer=Jimmy'
The challenge I am facing is that I'm not able to pick up the filename "inputFile.csv" from the request, though I'm getting the data from the "entity" object. I tried poring through the API but couldn't find out any way to get the filename.
My objective is to ensure that I allow upload of only csv files.
You need to process the entity as form data using as
as[MultipartFormData]
Then you can get the file name from the header fields:
def processFormData(data: MultipartFormData) = {
var attForm = ""
val bodyPart = data.fields(0)
data.fields foreach {
bodyPart => println(bodyPart.headers.find(h=> h.is("content-disposition")).get.value)
}
}
This might help:
The filename can be found in parameters.

How do I mask Facebook graph api URLs for pictures?

I'm trying to display Facebook profile pictures on my site, but don't want to leak the facebook id's of the people in the source.
For example, this URL: http://graph.facebook.com/4/picture will redirect to: http://profile.ak.fbcdn.net/hprofile-ak-snc4/157340_4_3955636_q.jpg when you load it in a browser. I'd like to get the 2nd url (CDN url) and use it as my img src since it doesn't show the facebook id in the url.
I'm doing this in Ruby on Rails at the moment and am curious if there's a better way that what I have done below:
def picture_square(facebook_id, secure=false)
raw_url = "http://graph.facebook.com/" facebook_id + "/picture?type=square"
if secure
binary_img = ''
open(raw_url) do |f|
binary_img = f.read
end
encoded_img = Base64.encode64(binary_img)
return 'data:image/jpg;base64,' + encoded_img.to_s
else
return raw_url
end
end
You could call this with the following HTML (using the above example):
<img src="<%= picture_square(4, true) %>"
This definitely works and uses the inline image properties to actually render the image, but it's a bit slow if you have a bunch of images that you're trying to load.
Is there a way in Ruby that I can get the redirected URL and just return that instead of trying to get the actual raw binary data and encode it to base64?
Make a call to the graph API with this url:
http://graph.facebook.com/4/?fields=picture&type=large
This will return the image you are looking for inside the json response. The other option would be to make an http request to the first url you posted and then inspect the HTTP headers to read the location header..

Using Send_File to a Remote Source (Ruby on Rails)

In my app, I have a requirement that is stumping me.
I have a file stored in S3, and when a user clicks on a link in my app, I log in the DB they've clicked the link, decrease their 'download credit' allowance by one and then I want to prompt the file for download.
I don't simply want to redirect the user to the file because it's stored in S3 and I don't want them to have the link of the source file (so that I can maintain integrity and access)
It looks like send_file() wont work with a remote source file, anyone recommend a gem or suitable code which will do this?
You would need to stream the file content to the user while reading it from the S3 bucket/object.
If you use the AWS::S3 library something like this may work:
send_file_headers!( :length=>S3Object.about(<s3 object>, <s3 bucket>)["content-length"], :filename=><the filename> )
render :status => 200, :text => Proc.new { |response, output|
S3Object.stream(<s3 object>, <s3 bucket>) do |chunk|
output.write chunk
end
}
This code is mostly copied form the send_file code which by itself works only for local files or file-like objects
N.B. I would anyhow advise against serving the file from the rails process itself. If possible/acceptable for your use case I'd use an authenticated GET to serve the private data from the bucket.
Using an authenticated GET you can keep the bucket and its objects private, while allowing temporary permission to read a specific object content by crafting a URL that includes an authentication signature token. The user is simply redirected to the authenticated URL, and the token can be made valid for just a few minutes.
Using the above mentioned AWS::S3 you can obtain an authenticated GET url in this way:
time_of_exipry = Time.now + 2.minutes
S3Object.url_for(<s3 object>, <s3 bucket>,
:expires => time_of_exipry)
Full image download method using temp file (tested rails 3.2):
def download
#image = Image.find(params[:image_id])
open(#image.url) {|img|
tmpfile = Tempfile.new("download.jpg")
File.open(tmpfile.path, 'wb') do |f|
f.write img.read
end
send_file tmpfile.path, :filename => "great-image.jpg"
}
end
You can read the file from S3 and write it locally to a non-public directory, then use X-Sendfile (apache) or X-Accel-Redirect (nginx) to serve the content.
For nginx you would include something like the following in your config:
location /private {
internal;
alias /path/to/private/directory/;
}
Then in your rails controller, you do the following:
response.headers['Content-Type'] = your_content_type
response.headers['Content-Disposition'] = "attachment; filename=#{your_file_name}"
response.headers['Cache-Control'] = "private"
response.headers['X-Accel-Redirect'] = path_to_your_file
render :nothing=>true
A good writeup of the process is here

Resources