AnApprentice
AnApprentice

Reputation: 110960

Using Carrierwave remote_image_url, how can you set the filename to use?

I'm currently using Carrierwave to remotely download an image. This is working fine. What I'm trying to figure out is how to set the filename to something other than what is downloaded from the URL.

This is in my rake task:

    @desk.desk_images.create(
      :file_name_to_use => "testing",
      :remote_image_url => photo.url(size)
    )

And in my uploader base: desk_image_uploader.rb

class DeskImageUploader < CarrierWave::Uploader::Base

  include CarrierWave::MiniMagick

  storage :fog

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

  version :thumb do
    process resize_to_fill: [200, 150]
    process :quality => 90

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

  end
end

desk_image.rb

class DeskImage < ActiveRecord::Base
  belongs_to :desk

  mount_uploader :image, DeskImageUploader

  attr_accessor :file_name_to_use
end

This is not storing correctly in the db. DeskImage.image is storing the orig filename not the override I passed "testing"...

Anyone know how I can customize the filename Carrierwave stores in the db and uses?

Thank you

Upvotes: 0

Views: 918

Answers (1)

lobati
lobati

Reputation: 10215

Okay, I've stripped down your code a bit to make sure we can be sure there aren't any confounding factors. First step is moving filename out of the version block. Can you give this a try:

class DeskImageUploader < CarrierWave::Uploader::Base

  include CarrierWave::MiniMagick

  storage :fog

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

  version :thumb do
    process resize_to_fill: [200, 150]
    process :quality => 90    
  end

  def filename
    "test-#{model.id}.#{File.extname(super)}"
  end
end

Upvotes: 1

Related Questions