OpenProject 上傳「中文文件名」不能正確顯示的問題

從 2007 年開始用 Redmine,最近半年一直想換一個平臺,團隊用了三個多月的 Worktile,不太習慣。最近一個月考察了 Trello、Tower、Teambition、OpenProject,最後決定試用 OpenProject 一週。上週六用了七個多小時完成手工配置,簡單試用下來,Timeline、Report、Backlogs 等功能都是極好的。git

美中不足的是:上傳附件若是是「中文文件名」不能正常顯示。不會 Ruby 和 Rails,Google 以後經過修改源碼的方式解決了這個問題。github

修改方案

OpenProject 使用了 CarrierWave 實現上傳文件功能,可是 CarrierWave 默認僅支持英語字母、數字、空格。若是要支持漢語須要重寫 sanitize_regexp 方法。express

Filenames and unicode chars

Another security issue you should care for is the file names (see Ruby On Rails Security Guide). By default, CarrierWave provides only English letters, arabic numerals and some symbols as white-listed characters in the file name. If you want to support local scripts (Cyrillic letters, letters with diacritics and so on), you have to override sanitize_regexp method. It should return regular expression which would match all non-allowed symbols.ruby

Also make sure that allowing non-latin characters won't cause a compatibility issue with a third-party plugins or client-side software.app

知道了解決方法,須要結合 OpenProject 代碼才能確認具體修改哪一個文件。ide

A 方案(確承認行)

/config/initializers/carrierwave.rb 文件最後添加一行代碼ui

CarrierWave::SanitizedFile.sanitize_regexp = /[^[:word:]\.\-\+]/

B 方案(未確認)

修改 /app/uploaders/fog_file_uploader.rb/app/uploaders/local_file_uploader.rb 文件,添加如下代碼:code

def sanitize_regexp
  /[^0-9\.\-\+_]/
end

參考資料

相關文章
相關標籤/搜索