我有一个上传器,我已经实现了#filename方法来获取自定义和唯一的文件名,但在保存文件之前看起来这个方法被忽略了(我上传到带有gem gem的rackspace)
以下是上传者:
class MyImageUploader < CarrierWave::Uploader::Base
include CarrierWave::RMagick
...
def filename
if original_filename.present?
"#{secure_token}.#{file.path.split('.').last.downcase}"
else
super
end
end
...
private
def secure_token
var = :"@#{mounted_as}_secure_token"
model.instance_variable_get(var) or model.instance_variable_set(var, SecureRandom.uuid)
end
end
这是控制台中的一个测试(我正在测试一个大文件名的问题,我认为这个问题是用自定义#filename方法解决的):
1.9.3-p392 :002 > f = File.open('/Users/myuser/Desktop/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.png')
=> #<File:/Users/myuser/Desktop/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.png>
1.9.3-p392 :003 > my_model_instance.image = f
Errno::ENAMETOOLONG: File name too long - /Users/myuser/app/tmp/uploads/20130509-1723-16769-8458/thumb_lolcat4lolcat4lolcat4lolcat4lolcat4lolcat4lolcat4lolcat4lolcat4lolcat4lolcat4lolcat4lolcat4lolcat4lolcat4lolcat4lolcat4lolcat4lolcat4lolcat4lolcat4lolcat4lolcat4lolcat4lolcat4lolcat4lolcat4lolcat4lolcat4lolcat4lolcat4lolcat4lolcat4lolcat4lolcat4lolcat.png
from /Users/myuser/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/fileutils.rb:1371:in `initialize'
from /Users/myuser/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/fileutils.rb:1371:in `open'
from /Users/myuser/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/fileutils.rb:1371:in `block in copy_file'
from /Users/myuser/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/fileutils.rb:1370:in `open'
from /Users/myuser/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/fileutils.rb:1370:in `copy_file'
from /Users/myuser/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/fileutils.rb:477:in `copy_file'
from /Users/myuser/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/fileutils.rb:396:in `block in cp'
from /Users/myuser/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/fileutils.rb:1515:in `block in fu_each_src_dest'
from /Users/myuser/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/fileutils.rb:1531:in `fu_each_src_dest0'
from /Users/myuser/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/fileutils.rb:1513:in `fu_each_src_dest'
from /Users/myuser/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/fileutils.rb:395:in `cp'
from /Users/myuser/.rvm/gems/ruby-1.9.3-p392@app/gems/carrierwave-0.6.2/lib/carrierwave/sanitized_file.rb:205:in `copy_to'
from /Users/myuser/.rvm/gems/ruby-1.9.3-p392@app/gems/carrierwave-0.6.2/lib/carrierwave/uploader/cache.rb:121:in `block in cache!'
from /Users/myuser/.rvm/gems/ruby-1.9.3-p392@app/gems/carrierwave-0.6.2/lib/carrierwave/uploader/callbacks.rb:17:in `with_callbacks'
from /Users/myuser/.rvm/gems/ruby-1.9.3-p392@app/gems/carrierwave-0.6.2/lib/carrierwave/uploader/cache.rb:112:in `cache!'
from /Users/myuser/.rvm/gems/ruby-1.9.3-p392@app/gems/carrierwave-0.6.2/lib/carrierwave/uploader/versions.rb:231:in `block in cache_versions!'
from /Users/myuser/.rvm/gems/ruby-1.9.3-p392@app/gems/carrierwave-0.6.2/lib/carrierwave/uploader/versions.rb:216:in `each'
from /Users/myuser/.rvm/gems/ruby-1.9.3-p392@app/gems/carrierwave-0.6.2/lib/carrierwave/uploader/versions.rb:216:in `cache_versions!'
from /Users/myuser/.rvm/gems/ruby-1.9.3-p392@app/gems/carrierwave-0.6.2/lib/carrierwave/uploader/callbacks.rb:18:in `block in with_callbacks'
from /Users/myuser/.rvm/gems/ruby-1.9.3-p392@app/gems/carrierwave-0.6.2/lib/carrierwave/uploader/callbacks.rb:18:in `each'
from /Users/myuser/.rvm/gems/ruby-1.9.3-p392@app/gems/carrierwave-0.6.2/lib/carrierwave/uploader/callbacks.rb:18:in `with_callbacks'
from /Users/myuser/.rvm/gems/ruby-1.9.3-p392@app/gems/carrierwave-0.6.2/lib/carrierwave/uploader/cache.rb:112:in `cache!'
from /Users/myuser/.rvm/gems/ruby-1.9.3-p392@app/gems/carrierwave-0.6.2/lib/carrierwave/mount.rb:315:in `cache'
from /Users/myuser/.rvm/gems/ruby-1.9.3-p392@app/gems/carrierwave-0.6.2/lib/carrierwave/mount.rb:173:in `image='
from /Users/myuser/.rvm/gems/ruby-1.9.3-p392@app/bundler/gems/carrierwave-mongoid-28a9b718d42b/lib/carrierwave/mongoid.rb:39:in `image='
所以我认为我需要在保存之前修改文件名以使自定义文件名完全正常工作,因为实际上文件只有在上传到Rackspace后才重命名(只有文件名不是那么大)
答案 0 :(得分:4)
上传在缓存步骤(在本地发生)而不是保存步骤(在雾中发生)中爆炸。阅读the source,看起来carwave有一个方法,你可以覆盖这样做:full_original_filename
如果您尝试解决的问题是缩短缓存的文件名,则可能会有以下情况。如果您想稍后更改该文件名,则还需要使用official solution for that。
MAX_FILENAME_LENGTH = 64 # arbitrary limit, your milage may vary
def full_original_filename
filename = super
return filename if !filename.present?
extension = File.extname(filename)
basename = File.basename(filename, extension)[0...MAX_FILENAME_LENGTH]
basename + extension
end