How to upload or use existing asset image using Carrierwave uploader? - ruby-on-rails

User has to options:
choose one of three images from my assets folder
upload his own
I am wondering if I can set up CarrierWave uploader to handle both cases.
So I don't want to upload file for chosen one. So I tried to update column for that.
user.update_column(:avatar, 'assets/images/default_avatar_1.jpg')
But then when I am using user.avatar.url uploader looks for this file in public folder as specified.
Is possible to override this behaviour?

Related

Copy images from one S3 bucket to another with specified path prefixes

I have an S3 bucket setup already, it contains images of a photo gallery, uploaded from my Rails app using PaperClip. Images are accessible via some arbitrary URLs like: http://s3.amazonaws.com/oldbucket/images/files/000/001/920/original/40a6885fc09c8ed4e1e3745d7f7fb770.jpg?1415766995.
Kindly advice me the best option considering following requirements:
I have to copy those images to another S3 bucket in another AWS
account
I want to make the new image URLs according to specific
patterns, like: .../newbucket/{userid}/{galleryid}/{image-size}.jpg
I want to create multiple versions of each image, according to size
(original, thumbnail and icon)
Any options using Rails gem or software that would do above would be helpful.
Thanks
For this you need to add carrierwave gem for saving image from remote url. You can also do with paperclip.
First create a seed file without adding carrierwave uploader to your app. I am considering User as model and avatar as image.
User.all.each{|u| puts user.avatar.url}`
Now remove paperclip and add carrierwave This will give you list of all images. Now add it to seed file for model you want to add this images.
class Modelx
mount_uploader :avatar, AvatarUploader
end
So your seed file should have entries like.
Modelx.create([{:avatar_remote_url => image_url1}, {:avatar_remote_url => image_url2},.....])
You can set specific path and also create multiple dimension images using carrierwave.
REfrence url for carrierwave here.

How can I upload a GarageBand (.band) file into my rails app?

I am creating a Rails app which should allow users to upload any type of audio file format. I am currently using Carrierwave gem to accomplish this, however CarrierWave will not allow uploads of certain file formats such as .band (GarageBand default format).
Is there a way to accomplish this?
Well, .band is actually a folder containing multiple files. So, yes, carrierwave does not allow uploads of folders. The folder must be converted into a .zip file.

Where do you store uploaded user images

I am not yet using a service such as Amazon S3, so where in the file structure should I store uploaded user images? I want to avoid the public directory as the images are private.
Are you using a plugin to handle your uploads? Many of them allow you to specify a path to store files, if you want to avoid the public folder a reasonable suggestion would be "#{RAILS_ROOT}/uploads/images/"
It's very much a matter of personal taste though.
For example in a carrierwave uploader this will place items in an uploads folder below RAILS_ROOT which is not publicly accessible.
def store_dir
"#{RAILS_ROOT}/uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
https://github.com/carrierwaveuploader/carrierwave#changing-the-storage-directory

Extracting uploaded archive to S3 with CarrierWave on Heroku

I want to do something what I thought will be a simple task:
Have a form with these controls:
File upload for one file
Checkbox if this file should be extracted
Text input where I would specify which file should I link to (required only if the checkbox is checked) - index_file
After submitting form:
If the checkbox isn't checked, upload the file via CarrierWave to S3 to the specified store_dir
If the checkbox is checked, extract all files from the archive (I expect only ZIP archives; I need to keep the directory structure), upload extracted files to the specified store_dir and set the index_file in database (I don't need to save to database anything about other extracted files)
As I have found, it isn't an easy task because of Heroku limitations. These files will have a large size (hundreds of MiBs or a few GiBs), so I don't want to redownload this file from S3 if possible.
I think that using Delayed Job or Resque might work, but I'm not exactly sure how to do it and what is the best solution of my problem.
Does anyone have any idea how to solve it with using the lowest resources as possible? I can change CarrierWave to another uploader (Paperclip etc.) and my hosting provider too if it isn't possible on Heroku.
I was also thinking about using CloudFlare, would this still work without problems?
Thank you for answers.
Based on this heroku support email, it would seem that the /tmp directory is many gigs in size. You just need to clean up after yourself so Heroku as a platform is not the issue.
A couple of articles may help you solve the problem:
https://github.com/jnicklas/carrierwave/wiki/How-to%3A-Make-Carrierwave-work-on-Heroku - which explains how to configure your app to use the /tmp directory as the cache directory for CarrierWave. Pay attention to the following line:
use Rack::Static, :urls => ['/carrierwave'], :root => 'tmp' # adding this line
This instructs rack to serve /carrierwave/xzy from the /tmp directory (useful for storing images temporarily)
Then, using the uploader.cache! method, you can deliberately cache the inbound uploaded file. Once stored, you can do checks to determine whether to call the uploader.store! method which will promote the contents to S3 (assuming you configured S3 as the store for CarrierWave.

carrierwave upload caching

How does carrierwave upload caching functionality work? From what I've read, it looks like it keeps the uploaded file in public/uploads/tmp to avoid reupload across form redisplays. I am guessing the cache would get assigned a unique id, but still be publicly accessible. How to make it more secure for sensitive uploads or disable this feature altogether?
One way to avoid this is to have the uploader as a separate model from the target model, such that validation errors won't require reuploading.
CarrierWave keeps uploaded images in a cache dir so you can easily re-submit forms in case of validation errors without forcing your users to re-upload images.
The cache dir in default is public/uploads/tmp but you can change it by setting the cache_dir configuration parameter.
Usually uploaded images are available for download without authentication. Therefore, placing uploaded and cached files in a public directory is fine. You can also change your uploader class to have a filename method that generates a unique random ID to make it less guessable.
By the way, this blog post describes how to integrate CarrierWave while storing and transforming images in the cloud and delivering through a CDN.

Resources