From 444658d0b3a8ef46431396bd9ec7ffb3f6df8364 Mon Sep 17 00:00:00 2001 From: guange <8863824@gmail.com> Date: Thu, 4 Jun 2015 10:24:05 +0800 Subject: [PATCH] =?UTF-8?q?=E8=BD=AC=E6=8D=A2=E5=8F=98=E4=B8=BA=E6=97=A2?= =?UTF-8?q?=E6=97=B6=E8=BD=AC=E6=8D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/attachments_controller.rb | 33 ++++++++++++++------- app/models/attachment.rb | 15 +++++----- app/views/files/_project_file_list.html.erb | 5 ++-- lib/trustie/utils/office.rb | 13 ++++---- 4 files changed, 41 insertions(+), 25 deletions(-) diff --git a/app/controllers/attachments_controller.rb b/app/controllers/attachments_controller.rb index d3781d747..8ad48c9b2 100644 --- a/app/controllers/attachments_controller.rb +++ b/app/controllers/attachments_controller.rb @@ -66,6 +66,12 @@ class AttachmentsController < ApplicationController file.downcase.end_with?(".pdf") end + def direct_download + send_file @attachment.diskfile, :filename => filename_for_content_disposition(@attachment.filename), + :type => detect_content_type(@attachment), + :disposition => 'attachment' #inline can open in browser + end + def download # modify by nwb # 下载添加权限设置 @@ -73,23 +79,30 @@ class AttachmentsController < ApplicationController if candown || User.current.admin? || User.current.id == @attachment.author_id @attachment.increment_download if stale?(:etag => @attachment.digest) - convered_file = @attachment.diskfile - 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' + if params[:force] == 'true' + direct_download else - send_file @attachment.diskfile, :filename => filename_for_content_disposition(@attachment.filename), - :type => detect_content_type(@attachment), - :disposition => 'attachment' #inline can open in browser + convered_file = @attachment.diskfile + #如果本身不是pdf文件,则先寻找是不是已转换化,如果没有则转化 + unless pdf?(convered_file) + convered_file = File.join(Rails.root, "files", "convered_office", @attachment.disk_filename + ".pdf") + unless File.exist?(convered_file) + office = Trustie::Utils::Office.new(@attachment.diskfile) + office.conver(convered_file) + end + end + if File.exist?(convered_file) && pdf?(convered_file) + send_file convered_file, :type => 'application/pdf; charset=utf-8', :disposition => 'inline' + else + direct_download + end end end else render_403 :message => :notice_not_authorized end rescue => e - redirect_to "http: //" + (Setting.host_name.to_s) +"/file_not_found.html" + redirect_to "http://" + (Setting.host_name.to_s) +"/file_not_found.html" end #更新资源文件类型 diff --git a/app/models/attachment.rb b/app/models/attachment.rb index 1f1d61581..f999e27d6 100644 --- a/app/models/attachment.rb +++ b/app/models/attachment.rb @@ -259,13 +259,14 @@ class Attachment < ActiveRecord::Base end def office_conver - return unless %w(Project Course).include? (self.container_type) - saved_path = File.join(Rails.root, "files", "convered_office") - unless Dir.exist?(saved_path) - Dir.mkdir(saved_path) - end - convered_file = File.join(saved_path, self.disk_filename + ".pdf") - OfficeConverTask.new.conver(self.diskfile, convered_file) + # 不在这里做后台转换,换为点击时做转换 + # return unless %w(Project Course).include? (self.container_type) + # saved_path = File.join(Rails.root, "files", "convered_office") + # unless Dir.exist?(saved_path) + # Dir.mkdir(saved_path) + # end + # convered_file = File.join(saved_path, self.disk_filename + ".pdf") + # OfficeConverTask.new.conver(self.diskfile, convered_file) end # Copies the temporary file to its final location diff --git a/app/views/files/_project_file_list.html.erb b/app/views/files/_project_file_list.html.erb index 35aa36195..a5152ecdc 100644 --- a/app/views/files/_project_file_list.html.erb +++ b/app/views/files/_project_file_list.html.erb @@ -30,6 +30,7 @@ <% else %> <%= link_to(l(:label_slected_to_project),quote_resource_show_project_project_file_path(project,file),:class => "f_l re_select",:remote => true) if has_project?(User.current,file) %> <% end %> + <%= link_to '下载',download_named_attachment_path(file.id, file.filename, force: true),class: 'f_l re_open' %> <% end %>
@@ -37,7 +38,7 @@

文件大小:<%= number_to_human_size(file.filesize) %>

<%= link_to( l(:button_delete), attachment_path(file), :data => {:confirm => l(:text_are_you_sure)}, :method => :delete,:class => "f_r re_de") if manage_allowed && file.container_id == project.id && file.container_type == "Project"%> -

<%= time_tag(file.created_on).html_safe %><%= l(:label_bids_published_ago) %>  |  下载<%= file.downloads %>  |  引用<%= file.quotes.nil? ? 0:file.quotes %>

+

<%= time_tag(file.created_on).html_safe %><%= l(:label_bids_published_ago) %>  |  下载<%= file.downloads %>  |  引用<%= file.quotes.nil? ? 0:file.quotes %>

@@ -51,4 +52,4 @@ -
\ No newline at end of file +
diff --git a/lib/trustie/utils/office.rb b/lib/trustie/utils/office.rb index 61d6b9d4c..2b4a5f1ac 100644 --- a/lib/trustie/utils/office.rb +++ b/lib/trustie/utils/office.rb @@ -17,12 +17,13 @@ module Trustie if File.exist? @file if office? begin - resource = RestClient::Resource.new( - 'http://192.168.80.107/Any2HtmlHandler.ashx', - :timeout => -1, - :open_timeout => -1 - ) - req = resource.post :txtDes => File.new(@file, 'rb') + # resource = RestClient::Resource.new( + # 'http://192.168.80.107/Any2HtmlHandler.ashx', + # :timeout => -1, + # :open_timeout => -1 + # ) + # req = resource.post :txtDes => File.new(@file, 'rb') + req = RestClient.post 'http://192.168.80.107/Any2HtmlHandler.ashx',:txtDes => File.new(@file, 'rb') File.delete(saved_file) if File.exist?(saved_file) if req.body.length > 10 && !req.body.eql?('转换出错') File.open(saved_file, "wb+") do |f|