carrierwave_direct icon indicating copy to clipboard operation
carrierwave_direct copied to clipboard

Breaks version URL format versus vanilla CarrierWave

Open lansing opened this issue 12 years ago • 6 comments

We've been using vanilla (non-direct) CarrierWave in production for a while. Recently I tried using this gem for an upload form. After including the module in the Uploader class, the URLs for the various versions of my existing images are now broken. It seems that when this gem's module is included, the generated version key format looks like "blahblah_versionName.jpg", while with vanilla CarrierWave they look like "versionName_blahblah.jpg".

In the end it wasn't a huge deal to fix (had to recreate_versions! for all the images and save the AR model) but nevertheless this was a nasty surprise. Is there a reason why this gem needs to change the versions' key format?

lansing avatar Feb 11 '13 22:02 lansing

+1

amnwebmaster avatar May 07 '13 00:05 amnwebmaster

@lansing This was implemented to account for the filename being stored with a guid, though I'd be happy to accept any suggestions for workarounds.

nddeluca avatar Jul 01 '13 23:07 nddeluca

Opps wrong button. Will add better documentation so it's more apparent that this gem will change existing behavior.

nddeluca avatar Oct 08 '13 02:10 nddeluca

I fixed that in my uploader with

def full_filename(for_file)
  if for_file == model.file_name && version_name
    dirname  = File.dirname(for_file)
    basename = File.basename(for_file)

    File.join(dirname, "#{version_name}_#{basename}")
  else
    for_file
  end
end

HTH

stephanschubert avatar May 27 '14 11:05 stephanschubert

Me too all previously uploaded image urls break. recreate_versions! fixes then.

jobinthepast avatar Apr 20 '15 19:04 jobinthepast

Hi, all!

I'm relatively new to Rails and am working on a project at work that had this issue.

For more context, this project has an image mounted on a User model:

mount_uploader :image, UserImageUploader

and defines a store_dir in UserImageUploader:

def store_dir
  "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end

and creates a thumb version with (I believe) MiniMagick

version :thumb do
  process resize_to_fill: [50, 50]
end

and has a serializer that returns the thumb version:

def image_thumb_url
  object.image_url(:thumb)
end

The value stored in the database is: my_image.jpg and the resulting value for image_thumb_url is currently: https://example.s3.amazonaws.com/uploads/user/image/1/thumb_my_image.jpg

I added carrierwave_direct and implemented it by changing storage :fog to include CarrierWaveDirect::Uploader and left everything else as-is.

Now, the serializer responds with: https://example.s3.amazonaws.com/uploads/user/image/1/my_image_thumb.jpg 🚫

I opened my Rails console and ran:

my_user = User.find(1)
my_user.image.recreate_versions!

and the serializer still responds with https://example.s3.amazonaws.com/uploads/user/image/1/my_image_thumb.jpg 🚫 for that image.

When I call my User controller to update the user's image with a new upload, the database now stores 1/my_new_image.jpg (id in the filename) and the resulting URL from the serializer is now: https://example.s3.amazonaws.com/uploads/user/image/1/1/my_new_image_thumb.jpg

That's fine for the new image (even with the redundant /1/1), but existing images are still broken (due to the version prefix/suffix swap).

I must be running recreate_versions wrong? Any ideas?

claudiorivera avatar Feb 27 '23 15:02 claudiorivera