使用下面的代码,非基于模型的上传工作正常,我只需要查看如何指定filename
,并指定store_dir
。
我的Rails 4.2应用程序上传了一些远程PNG,将它们转换为JPG,然后存储在S3上,但图像与任何Model字段都没有关联。 Carrierwave版本(0.10.0)
在Carrierwave文档中,我看不到如何为要使用的上传器实例指定任意store_dir和filename。 (当绑定到模型时,通过重写filename()和store_dir()方法并访问方法中的模型参数,可以很好地记录从模型参数和/或随机标记中指定名称派生。 / p>
但是当没有关联的模型字段时,一个"如何传入"上传者的实例是要使用的文件名和store_dir吗?
# working ruby code to upload a couple of PNGs not associated with any Model
u = next_url_to_copy() # http://example.com/tom.png
uploader = SnapshotUploader.new
# HOW SPECIFY DESIRED DESTINATION FILENAME
# "tom.jpg" AND SUB-FOLDER "tom_stuff" ??
open(u) { |file| something = uploader.store!(file) }
puts "#{uploader.filename}"
# open-uri20160516-982-xxxxxx # filename created by Carrierwave
u = next_url_to_copy() # http://example.com/harry.png
uploader = SnapshotUploader.new
# HOW SPECIFY DESIRED DESTINATION FILENAME
# "harry.jpg" AND SUB-FOLDER "harry_stuff" ??
open(u) { |file| something = uploader.store!(file) }
puts "#{uploader.filename}"
# open-uri20160516-982-yyyyy # filename created by Carrierwave
# SnapshotUploader.rb
require 'carrierwave/processing/mime_types'
class SnapshotUploader < CarrierWave::Uploader::Base
include CarrierWave::RMagick
def cache_dir
"#{Rails.root}/tmp/uploads"
end
def store_dir
"#{Settings.root_picture_folder}/snapshots"
end
# Provide a default URL as a default if there hasn't been a file uploaded:
def default_url
Settings.missing_picture_flyer
end
# Process files as they are uploaded:
process :convert => 'jpg'
process :set_content_type
def set_content_type(*args)
self.file.instance_variable_set(:@content_type, "image/jpg")
end
end
上传者类似乎没有提供&#34; setter&#34;对于filename或store_dir,我也找不到直接设置的属性。重写filename()和store_dir()以使用模型参数的常用方法(当上传者与模型相关联时)不起作用,因为没有与上传者关联的模型。