1

I want to use the same file name but a different directory for a version of my uploaded file.

upload file imageName.jpg should create
   uploads/drawing/2018/imageName.jpg    #which is the original file
   uploads/thumbs/2018/imageName.jpg     #a 120 x 100 scaled version of the original file

In my: class DrawingUploader < CarrierWave::Uploader::Base

I override the store_dir method, which correctly stores my downloaded file, imageName.jpg in uploads/drawings/2018

def store_dir
    "uploads/drawings/2018"
end

Then I create a thumbnail version and I want to specify the file name so that it is the same as the original (without "thumb" added to it) and a directory for it. The file name works with full_filename but store_dir doesn't.

version :thumb do
  process resize_to_fit: [120, 100]

  def store_dir (for_file = model.drawing.file)
    "uploads/thumbs/2018"
  end
  def full_filename (for_file = model.drawing.file)
    "imageName.jpg"
  end
end

This stores a 120x100 file at uploads/drawings/2018/imageName.jpg

It is still using the store_dir (drawings not thumbs) defined before version.

More info from my tests:

if the file uploads/drawings/2018/imageName.jpg already exists when uploader is called, the store_dir defined in version works correctly, and places the file in uploads/thumbs/2018/imageName.jpg

if versions only uses full_filename, but I include the path for the specified file name it places that path inside of the path last used by uploader so:

version :thumb do
   process resize_to_fit: [120, 100]

   def full_filename (for_file = model.drawing.file)
     "uploads/thumbs/2018/imageName.jpg"
   end
end

creates a file at uploads/drawings/2018/uploads/thumbs/2018/imageName.jpg

and if I try to make the path relative it correctly creates the directories but never writes the version file -- the folders are simply empty

version :thumb do
 process resize_to_fit: [120, 100]

 def store_dir (for_file = model.drawing.file)
   "../../uploads/thumbs/2018"
 end
 def full_filename (for_file = model.drawing.file)
   "imageName.jpg"
 end
end

creates an empty directory at uploads/drawings/2018 the file imageName.jpg is not written

ESim
  • 11
  • 2

1 Answers1

0

It appears to be a carrierwave issue with caching. I found this hack which was super helpful: https://github.com/carrierwaveuploader/carrierwave/issues/1861

What I used follows:

def store_dir
  if version_name
    @dirName = "thumbs"
  else
    @dirName = "drawings"
  end

  "uploads/#{@dirName}/2018"
end

def filename
  "newImage.jpg" if original_filename.present?
end

def full_filename(for_file)
  if model.new_record?
    super(for_file)
  else
    for_file
  end
end

# Create different versions of your uploaded files:
version :thumb do
  process resize_to_fit: [120, 100]
end
ESim
  • 11
  • 2