Read data image, upload from web and save them in a database - ruby-on-rails

I'm newest in ruby on rails developpement and i would like what is the best way to save an pictures/image from the controller of my web page. I try with something like this:
#fin = File.open(params[:photos] , "rb")
#img = #fin.read
I think you have understand my reasoning. At the end I want to be able to save my picture into my database.

I would recommend that you use a gem like carrierwave: https://github.com/carrierwaveuploader/carrierwave
You really should not save a picture into a database. Instead you should store the image on some sort of other datastore and put a pointer to it in your database. Carrierwave makes this very easy and has different adapters to store the images on your local filesystem, S3, SFTP, or NFS.
Thoughtbot's Paperclip is another good alternative: https://github.com/thoughtbot/paperclip

Related

Retrieving Images Off Rails API That Used Paperclip and S3 to Handle Upload

I'm working on a project that uses a Rails API for the backend and has a separate front end calling on that API. This is the first project where I've had to store images on the API and after days of research on best practices, I'm now more confused than I was going into it. Basically, I use Paperclip and S3 to handle uploading images onto the API, but I've hit a roadblock now that I'm trying to call on the API to retrieve those images. I'm very new to handling images this way and don't know if I should somehow generate a url and store that in the database to call on for retrieving and displaying the images on the front end, or if there is a way to take the multiple parts of the image that Paperclip creates and generate the image from those? The API successfully calls the rest of the data like my object names and bios, I just can't figure out the proper way to store the images so I can easily retrieve them.
If you are using paperclip to upload images to amazon s3 then the object that the attatchment is associated with should have a method that is the name of whatever you set the item name as when you generated your paperclip migrations.
For example if when you generated the migration using...
https://github.com/thoughtbot/paperclip#migrations
rails generate paperclip user avatar
then this will give you the method on User called .avatar and this will in turn give you a method called .url that you can call on avatar.
i.e.
User.avatar.url
will give you the url to the location where the image is stored on S3

Using Amazon AWS SDK instead of Paperclip for active admin

I am developing an admin panel for a legacy database which has images stored on S3. The way this is done is a full url pointing out to the resource on S3 which is publicly readable.
Paperclip store this in different fields in a database. I need to do these steps.
Upload the file to my server
Generate a UUID
Upload file to S3 with correct permissions and name it with the UUID.
Save the complete url in a single db col.
My only requirement is uploading single image file and storing it in a single field as explained. Dont care which lib I use.
My questions are
Can I configure paperclip to change its default behaviour to do this?
If I use AWS sdk myself do I have to use somekind of threads? I cant see a decent example from where we can use Model to upload image in the same way as done in paperclip.
How is the image recieved in model when uploading using paperclip and active admin.
These may be basic questions but I am a ROR newbie! Guidance and help is much appreciated.
Instead of using paperclip I used carrierwave and fog. Which gives the ability to override name and storage location etc.
storage :fog
def initialize(*)
super
self.fog_directory = ENV['S3_BUCKET_NAME']
end
def store_dir
'pronunciations'
end
def cache_dir
"#{Rails.root}/tmp/uploads/pronunciations"
end
def extension_white_list
%w(mp3 m4a)
end

Rails: What column type to use for saving uploaded images?

This is my first Rails project. I'm a bit stumped on how image files should be saved in the database, since they take up a lot of space. My site is going to be one where people upload a lot of images. In my migration for creating the pictures table, would I save the image file name as a string? Also does anyone have hints on where to save the images (assets, public, etc)?
Sorry, I'm just a noob looking for a little guidance.
You should consider storing your images on Amazon's S3 (or a similar alternative) instead of the database. You can use gems like paperclip to help you with uploading to a remote storage server.
However, if you really do want to store the binary image data in the database, then you probably want to use 'blob' as the column datatype.
You can also use carrierwave. Just create a string column and mount an uploader on it.
Blobs should only be used in special cases!
You save the images as files on a storage system on a server or the cloud. Then, you create a record in your table with the path to it.
Do not store an images byte for byte\BOLB.

How to use ORM (activerecord) with Carrierwave_direct?

I'm successfully using Carrierwave_direct - it mounts an uploader and uploads directly to S3 yay! H
owever, unlike Carrierwave it does not persist a record into the DB - rather it just redirects back to a 'success_path' (standard AWS/S3 function).
Before embarking on rolling my own solution I'm curious if anyone has figured this out or has a good approach for this. I would like it to upload directly to S3 and use carrierwave to persist the record to the db.
My immediate thoughts are to pass params to the process which get carried back to the app - then grap these params and create the record.
Appreciate any thoughts.
All you have to do is:
giving the page you want to go back on success in the new action of your controller: #uploader.success_action_redirect = 'Your_update_page'
Amazon will bring you back to this page on success and add a 'key' argument in which you will have the information you need to update the db.
This is very well explained on the github readme of carrierwave direct.

Mananging upload of images to create custom pdfs on heroku - right tools

Im desiging an app which allows users to upload images (max 500k per image, roughly 20 images) from their hard drive to the site so as to be able to make some custom boardgames (e.g. snakes and ladders) in pdf formate. These will be created with prawn instantly and then made available for instant download.
Neither the images uploaded nor the pdfs created need to be saved on my apps side permanently. The moment the user downloads the pdf they are no longer needed.
Heroku doesn't support saving files to the system (it does allow to the tmp directory but says you shouldnt rely on it striking it out for me). I'm wondering what tools / services I should be looking into to get round this. Ive looked into paperclip, I'm wondering if this is right for this type of job.
Paperclip is on the right track, but the key insight is you need to use the S3 storage backend (Paperclip uses the FS by default which as you've noticed is no good on Heroku). It's pretty handy; instead of flushing writes out to the file system, it uses the AWS::S3 gem to upload them to S3. You can read more about it in the rdoc here: http://github.com/thoughtbot/paperclip/blob/master/lib/paperclip/storage/s3.rb
Here's how the flow would work:
I'd let your users upload their multiple source images. Here's an article on allowing multiple attachments to one model with paperclip: http://www.cordinc.com/blog/2009/04/multiple-attachments-with-vali.html.
Then when you're ready to generate the PDF (probably in a background job, right?), what you do is download all the source images to somewhere in tmp/ (make sure the directory is based on your model id or something so if two people do this at once, the files don't get stepped on). Once you've got all the images downloaded, you can generate your PDF. I know this is using the file system, but as long as you do all your filesystem interactions in one request or job cycle, it will work, your files will still be there. I use this method in a couple production web apps. You can't count on tmp/ being there between requests, but within one it's reliably there.
Storing your generated PDF on S3 with paperclip makes sense too, since then you can just hand your users the S3 URL. If you want you can make something to clear the files off every so often if you don't want to pay the S3 costs, but they should be trivial.
Paperclip sounds like an ideal candidate. It will save images in RAILS_ROOT/public/system/, which is both persistent and private (shouldn't be able to be enumerated on shared hosting).
You can configure it to produce thumbnails of your images if you wish.
And it can remove the images it manages when the associated model is destroyed - after your user downloads their PDF, and you delete the record from the database.
Prawn might not be appropriate, depending on the complexity of the PDFs you need to generate. If you have $$$, go for PrinceXML and the princely gem. I've had some success with wkhtmltopdf, which generates PDFs from a Webkit render of HTML/CSS - but it doesn't support any of the advanced page manipulation stuff that Prince does.

Resources