Rails: receive and send images - ruby-on-rails

I'm writing rails application. My task is to show data from old java application. One of the objects has images attached, I have to display these images and allow user to add new ones.
I want to know what is the best way to let user uploading images to old app.
Old app has http post method for adding images with params: multipart file, md5 hash of the image and object id.
I tried using paperclip or carrierwave but their documentation is about saving uploaded images and I just want to transfer them somewhere else and not save anything.

there are few ways to communicate with java from ruby.
REST API - expose Java functionalities through endpoints. then use an HTTP client.
Remote Procedure Call - Apache Thrift
JRuby - embedded java programme into Ruby
however, I think maybe you can just proxy the Java API without going through the rails application.

Related

Uploading image with vuejs frontend

I'd like to implement uploading a profile picture for users. I'm using a VueJs frontend with a Rails API. What I'm trying to do is upload the image only using the frontend. I'd like for the file to get uploaded without any calls API calls. I could then store the location of the file in the picture attribute in the backend and retrieve it. Is that possible? I'm also using Element library.
<el-upload :http-request="addAttachment">
<el-button size="small" type="primary">Click Upload</el-button>
</el-upload>```
What you are looking at is called,
direct uploads or browser based uploads.
There should be support from storage service you are using.
Example: using S3 and GCS it is possible.
Upload without any API calls? -
Not sure, I once had to make a small API call to get the signature key and use it with POST params to upload file to storage service(GCS)
Once the API response is returned, you then might want to write to db about the file path.

Is it possible to send file through actioncable ? If possible, how?

I already implemented the chat function using actioncable but don't know how to send file through it or is it even possible or not.
I am trying to make chat application where user can upload file in chatroom other users can see that immediately without reloading the page as actioncable offers.
It's possible to upload files using websockets. (File upload using java websocket API and Javascript)
But through ActionCable that's not possible at the moment. As ActionCable wraps the Websocket in Javascript this is also going to be "hacky" to patch this in, so I would wait for a new release and write an issue on the rails repo instead.
So for your chat app you still need to use a normal form submit to upload a file. If you want it to happen asynchronously you can use my "patched" version of jquery-ujs which allows sending files with the "data-remote=true" flag.
See https://github.com/Elektron1c97/jquery-ujs-files
It's possible by transforming your file into a base64 data url on the client via some js then to send this url thru actioncable to the server who's gonna broadcast just a base64 data url.
I have test it with image, audio and video files. Not with pdf or txt but it should work. For big file like a video it's instable.
It's just experimental.
VoilĂ .

how to get bytes loaded to server using rails paperclip gem?

i am using paperclip gem to upload images on the server side, i need to get updated with the bytes loaded on server through the upload process.
my aim is to update the progress bar accordingly. how do I do this?
You can only do this by using a Flash-based uploading tool which has direct access to the file and the upload stream. Just integrate it with Paperclip (and there are a ton of examples on google showing how to)
I would recommend one of these:
http://plupload.com/
http://www.uploadify.com/

Posting multipart form data with Ruby

I'm building a rails app that interacts with a 3rd party API
When a user uploads a file to rails, it should be forwarded on to the 3rd party site via an HTTP POST.
In some cases, the upload can be several hundred MBs.
At the moment, I've just been re-posting to the API using Net::HTTP and accessing the multipart form object like so
#tempfile = params[:video][:file_upload].tempfile
This is hella slow though and feels kinda dirty.
Is there a better way to do this?
Is it possible to have the user post directly to the 3rd party service or do you have to handle the API through your Rails stack? Ideally you would be able to do this and would not have to load the file into your stack and then re-post it to the API. If you can't post directly, I would recommend seeing if the API has a streaming service so that you can send parts of the file instead of the entire thing at once. Either way I think you'll start running into Timeout errors on your side and on the API side with large files, so you'll have to increase your own timeouts or create a different type of streaming file uploader.
Spin up a background job using DelayedJob. In the delayed job, you could try rails redirect_to.
https://github.com/tobi/delayed_job
http://apidock.com/rails/ActionController/Base/redirect_to

Ruby on Rails deployment, on "thin" server with lot of attachments

A lot of PDFs are stored inside MySQL as a BLOB field for each PDF file. The average file size is 500K each.
The Rails app will stream the :binary data as file downloads, where there is a user click on the download link.
Assume there is a maximum of 5 users downloading 5 PDFs concurrently, what kind of deployment setup parameters I should be aware of? e.g. for the case of thin:
thin start --servers 3
whether --servers 3 is good enough (or 5 or more is needed) for the above example?
The 2nd question is whether 'thin' a capable solution?
Thanks!
Firstly I don't think you should be storing files in a database. A better place would be in the file system, or alternatively in cloud storage like S3. If you were to use an attachment plugin like paperclip this is a very easy to setup.
However, lets assume you want to store your files in your database.
The problem with your current set up is that when you're sending your file your thin instance is blocking while your client downloads the data. This means if you have 3 thin instances and 3 people downloading pdfs then you're site will not respond to any requests.
Thankfully there is a solution to this problem which involves the x-sendfile header. The way this works is your thin instance sends the file to your webserver, for example nginx, which then serves the file directly.
Here's a great post on stackoverflow on how to set this up with nginx.
Which web server are you using?
You can define one or two thin dedicated to your download. In your webserver you can made a different proxy distribution in your Rails Application and in your download url

Resources