2015-01-16 3 views
0

carrierwave use remote_url을 사용하여 이미지를 업로드하고 있습니다.rails4, carrierwave 업로드 파일 이름이 너무 깁니다.

info.remote_image_url = "IMG_URL_HOST/IMG_PAHT/IMG_NAME" 

반환 메시지는`/Users/kai/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/fileutils.rb:1396:in에서

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 

입니다

이미지 파일의 길이가 101

이다 '초기화하지만, UTF-8의 길이를 인코딩 한 후 637

그들에게,691입니다이 해결책을 찾았습니다.

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

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>

+0

들으로 파일의 이름을 변경합니다 해결했습니다 ~ :) – kai

관련 문제