优化文件读取编码解析

This commit is contained in:
nwb 2014-07-28 14:43:38 +08:00
parent 1b589bf562
commit 2c79a51e48
1 changed files with 8 additions and 0 deletions

View File

@ -26,6 +26,7 @@ class AttachmentsController < ApplicationController
accept_api_auth :show, :download, :upload
require 'iconv'
def show
respond_to do |format|
format.html {
@ -41,6 +42,13 @@ class AttachmentsController < ApplicationController
render :action => 'diff'
elsif @attachment.is_text? && @attachment.filesize <= Setting.file_max_size_displayed.to_i.kilobyte
@content = File.new(@attachment.diskfile, "rb").read
# 编码为非 UTF-8先进行间接转码
# 部分unicode编码不直接支持转为 UTF-8
# modify by nwb
if @content.encoding.name != 'UTF-8'
@content = @content.force_encoding('GBK')
@content = @content.encode('UTF-8')
end
render :action => 'file'
else
download