Rename existing CarrierWave files

How do you rename existing images with CarrierWave? (This question is close, but actually not the same: Renaming downloaded files using Carrierwave ) For example, if model.image.url is foo-bar-jpg , how can I rename the file and the corresponding database field to foo-bar.jpg ?

model.image.recreate_versions! will create new files, but in the database it will not change the model model field.

+6
source share
3 answers

I used the technique described here: How to assign a remote Carrierwave file?

This may not be the best way, but it worked for me. My deleted file just turned out to be the old file name / path.

First, I modified the Carrierwave bootloader to have the new file name style I wanted. Then I wrote a rake task to iterate through the records and update such files:

 model.remote_image_url = old_image_url model.save! 

This will download the existing file again, setting the name / path based on your updated bootloader (and recreate all versions). I have not yet dealt with the problem of cleaning old files, I'm not sure how this will work if your store_dir is the same (mine has changed too).

Be sure to carefully test a few entries before running around the table, which is easy to make of things. Keep in mind that modifying your store_dir will break all of your searches for existing files.

+1
source

the Carrierwave locomotive branch seems to support renaming - a specific file.

There is a file rename mainly when converting file types, but without renaming support:

 "This should help with situations where a file has multiple versions, and at least one of the versions is of a different format than the master version." 
+2
source

Found this, it seems outdated, but can help someone: https://github.com/stvkoch/carrierwave_single_store

0
source

Source: https://habr.com/ru/post/904570/


All Articles