I have thousands of images uploaded with the following filename structure.
def filename
"#{model.id}" + "-v#{timestamp}" + "-" + Category.find("#{model.category_id}").slug + "-" + CategoryItem.find("#{model.category_item_id}").slug + ".png" if original_filename.present?
end
def timestamp
var = :"@#{mounted_as}_timestamp"
model.instance_variable_get(var) or model.instance_variable_set(var, Time.now.to_i)
end
I need to create thumbs for all the images to speed up certain page load times. I've setup the thumb version in the uploader file
# Create different versions of your uploaded files:
version :thumb do
process :resize_to_limit => [110, nil]
end
but now when I run: CategoryItem.each {|item| item.image_value.recreate_versions! if item.image_value? }
The thumb is created but the filename of the thumb image isn't the same as the original file with beyond the added 'thumb_' at the start of the filename.
saved filename: 1-v1474175808-shoes-runners.png
thumb filename: thumb_1-v1472111618-shoes-runners.png (different timestamp)
CategoryItem.find(1).image_value_url(:thumb): thumb_1-v1474175808-shoes-runners.png (original filename timestamp)
So calling image_tag CategoryItem.find(1).image_value_url(:thumb)
looks for a file that doesn't exist.
How can I run recreate_versions
and have the thumb filename be the same as the original saved filename but with 'thumb_' added to the front without removing the timestamp?
Update:
I now have the following from finding this https://github.com/carrierwaveuploader/carrierwave/wiki/How-to:-Customize-your-version-file-names
version :thumb do
process :resize_to_limit => [110, nil]
def full_filename(for_file = model.image_value.file)
'thumb_' + File.basename(model.image_value.path).to_s
end
end
my problem is that even though I'm calling the direct filename File.basename(model.image_value.path).to_s
, when I run recreate_versions
it still saves the file with the current timestamp in it rather than grabbing the original filename. I thought maybe the filename is changing in the DB and its grabbing the new filename but it is staying the same as expected so I have no idea why it isnt grabbing the direct value from the db and not making a random timestamp in the name.
Also I though maybe def full_filename(for_file = model.image_value.file)
isnt working but if I change it to
version :thumb do
process :resize_to_limit => [110, nil]
def full_filename(for_file = model.image_value.file)
'thumb_' + "random-text"
end
end
the thumb image is saved as 'thumb_random-text' so it is running through that code