Carrierwave: Move version name to end of filename, instead of front
Asked Answered
A

3

9

Currently with Carrierwave, after uploading a file like foo.png when creating different versions like so:

class ImageUploader < CarrierWave::Uploader::Base
  include CarrierWave::MiniMagick
  storage :fog
  def store_dir
    "#{model.class.to_s.underscore}/#{model.id}"
  end

  version :thumb do
    process :resize_to_fit => [500, 500]
  end
end

that results in the files being uploaded as:

thumb_foo.png
foo.png

I want to move "thumb" to the end of the filename for SEO reasons. Based on their docs here I added:

  def full_filename(for_file)
    if parent_name = super(for_file)
      extension = File.extname(parent_name)
      base_name = parent_name.chomp(extension)
      [base_name, version_name].compact.join("_") + extension
    end
  end

  def full_original_filename
    parent_name = super
    extension = File.extname(parent_name)
    base_name = parent_name.chomp(extension)
    [base_name, version_name].compact.join("_") + extension
  end

The docs say this should result in:

foo_thumb.png
foo.png

However, I end up actually getting the following:

thumb_foo_thumb.png
foo.png

Any idea what I'm doing wrong?

Anele answered 4/1, 2017 at 6:0 Comment(1)
Have you tried this customize version file names? It's also move version to end of file namePrismoid
E
5

Simply use #full_filename under the version block:

class AvatarUploaer < CarrierWave::Uploader::Base
  include CarrierWave::MiniMagick

  storage :file

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

    def full_filename(for_file = model.logo.file)
      parts     = for_file.split('.')
      extension = parts[-1]
      name      = parts[0...-1].join('.')
      "#{name}_#{version_name}.#{extension}"
    end
  end
end

The result will be following:

/Users/user/app/uploads/1x1.gif
/Users/user/app/uploads/1x1_thumb.gif
Evvoia answered 8/1, 2017 at 16:2 Comment(4)
I appreciate the post I just gave this a try and it's not working as expected. The file names are unchanged. I was sure to restart rails etc... however content_ and thumb_ are still being added to the front of the file name... I'm using Using carrierwave 1.0.0 -- ideas?Anele
Also, my storage is set to storage :fogAnele
My mistake, I was looking at the wrong uploader. I appreciate all your help!Anele
@Anele Ty for the interesting question. And I glad it helped you ;)Evvoia
B
0

If you have a lot of versions, the accepted answer can get a little tedious.

I ended up overriding full_filename for everything instead of in each individual version definition. It works fine. This is for Carrierwave 1.0

photo_uploader.rb

# Override the filename of the uploaded files:
def full_filename(name)
  "#{File.basename(name, '.*')}_#{version_name || 'original'}#{File.extname(name)}"
end

I'm using the built in File.basename and File.extname methods instead of doing it manually as seen in the accepted answer (although that's where I started and that code works fine too).

Note: I wanted to add "original" to the unversioned upload just so my directory listing looked cleaner. That part could be removed fairly easily.

foo_mobile.jpg

foo_original.jpg

foo_square.jpg

Bailsman answered 5/4, 2017 at 21:10 Comment(0)
P
-2

In the current version of CarrierWave if you have an uploader defined like this:

class LogoUploader < CarrierWave::Uploader::Base 
  # ... 
  def filename 
    "original_#{model.logo.file.extension}" if original_filename 
  end 
  version :small do 
    process :resize_to_fit => [190, 190] 
    process :convert => 'png' 
  end 
  version :icon do 
    process :resize_to_fill => [50, 50] 
    process :convert => 'png' 
  end 
  # ... 
end

and attach a file name somefile.jpg, you will end up with files named original.jpg, original_small.png and original_icon.png respectively.

Pupil answered 4/1, 2017 at 6:4 Comment(8)
I don't think that answers the question unless I'm missing something....Anele
That has been written on the official site of carrier wave and what you want is the name of file, that code will do the same. Are you not able to getting the way to solve your problem now ?Pupil
What I want is for the version "thumb" to not be in the default front location thumb_foo.png but at the end like so: foo_thumb.pngAnele
and attach a file name somefile.jpg, you will end up with files named original.jpg, original_small.png and original_icon.png respectively.Pupil
"original_#{model.logo.file.extension}" on the place of the original you can put your file name by parsing from the original_file.Pupil
Sorry, I don't followAnele
Not getting you. Did you fine it be helpful for you ?Pupil
sorry this was not helpful and doesn't answer the question :(Anele

© 2022 - 2024 — McMap. All rights reserved.