0

当我尝试上传名称中包含 UTF-8 字符的文件(例如西里尔符号)时,我收到回形针引发的此错误:

[2012/12/11 17:01:45] (INFO) 26707 Command :: identify -format %wx%h '/tmp/Знімок екрана з 2012-09-18 12:49:4220121211-26707-4evsj6.png[0]'
[2012/12/11 17:01:45] (INFO) 26707 [paperclip] An error was received while processing: #<Paperclip::Errors::NotIdentifiedByImageMagickError: /tmp/Знімок екрана з 2012-09-18 12:49:4220121211-26707-4evsj6.png is not recognized by the 'identify' command.>

然而,识别命令成功通过:

$ identify -format %wx%h ~/Картинки/Знімок\ екрана\ з\ 2012-09-07\ 15\:45\:48.png 
1920x1080

其他文件(名称 like IMG_0286.JPG)也通过了。

什么可能导致此问题,我该如何解决?

4

1 回答 1

0

找到了解决方案。这是一个棘手的问题,但它有效。创建了一个补丁paperclip-3.3.1

diff -r --unidirectional-new-file paperclip-3.3.1/lib/paperclip/geometry.rb paperclip-3.3.1-my/lib/paperclip/geometry.rb
23c23
<                      Paperclip.run("identify", "-format %wx%h :file", :file => "#{file_path}[0]")
---
>                      Paperclip.run("identify", "-format %wx%h :file", :file => file_path)
31c31
<         raise(Errors::NotIdentifiedByImageMagickError.new("#{file_path} is not recognized by the 'identify' command."))
---
>         raise(Errors::NotIdentifiedByImageMagickError.new("#{file_path} is not recognized by the 'identify' command. (from #{file.inspect})"))
diff -r --unidirectional-new-file paperclip-3.3.1/lib/paperclip/thumbnail.rb paperclip-3.3.1-my/lib/paperclip/thumbnail.rb
77c77
<         success = convert(parameters, :source => "#{File.expand_path(src.path)}#{'[0]' unless animated?}", :dest => File.expand_path(dst.path))
---
>         success = convert(parameters, :source => "#{File.expand_path(src.path)}#{'[0]' if animated?}", :dest => File.expand_path(dst.path))
110c110,111
<       raise Paperclip::Error, "There was an error running `identify` for #{@basename}" if @whiny
---
>       #raise Paperclip::Error, "There was an error running `identify` for #{@basename}" if @whiny
>       return false

似乎回形针只是引发异常而不是返回truefalse- 没有任何异常处理。这是它的主要失败。

或者猴子补丁:

module Paperclip
  class Geometry
    def self.from_file file
      file_path = (file.respond_to?(:path) ? file.path : file) #.gsub(/\s/, '\\\\\1')
      raise(Errors::NotIdentifiedByImageMagickError.new("Cannot find the geometry of a file with a blank name")) if file_path.blank?
      geometry = begin
        silence_stream(STDERR) do
          Paperclip.run("identify", "-format %wx%h :file", :file => file_path)
        end
      rescue Cocaine::ExitStatusError
        ""
      rescue Cocaine::CommandNotFoundError => e
        raise Errors::CommandNotFoundError.new("Could not run the `identify` command. Please install ImageMagick.")
      end
      parse(geometry) ||
          raise(Errors::NotIdentifiedByImageMagickError.new("#{file_path} is not recognized by the 'identify' command. (from #{file.inspect})"))
    end
  end

  class Thumbnail
    def make
      src = @file
      dst = Tempfile.new([@basename, @format ? ".#{@format}" : ''])
      dst.binmode

      begin
        parameters = []
        parameters << source_file_options
        parameters << ":source"
        parameters << transformation_command
        parameters << convert_options
        parameters << ":dest"

        parameters = parameters.flatten.compact.join(" ").strip.squeeze(" ")

        success = convert(parameters, :source => "#{File.expand_path(src.path)}#{'[0]' if animated?}", :dest => File.expand_path(dst.path))
      rescue Cocaine::ExitStatusError => e
        raise Paperclip::Error, "There was an error processing the thumbnail for #{@basename}" if @whiny
      rescue Cocaine::CommandNotFoundError => e
        raise Paperclip::Errors::CommandNotFoundError.new("Could not run the `convert` command. Please install ImageMagick.")
      end

      dst
    end

    protected

    def identified_as_animated?
      ANIMATED_FORMATS.include? identify("-format %m :file", :file => "#{@file.path}[0]").to_s.downcase.strip
    rescue Cocaine::ExitStatusError => e
      #raise Paperclip::Error, "There was an error running `identify` for #{@basename}" if @whiny
      return false
    rescue Cocaine::CommandNotFoundError => e
      raise Paperclip::Errors::CommandNotFoundError.new("Could not run the `identify` command. Please install ImageMagick.")
    end
  end
end
于 2012-12-19T13:23:31.593 回答