2014-03-26 5 views
0

이전에 이미 질문했지만 이미 내가 직면 한 문제와 비슷한 것을 찾을 수 없었습니다.문서 클립을 사용하여 URL에서 이미지 가져 오기

open() 메서드를 사용하여 파일을 다운로드하고 저장합니다.

img = Image.new 
img.image = open(url) 
img.save 

오류가 발생합니다 : Paperclip error while determining content type 내가 SO에 발견,하지만이 적용되지 않습니다 그래서 나는 리눅스 머신에 오전이가있다 할

paperclip Error while determining content type: Cocaine::CommandNotFoundError in Rails 3.2.1

다른 방법을 URI.parse()을 사용하십시오. 그러나 이전에 문제가 발생 했으므로 지금은 문제가 없습니다.

전체적으로 open() 또는 URI.parse() 중 하나의 동작을 예측할 수 없었습니다. 때로는 때로는 일하지 않는 경우도 있습니다. 이 경우 사용할 수있는 가장 좋은 것은 무엇이며 무엇이 안전한 전략을 사용하지 못합니까?

+0

파일의 상단에 당신이 '오픈 URI'요구해야합니까? – fengd

답변

2

특정 파일 형식에서 비슷한 문제가 발생했습니다. 우분투 12.04와 file --mime을 사용하고있어 .doc 파일과 같이 항상 파일 유형을 찾을 수있는 것은 아닙니다.

나는 Paperclip을 수정하여 file --mime을 사용할 수있는 곳에서 사용하고, 그렇지 않은 경우 mimetype으로 되돌아갑니다. 이 같은

뭔가 :

module Paperclip 
    class FileCommandContentTypeDetector 
    private 

    def type_from_file_command 
     # -- original code -- 
     # type = begin 
     # # On BSDs, `file` doesn't give a result code of 1 if the file doesn't exist. 
     # Paperclip.run("file", "-b --mime :file", :file => @filename) 
     # rescue Cocaine::CommandLineError => e 
     # Paperclip.log("Error while determining content type: #{e}") 
     # SENSIBLE_DEFAULT 
     # end 

     # if type.nil? || type.match(/\(.*?\)/) 
     # type = SENSIBLE_DEFAULT 
     # end 
     # type.split(/[:;\s]+/)[0] 

     # -- new code -- 
     type = begin 
     Paperclip.run('file', '-b --mime :file', file: @filename) 
     rescue Cocaine::CommandLineError 
     '' 
     end 

     if type.blank? 
     type = begin 
      Paperclip.run('mimetype', '-b :file', file: @filename) 
     rescue Cocaine::CommandLineError 
      '' 
     end 
     end 

     if type.blank? || type.match(/\(.*?\)/) 
     type = SENSIBLE_DEFAULT 
     end 
     type.split(/[:;\s]+/)[0] 
    end 
    end 
end 
관련 문제