我使用carrierwave上传图片使用remote_url。
info.remote_image_url = "IMG_URL_HOST/IMG_PAHT/IMG_NAME"
返回消息
Errno::ENAMETOOLONG: File name too long @ rb_sysopen
- /Users/kai/rails/rails_pjt/public/uploads/tmp/1421373782-65124-7719/D-__E1_84_8B_E1_85_A1_E1_86_AB_E1_84_8C_E1_85_B5_E1_84_8B_E1_85_A7_E1_86_AB__E1_84_82_E1_85_A1_E1_86_AF_E1_84_8D_E1_85_A1_E1_84_87_E1_85_A7_E1_86_AF_E1_84_8B_E1_85_A5_E1_86_B8_E1_84_86_E1_85_AE_0423__E1_84_8B_E1_85_A9_E1_84_85_E1_85_B3_E1_84_89_E1_85_A5_E1_86_BC_E1_84_92_E1_85_A7_E1_86_BC_E1_84_8B_E1_85_AC_E1_84_80_E1_85_AA-_E1_84_80_E1_85_AE_E1_86_BA_E1_84_83_E1_85_A1_E1_86_A8__E1_84_87_E1_85_A9_E1_86_AF_E1_84_85_E1_85_B2_E1_86_B7_E1_84_8C_E1_85_B5_E1_84_87_E1_85_A1_E1_86_BC_E1_84_8B_E1_85_B5_E1_84_89_E1_85_B5_E1_86_A8__E1_84_86_E1_85_A1_E1_86_AF_E1_84_84_E1_85_A9_E1_86_BC_E1_84_80_E1_85_A1_E1_84_85_E1_85_B5_orps3_landing_05.jpg
来自/Users/kai/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/fileutils.rb:1396:in'initialize'
图像文件的长度为101
但编码后UTF-8长度为637
和他们 我找到了这个解决方案。
http://stackoverflow.com/questions/16472894/modify-filename-before-saving-with-carrierwave
但它是不同版本的图像路径
ex) file_name : D-__E1_84_8B_E1_85_A1_E1_86_AB_E1_84.png
android_version : android_D-__E1_84_8B_E1_85_A1_E1_86_.png
thumbnail_version : thumbnail_D-__E1_84_8B_E1_85_A1_E1_8.png
如何修复扩展文件名限制。
答案 0 :(得分:0)
根据this(被拒绝)拉取请求,您可以将此代码放入初始化程序并修复问题
# Monkey patch for long filenames
# @see https://github.com/carrierwaveuploader/carrierwave/pull/539/files
module CWRemoteFix
# 255 characters is the max size of a filename in modern filesystems
# and 100 characters are allocated for versions
MAX_FILENAME_LENGTH = 255 - 100
def original_filename
filename = super
if filename.size > MAX_FILENAME_LENGTH
extension = (filename =~ /\./) ? filename.split(/\./).last : false
# 32 for MD5 and 2 for the __ separator
split_position = MAX_FILENAME_LENGTH - 32 - 2
# +1 for the . in the extension
split_position -= (extension.size + 1) if extension
# Generate an hash from original filename
hex = Digest::MD5.hexdigest(filename[split_position, filename.size])
# Create a new name within given limits
filename = filename[0, split_position] + '__' + hex
filename << '.' + extension if extension
end
# Return original or patched filename
filename
end
end
# Monkeypatch downloader class using prepend
CarrierWave::Uploader::Download::RemoteFile.prepend CWRemoteFix
此修补程序会将文件重命名为<prefix-from-original-filename>__<md5-of-original-filename>.<extension-if-present>