We're having some trouble using Carrierwave, I will try to explain our problem.
We need to generate an endpoint in our API to upload images to AWS S3, those images are only meaningful for the frontend so we did not associate the carrierwave uploader to a model. We are handling a few image versions also.
We were able to make this work so far but we wanna do this process asynchronous so we need to pre-define a filename for the image and all its versions. We were able to sent to the uploader a pre-defined filename but the uploader only uses that filename for the original version, the version are stored just as prefixes ("small_.jpg", "medium_.jpg", ...) so they are being overwritten.
I found this about custom file names: How to: Customize your version file names and CarrierWave: Create the same, unique filename for all versioned files
Both links were useful to understand the problem, we are even using it for other uploaders but our problem here is a little bit different
If someone has worked in something similar or has an idea, I will be really helpful!
Thanks a lot!
UPDATE:
Adding some code (2016/06/24)
This is my controller code:
class MyController < ApplicationController
def upload
base64_string = params[:resource]
file_name = SecureRandom.uuid + ".jpg" #<= The filename I wanted to parameterize
# This code should be delayed =>
base64 = Base64.decode64(base64_string.partition(',').last)
image = MiniMagick::Image.read(base64)
uploader = MyUploader.new
File.open(image.path) { |file| something = uploader.store!(file) }
# <=
return file_name
end
end
My uploader code:
class MyUploader < CarrierWave::Uploader::Base
include CarrierWave::MiniMagick
storage :fog
def store_dir
"some/custom/dir/"
end
version :large do
process :resize_to_fit => [630, 450]
end
version :medium, :from_version => :large do
process :resize_to_fit => [420, 300]
end
version :small, :from_version => :large do
process :resize_to_fit => [315, 225]
end
def filename
"#{secure_token}.jpg"
end
protected
def secure_token
SecureRandom.uuid
end
end
The 'secure_token' method in the uploader is defined just as a work around. I wanna use 'file_name' defined in the controller