Carrierwave store method selection on-the-fly - ruby-on-rails

Is there any existing solution to select store method on the fly in carrierwave?
I wish to select local store or fog (two providers) store on record (user) basis.
Do i have to rewrite my models to inherited models or there is other solution already?
Thanks for your thoughts.

You can access your model from uploader using the model method
To specify custom store dir, you can use
storage :fog
So, you can write your own macro-class-method which will call storage :for or local based on preference, should be possible but I don't know what you want to archieve exactly. Providing more code (The user code and the conditions on which storage depends) would be helpful.

Related

Active Storage multiple attachments per record in a separate way?

I've looked in the official guides but can't resolve this.
I have a model campaign which needs to have multiple image attachments that are different between them such as campaignlogo, campaigndesign, campaignextra.
I'm using active storage to make this possible, but don't know how to approach it.
¿Can I use multiple has_one_attached in the campaign model?
has_one_attached :campaignextra
has_one_attached :campaigndesign
has_one_attached :campaignlogo
I see there is an option has_many_attached, but works only for a same attachment type (ex. images). Is it possible to use this method for this? How would it work in the form view if i want to have multiple file uploads for each one of them?
Thanks a lot

Can you _remove_ a variant from ActiveStorage?

Using ActiveStorage in Rails, variants are added "on demand" as you create them. They are now persisted in some storage (disk, S3, google cloud, etc).
If you realize some variants are un-needed after all and change your code to not invoke them... I think they'll still be sitting persisted in storage.
How does one clean these up, so they're not taking up storage space? I can't find any ActiveStorage API to remove variants.
You can delete a file from ActiveStorage services (disk, s3 etc.) with its key . And a variant's key is identified by its blob and transformation. Therefore you can delete a specific variant like this:
avatar = user.avatar
variant = avatar.variant(resize: '100x100')
avatar.service.delete(variant.key)
If the cost of generating all variants again is acceptable or if you need to invalidate most variants, then you can simply delete the variants folder. Rails will generate the variants again when needed.

How do I throw away the original image file and just keep the resizes with Paperclip and Rails 3?

I am using Paperclip to, among other things, allow a registered user to upload an avatar for use on thier profile. I want to store a big and small version of the image which will be rmagicked to standard sizes. Now, what i want to do, is store these two standard sizes (:normal and :tiny, for example.) but I do not want to store the :original.
This will be nice for several reasons as I will never display or use any version than the two standard (re)sizes.
What is your reasoning for wanting to delete the files? File storage is so cheap now, that it's not really a valid reason anymore.
I would advise against deleting the original files. If you ever decide you want to resize the files using Paperclip's rake tasks, you will need the originals.
I can't think of an way to do that with Paperclip directly, but you could remove the original manually after creating the record. An example could look like this:
class Photo
has_attached_file :photo
after_create :destroy_original
protected
def destroy_original
# photo.url will look something like /system/photos/1/original.png
File.unlink("#{Rails.root}/public#{self.photo.url}")
end
end

How to give users a file storage limit?

I'm working on a web application right now using Rails and wanted to see if anyone knew of a good way to keep track of file storage limits? We want to give users a specific amount of room that they can use to upload files and we are using paperclip for storage on Amazon S3. Any thoughts? Thanks
One of the optional columns you can use with paperclip is the OBJECT_file_size which you can sum up like
# Assuming a user has many files relationship
#user.uploads.sum(:image_file_size)
As to the actually imposing the cap, I'd recommend creating a custom validation on whatever the file model is so that you can pass errors and issues back to the client.
Paperclip stores the file size.
So you could just, for one user, do something like :
def used_file_size
self.files.sum(:document_file_size)
end
You'll then have the total size of every user's documents.
You can then add a max size limit and not validate the document's upload if the user has reached that size.

Storing pdfs in Rails app

How would I store a pdf in my Rails app so that users can access/download them?
In my migration, would i i use the datatype t.binary?
Then in my controller/view, how should I present this data?
The answer here is probably it depends. Depends on the size and number of PDFs, where users have to be logged in to view them etc.
If you have lots of large PDFs, its probably not a good idea to store them in the database - just store them on the filesystem and store a file location in the database model.
If you do want to store them in the database, then using a binary column is the way to go.
If users don't have to be logged in to download the PDF's, then you could just place them into the public folder (inside a subfolder) and generate links to them for download - then your controller would only need to generate a link to a static PDF file, and the front end webserver will serve them up automatically without using a Rails process.
I've heard good things about Paperclip, which is a way of transparently handling this stuff - storing files on the filesystem, with a reference to that file location in the database.
This question probably isn't rails specific, but probably best way to do it is to generate the pdf and store them on your filesystem, then in your database keep a record of filenames and a date of when they were created (to delete old ones if you need to).

Resources