0

我已经获取了名为 socialstream 的 gem 的最新代码库,但我注意到 Windows 机器上的文件上传存在问题。问题似乎与下面的代码有关...

# Monkey patches to Ruby on Rails
#
# Use unix file util to prove the content type sent by the browser
class ActionDispatch::Http::UploadedFile
  def initialize_with_magic(*args, &block)
    initialize_without_magic(*args, &block)

    if (unix_file = `which file`.chomp).present? && File.exists?(unix_file)
      `#{ unix_file } -v 2>&1` =~ /^file-(.*)$/
      version = $1

      @content_type =
        if version >= "4.24"
          `#{ unix_file } -b --mime-type #{ @tempfile.path }`.chomp
        else
          `#{ unix_file } -bi #{ @tempfile.path }`.chomp =~ /(\w*\/[\w+-\.]*)/
          $1
        end
    end
  end

  alias_method_chain :initialize, :magic
end

错误如下

NoMethodError (undefined method chomp' for nil:NilClass): social_stream-base (0.22.0) lib/rails/social_stream.rb:8:ininitialize_with_magic' actionpack (3.2.5) lib/action_dispatch/http/upload.rb:39:in new' actionpack (3.2.5) lib/action_dispatch/http/upload.rb:39:innormalize_parameters' actionpack (3.2.5) lib/action_dispatch/http/parameters.rb:73:in block in normalize_parameters' actionpack (3.2.5) lib/action_dispatch/http/parameters.rb:73:ineach' actionpack ( 3.2.5) lib/action_dispatch/http/parameters.rb:73:in normalize_parameters' actionpack (3.2.5) lib/action_dispatch/http/upload.rb:41:innormalize_parameters' actionpack (3.2.5) lib/action_dispatch/http/parameters.rb:73:in block in normalize_parameters' actionpack (3.2.5) lib/action_dispatch/http/parameters.rb:73:ineach' actionpack (3.2.5) lib/action_dispatch/ http/parameters.rb:73:in normalize_parameters' actionpack (3.2.5) lib/action_dispatch/http/upload.rb:41:innormalize_parameters'

有没有办法让这个在 Windows 上工作,或者通过使用 gem,我可以在 Windows 上安装的东西,或者重写文件?

4

1 回答 1

1

删除或注释掉该行:

alias_method_chain :initialize, :magic

这将阻止它挂接到 ActionDispatch::HTTP::UploadFile#initialize

这也意味着您不会获得“证明”内容类型的好处,但我不知道这对您的应用程序是否真的有必要。

于 2012-07-31T21:17:00.690 回答