dwilkie / carrierwave_direct

Process your uploads in the background by uploading directly to S3
MIT License
839 stars 181 forks source link

Breaks version URL format versus vanilla CarrierWave #61

Open lansing opened 11 years ago

lansing commented 11 years ago

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?

amnwebmaster commented 11 years ago

+1

nddeluca commented 11 years ago

@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 commented 11 years ago

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

stephanschubert commented 10 years ago

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

jobinthepast commented 9 years ago

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

claudiorivera commented 1 year ago

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?