我在用户上传的图片中使用了carrierwave_direct和delayed_job。这个过程是:
我必须在文件上传后立即将作业放入队列。它将延迟几个小时(或几天)。此作业检查文件是否存在,然后检查数据库中所有最近创建的图像记录,以查看是否有任何文件附加到该文件。如果没有,则删除该文件。
最后,我想利用Carrierwave类来处理所有的雾,因为我还在使用它。
我无法在任何地方找到它,所以这是我的版本。离开这里是为了让人们在将来遇到。
答案 0 :(得分:0)
这就是我做到的。
def new
# STEP 1: Show upload dialog that user can drop an image unto
@image = current_user.portfolio.images.new.images
# Status 201 returns an XML
@image.success_action_status = "201"
end
def crop
# STEP 2: A file is now on Amazon S3, but no record exists in the database yet. Make the user crop the image and enter a title.
# Meanwhile, also setup a delayed job that will later check if this step has been completed.
# Note: the crop view gets retrieved and inserted using ajax in images.js.coffee
@image = Image.new(key: params[:key])
Delayed::Job.enqueue ImageDeleteIfUnattachedJob.new(params[:key]), 0, 15.minute.from_now.getutc
render :partial => "images/crop.html.erb", :object => @image
end
ImageDeleteIfUnattachedJob = Struct.new(:key) do
def perform
# Do any of the images created in the last week match the key that was passed in?
# If not, the user probably went through the upload process, which then either went wrong or was cancelled.
unless Image.where("created_at > ?", 1.week.ago).order('created_at DESC').any? { |image| key == image.images.path }
# We spawn these to let Carrierwave handle the Fog stuff.
@uploader = ImageUploader.new
@storage = CarrierWave::Storage::Fog.new(@uploader)
@file = CarrierWave::Storage::Fog::File.new(@uploader, @storage, key)
if @file.exists?
# Indeed, the file is still there. Destroy it.
@file.delete
else
return true
end
end
end
def max_attempts
3
end
end