From c331e5a9c8933064f8460de75e6cbec8a5805b42 Mon Sep 17 00:00:00 2001 From: guange <8863824@gmail.com> Date: Wed, 3 Jun 2015 17:42:41 +0800 Subject: [PATCH] =?UTF-8?q?=E8=BD=AChtml=E6=94=B9=E4=B8=BA=E8=BD=ACpdf?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/attachments_controller.rb | 13 ++++++++++--- lib/trustie/utils/office.rb | 9 ++++++--- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/app/controllers/attachments_controller.rb b/app/controllers/attachments_controller.rb index c8b6a6fda..9c3b624af 100644 --- a/app/controllers/attachments_controller.rb +++ b/app/controllers/attachments_controller.rb @@ -62,6 +62,10 @@ class AttachmentsController < ApplicationController render :action => 'file' end + def pdf?(file) + file.downcase.end_with?(".pdf") + end + def download # modify by nwb # 下载添加权限设置 @@ -69,9 +73,12 @@ class AttachmentsController < ApplicationController if candown || User.current.admin? || User.current.id == @attachment.author_id @attachment.increment_download if stale?(:etag => @attachment.digest) - convered_file = File.join(Rails.root, "files", "convered_office", @attachment.disk_filename + ".html") - if File.exist?(convered_file) - send_file convered_file, :type => 'text/html; charset=utf-8', :disposition => 'inline' + convered_file = @attachment.disk_filename + unless pdf?(convered_file) + convered_file = File.join(Rails.root, "files", "convered_office", @attachment.disk_filename + ".pdf") + end + if File.exist?(convered_file) && pdf?(convered_file) + send_file convered_file, :type => 'application/pdf; charset=utf-8', :disposition => 'inline' else send_file @attachment.diskfile, :filename => filename_for_content_disposition(@attachment.filename), :type => detect_content_type(@attachment), diff --git a/lib/trustie/utils/office.rb b/lib/trustie/utils/office.rb index 1bc18cb84..adfa3fb5c 100644 --- a/lib/trustie/utils/office.rb +++ b/lib/trustie/utils/office.rb @@ -7,7 +7,7 @@ module Trustie end def office? - %w(doc docx ppt pptx xls xlsx pdf).any?{|word| @file.downcase.end_with?(word)} + %w(doc docx ppt pptx xls xlsx).any?{|word| @file.downcase.end_with?(word)} end def conver(saved_file, force=false) @@ -21,8 +21,11 @@ module Trustie :open_timeout => -1 ) req = resource.post :txtDes => File.new(@file, 'rb') - File.open(saved_file, "wb+") do |f| - f.write(req.body) + File.delete(saved_file) if File.exist?(saved_file) + if req.body.length > 10 && !req.body.eql?('转换出错') + File.open(saved_file, "wb+") do |f| + f.write(req.body) + end end return true rescue =>e