我遇到过某些文件类型的类似问题。我使用Ubuntu 12.04和file --mime
并不总能找到文件类型,例如.doc文件。
我通过修改回形针使用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
你有没有需要“开URI”在你的文件的顶部? – fengd