Rails, where does CarrierWave store files? - ruby-on-rails

Couldn't find/understand good documentation on this. I wonder where uploads that are done by CarrierWave goes? As from my understanding it goes directly into the db, right? Could I force it to store (or create like a reference to the file) in my assets pipeline? Today had an issue when couldn't use image_tag since it grabs asset only from the assets pipeline. Could it be that letting users to store files in asset pipeline could be a potentially very risky and harmful?
So my questions:
Can I store / reference file in the assets pipeline?
Would it be a good idea?
Thanks for sharing!

If you look at your uploader you'll see a method called store_dir the default looks like this...
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
So for an attachment named "oranges.jpg" in a field called image in a model called FilmReview in the record with id 45 it's stored in...
public/uploads/film_review/image/45/oranges.jpg
You can change store_dir to store the image in a different directory, or upload it to a cloud service like AWS... see railscasts or other resources for examples of how to do ths.

Related

Rails file upload: upload a folder

I work on Rails project and client asked if I can add 'upload a folder' feature to simple file upload system that we have now. Currently it attaches files to model and then displays them on a page for download. Pretty basic.
But I can't figure out how can I handle folder uploads, with every folder having it's own content. Is there any pre-made gems that can help accomplish that?
We use Paperclip at the moment, but I don't mind migrating to Carrerwave or some other gem that would
UPDATE I see that I was unclear about my needs. I need an upload system that could handle folders. Something like this.
In Dropbox I am able to upload both files and folders. How can I make my uploaders accept folders and then display them alongside regular attached files?
you can solve it by using the interpolation of paperclip where you can create or naming the folder dynamically for the same you need to do like below
specify the path into model which you wanted always
:path => ":folder/:id_:filename"
and specify the private method in same model or using globally specify in initializer
Paperclip::interpolates :folder do |attachment, style|
attachment.instance.name
end

How can I migrate CarrierWave files to a new storage mechanism?

I have a Ruby on Rails site with models using CarrierWave for file handling, currently using local storage. I want to start using cloud storage and I need to migrate existing local files to the cloud. I am wondering if anyone can point out a method for doing this?
Bonus points for using a model attribute that would allow me to do this row-by-row in the background without interrupting my site for extended downtime (in other words, some model rows would still have local storage while others used cloud storage).
My first instinct is to create a new uploader for each model that uses cloud storage, so I have two uploaders on each model, then transferring the files from one to the other, setting an attribute to indicate which file should be used until they are all transferred, then removing the old uploader. That seems a little excessive.
Minimal to Possibly Zero Donwtime Procedure
In my opinion, the easiest and fastest way to accomplish what you want with almost no downtime is this: (I will assume that you will use AWS cloud, but similar procedure is applicable to any cloud service)
Figure out and setup your assets bucket, bucket policies etc for making the assets publicly accessible.
Using s3cmd (command line tool for interacting with S3) or a GUI app, copy entire assets folder from file system to the appropriate folder in S3.
In your app, setup carrierwave and update your models/uploaders for :fog storage.
Do not restart your application yet. Instead bring up rails console and for your models, check that the new assets URL is correct and accessible as planned. For example, for a video model with picture asset, you can check this way:
Video.first.picture.url
This will give you a full cloud URL based on the updated settings. Copy the URL and paste in a browser to make sure that you can get to it fine.
If this works for at least one instance of each model that has assets, you are good to restart your application.
Upon restart, all your assets are being served from cloud, and you didn't need any migrations or multiple uploaders in your models.
(Based on comment by #Frederick Cheung): Using s3cmd (or something similar) rsync or sync the assets folder from the filesystem to S3 to account for assets that were uploaded between steps 2 and 5, if any.
PS: If you need help setting up carrierwave for cloud storage, let me know.
I'd try the following steps:
Change the storage in the uploaders to :fog or what ever you want to use
Write a migration like rails g migration MigrateFiles to let carrierwave get the current files, process them and upload them to the cloud.
If your model looks like this:
class Video
mount_uploader :attachment, VideoUploader
end
The migration would look like this:
#videos = Video.all
#videos.each do |video|
video.remote_attachment_url = video.attachment_url
video.save
end
If you execute this migration the following should happen:
Carrierwave downloads each image because you specified a remote url for the attachment(the current location, like http://test.com/images/1.jpg) and saves it to the cloud because you changed that in the uploader.
Edit:
Since San pointed out this will not work directly you should maybe create an extra column first, run a migration to copy the current attachment_urls from all the videos into that column, change the uploader after that and run the above migration using the copied urls in that new column. With another migration just delete the column again. Not that clean and easy but done in some minutes.
When we use Heroku, most of people suggest to use cloudinary. Free and simple setup.
My case is when we use cloudinary service and need move into aws S3 for some reasons.
This is what i did with the uploader:
class AvatarUploader < CarrierWave::Uploader::Base
def self.set_storage
if ENV['UPLOADER_SERVICE'] == 'aws'
:fog
else
nil
end
end
if ENV['UPLOADER_SERVICE'] == 'aws'
include CarrierWave::MiniMagick
else
include Cloudinary::CarrierWave
end
storage set_storage
end
also, setup the rake task:
task :migrate_cloudinary_to_aws do
profile_image_old_url = []
Profile.where("picture IS NOT NULL").each do |profile_image|
profile_image_old_url << profile_image
end
ENV['UPLOADER_SERVICE'] = 'aws'
load("#{Rails.root}/app/uploaders/avatar_uploader.rb")
Profile.where("picture IS NOT NULL OR cover IS NOT NULL").each do |profile_image|
old_profile_image = profile_image_old_url.detect { |image| image.id == profile_image.id }
profile_image.remote_picture_url = old_profile_image.picture.url
profile_image.save
end
end
The trick is how to change the uploader provider by env variable. Good luck!
I have migrated the Carrier wave files to Amazon s3 with s3cmd and it works.
Here are the steps to follow:
Change the storage kind of the uploader to fog.
Create a bucket on Amazon s3 if you already dont have one.
Install s3cmd on the remote server sudo apt-get install s3cmd
Configure s3cmd s3cmd --configure.
You would need to enter public and secret key here, provided by Amazon.
Sync the files by this command s3cmd sync /path_to_your_files ://bucket_name/
Set this flag --acl-public to upload the file as public and avoid permission issues.
Restart your server
Notes:
sync will not duplicate your records. It will first check if the file is present on remote server or not.

Where's the best place to store user uploaded assetes in rails?

Given that rake assets:clean completely empties the public/assets directory, I'm having somewhat of an issue figuring out how to store model instance assets.
So for example, I have a Wine model which uses Carrierwave to store several versions of an uploaded photo in public/assets/wines/[version]_[wine_name].[ext]. This works just fine. The asset is easily accessible.
But since it's stored in public/assets/*, running rake assets:clean will remove them! What should I do about this?
I just did this for my portfolio site. Assets for users that will be uploaded via some sort of admin panel should be stored on the cloud. Something like an Amazon S3 bucket. Signup for amazon web services.
https://aws.amazon.com/
It's free up to a certain point. You should especially do this if you are using anything like heroku to deploy otherwise you could end up removing them on your next commit. I used paperclip and the setup was easy. Carrierwave is probably similar.
The answer is so mind-numbingly simple I can't believe I didn't think to try it earlier.
Carrierwave Example:
# It was this before
def store_dir
"assets/photos#{ '/testing' if Rails.env.test? }"
end
# Now it's this
def store_dir
"for/photos#{ '/testing' if Rails.env.test? }"
end
i.e., folder layout is as such:
..app/
../public
../assets # Contains only precompiled assets
../for # Contains only model instance assets
Regular precompiled assets resolve to http://mydomain.com/assets/* and instance assets resolve to http://mydomain.com/for/*. Specifically, I have a subdomain pointing to my assets called http://assets.mydomain.com, so for example it resolves to http://assets.mydomain.com/for/wines/some-asset.jpg.
This solves the issue of rake assets:clean because the /for directory is never touched. No need to explicitly host assets offsite.
Now as to whether or not I need to specify something else in production so the assets are actually served, I don't know - but at least in development this is working.

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

Is there a best directory to place image uploads on heroku?

I'm using carrierwave and I want to change the directory where images are stored.
Right now the image URL is /uploads/modelname/image/51/nameoffile.jpg
the store_dir in ImageUploader is
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
I definitely do not want the modelname to show
Is there an accepted ideal path where images should be stored on heroku?
Heroku doesn't allow uploads to their servers. You need to use another storage medium, like Amazon's S3.
I'm actually using Parse's (www.parse.com) API to store images on their solution. But it depends how you need access to your images.
You can upload files to the Heroku dyno filesystems but the filesystem are perishable and not shared among your dynos. Here's a Gist showing how to make Carrierwave store uploaded file in AWS S3 which is a better option: https://gist.github.com/cblunt/1303386
Here's a Heroku guide for accomplishing this with PaperClip: https://devcenter.heroku.com/articles/paperclip-s3

Resources