1

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

Community
  • 1
  • 1
  • So you are looking for something like this as a result: three files: bianca-001.jpg, small_bianca-001.jpg, medium_bianca-001.jpg? – Elvn Jun 22 '16 at 21:05
  • That's right but I need to send 'bianca-001' myself or knowing it before Carrierwave starts uploading the files. – Lucas Quintana Jun 23 '16 at 11:30
  • Yes. Not a problem. But, you need to post some code in order for someone to assist you. – Elvn Jun 24 '16 at 01:16
  • Thanks foo! I just forgot. – Lucas Quintana Jun 24 '16 at 12:35
  • 1
    uploader = MyUploader.new -- I think this is a problem that may stop you from what you want to do. Carrierwave, according to their docs requires an ActiveRecord Model, and the use of their method .mount_uploader in order for their callbacks to work. I suspect it's where the filename versioning is handled. This is not an answer, which is why I didn't make it an answer. You are using Carriewave in a custom way; you may have to edit the Carrierwave code to make it work without an associated ActiveRecord model. Maybe there is another upload gem that is designed for passthrough uploads. – Elvn Jun 24 '16 at 16:50
  • Hi foo! I did it that way because I found this: [link](http://stackoverflow.com/questions/20068620/upload-file-to-s3-using-carrierwave-without-a-model-is-it-possible). Nevertheless I agree with you, Maybe Carrierwave is not what I need, I used it because I already had it in my project – Lucas Quintana Jun 25 '16 at 10:45

0 Answers0