Carrierwave rename uploaded images - ruby-on-rails

I am using CarrierWave for uploading images. I also have some image cropping that happens later. This process includes sending of the cropped-area dimensions from the client(javascript) and then recreate a cropped image on the server. After this I would like to rename all the version filenames.
Is it possible after the cropping process, I can rename all the various versions. I do not wish to recreate the versions but rather just rename the already existing versions.

You can rewrite method in your uploader
def filename
"new_name_for_file" if file
end

Related

Processing file before upload using ActiveStorage

How would I process a file before it's uploaded using activestorage. I need to be able to modify an svg file's content before it actually gets uploaded to S3. Can't seem to find any callbacks.
There is no way to do this natively with ActiveStorage. It's the major drawback with using ActiveStorage.
As far as I know, the only way to modify an upload is to create a variant of the original upload after it is created...which creates a (completely different) variant image based on the image that was originally uploaded.
ActiveStorage is easy to setup but, after using it with a few applications, Carrierwave..etc seem like better options.
In addition, if you want to upload in a background job, ActiveStorage is a pain.

Upload two versions of an image (compressed and uncompressed) using CarrierWave in Rails

I am using Carrierwave and S3 to store images uploaded by the user in my rails app.
I want to store 2 versions of the image - one is the original, uncompressed image and the other is its compressed version. How do I do that?
You can add to your uploader:
version :name_of_your_version do
process resize_to_fit: [200,200]
end
Inside the block you can pass whatever processes you want and these will be executed and saved on a version called name_of_your_version.

Local file upload: File.open or StringIO

My application allows a user to upload an image and then create different versions of that image (e.g. aligned and cropped with another image). The intermediate step is that I need to copy the uploaded file to another object and process it. The easiest way to do this is to upload it locally.
From the carrierwave wiki, they suggest using a modified version of StringIO.
On the carreirwave readme, they also suggest using File.open (something like obj.image=File.open('path_to_file').
I've also found references to using fixture_file_upload from ActionDispatch::TestProcess (usually in testing, but I'm unsure why it is restricted to that environment).
Can anyone give me a good explanation on the pros and cons (if any) of using these methods?
Thanks.
I just discovered one major difference, at least in the context of carrierwave. If you use carrierwave with the move_to_cache option set to true and mount your upload column with File.open, the file given to File.open will be moved, while with StringIO, it won't.

paperclip gem the file size column isn't correct

I'm using the paperclip gem to store files (photo, video, audio, etc). The file size is set to zero for most of the file uploads. A couple of the jpeg uploads have the right file size. Anyone seen this before?
This is an old question, but I think it's got something to do with what happens when the file is saved. Paperclip runs imagemagik in the background to create the thumbnails and so on and I think that's when it gets the file size too.
Don't know for sure, but suspect that you've set up the options so it doesn't need to create them so it's never calling the utilities that get the sizes and so on and pulling those values back into the database.

Rails: attachment_fu plugin, convert PDF uploads to image

Yo,
I've got attachment_fu set up on my server, and it works just fine. My goal, however, is to be able to upload PDFs and have them convert to image (then use all the pre-built attachment_fu options like resize and thumb produce the desired images). What currently happens is that PDFs upload and remain PDFs, whereas images go through all the motions of resizing and whatnot.
How can I get the functionality I want? Is there a simple way to go about doing this using the tools I already have, or should I just manually invoke RMagick to do the initial conversion? If so, how can I do that and then pass off the file to attachment_fu for handling?
Thanks,
--Matchu
For the oldest unanswered rails question: https://stackoverflow.com/a/69804/156561 his answer is probably very similar to get you in the door to use Attachment_fu which I'm sure you've by now migrated off.

Resources