【问题标题】:CarrierWave Add ActionDispatch::Http::UploadedFile After Model ProcessingCarrierWave 模型处理后添加 ActionDispatch::Http::UploadedFile
【发布时间】:2014-09-15 06:19:37
【问题描述】:

我有一个Image 模型,它需要我在保存图像之前设置一些数据,因为我根据模型中的一些数据确定将图像上传到哪里以及命名它的名称。现在我正在尝试做:

# ImagesController sets up data
img = Image.new
img.imageable = user
phashion_img = Phashion::Image.new(file.path)
img.image_hash = phashion_img.fingerprint.to_s
img.batch = img.latest_batch + 1
img.extension = 'jpg'
img.height = 640
img.width = 640

# ImageUploader uses it
version :original do
  def full_filename
    "#{model.image_hash}/orig/#{alnum_encode(model.imageable.id)}.#{model.extension}"
  end
end

我在尝试将上传的图片传递到上传器时遇到了问题。

img.uploader.store! file 给我错误wrong number of arguments (1 for 0) img.uploader.store! file.tempfile 给我You are not allowed to upload "" files, allowed types: jpg, jpeg, gif, png

供参考,file 是:

#<ActionDispatch::Http::UploadedFile:0x00000109ccdb70 @tempfile=#<Tempfile:/var/folders/lx/xk8vzr4s0fdd_m5w0syftfl80000gn/T/RackMultipart20140723-28731-1b5weu8>, @original_filename="20140522_164844.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"user[images_attributes][0][uploader]\"; filename=\"20140522_164844.jpg\"\r\nContent-Type: image/jpeg\r\n">

这将使 file.tempfile:

#<Tempfile:/var/folders/lx/xk8vzr4s0fdd_m5w0syftfl80000gn/T/RackMultipart20140723-28731-1b5weu8>

有什么方法可以将ActionDispatch::Http::UploadedFileTempfile 这些对象中的任何一个传递给Carrierwave 上传器?我可以对它们进行一些转换,以便上传者可以接受其中之一?

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-4 carrierwave image-uploading


    【解决方案1】:

    由于我的用例与 CarrierWave 或任何其他图像上传 gem 的预期相差甚远,因此我最终推出了自己的解决方案,使用 AWS-SDK gem 上传到 S3。它非常具体,但我会发布它的要点,以防它对其他人有所帮助。

    # Image processing
    magick = Magick::Image.from_blob( file.read )[0]
    
    if !["JPG", "JPEG", "GIF", "PNG"].include? magick.format
      # Invalid format specified
    end
    
    orig = Image.new
    orig.imageable = user
    phashion_img = Phashion::Image.new(file.path)
    orig.image_hash = phashion_img.fingerprint.to_s
    orig.batch = orig.latest_batch + 1
    orig.extension = magick.format.downcase
    filename = "#{orig.image_hash}/orig/#{Alnum.encode(orig.imageable_id)}.#{orig.extension}"
    orig.height = magick.rows
    orig.width = magick.columns
    orig.size = 'orig'
    
    # Upload to S3
    s3 = AWS::S3.new
    uploaded_name = Rails.root.to_s+filename.gsub('/', '_')
    magick.write( uploaded_name ) # Save file to filesystem so I can upload to S3
    s3.buckets[bucket_name].objects[filename].write( open(uploaded_name), :acl => :public_read )
    FileUtils.rm uploaded_name # Remove so it's not taking up space
    
    # Crop
    cropped = magick.crop(x.to_i, y.to_i, w.to_i, h.to_i, true) # Discard offset data to avoid borders
    
    # Create Profile
    # Resize_from is my own custom function to copy everything 
    # but the size into another object and resize the image
    resize_from(s3, orig, cropped, 640, 640)
    
    # Create Grid
    resize_from(s3, orig, cropped, 220, 220)
    
    # Create Thumbnail
    resize_from(s3, orig, cropped, 72, 72)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-22
      • 1970-01-01
      • 2015-07-10
      相关资源
      最近更新 更多