From be3680d4f33a1be96bac81bdf0e3a32f680f1cd9 Mon Sep 17 00:00:00 2001 From: guange <8863824@gmail.com> Date: Tue, 2 Jun 2015 15:43:03 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E6=94=AF=E6=8C=81doc=20docx=20pdf?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/attachments_controller.rb | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/app/controllers/attachments_controller.rb b/app/controllers/attachments_controller.rb index b2364165b..68d83f974 100644 --- a/app/controllers/attachments_controller.rb +++ b/app/controllers/attachments_controller.rb @@ -69,12 +69,14 @@ class AttachmentsController < ApplicationController if candown || User.current.admin? || User.current.id == @attachment.author_id @attachment.increment_download if stale?(:etag => @attachment.digest) - req = RestClient.post 'http://192.168.80.107/Any2HtmlHandler.ashx', :txtDes => File.new(@attachment.diskfile, 'rb') - render :text => req.body - - # send_file @attachment.diskfile, :filename => filename_for_content_disposition(@attachment.filename), - # :type => detect_content_type(@attachment), - # :disposition => 'attachment' #inline can open in browser + if %w[doc docx pdf].any?{|word| @attachment.diskfile.downcase.end_with? word} + req = RestClient.post 'http://192.168.80.107/Any2HtmlHandler.ashx', :txtDes => File.new(@attachment.diskfile, 'rb') + render :text => req.body + else + send_file @attachment.diskfile, :filename => filename_for_content_disposition(@attachment.filename), + :type => detect_content_type(@attachment), + :disposition => 'attachment' #inline can open in browser + end end else From 49c86eb85fb6d8872ba0050d352e545f33381525 Mon Sep 17 00:00:00 2001 From: guange <8863824@gmail.com> Date: Wed, 3 Jun 2015 09:46:44 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E5=B0=86=E9=A2=84=E8=A7=88=E6=94=B9?= =?UTF-8?q?=E4=B8=BA=E5=90=8E=E5=8F=B0=E6=89=A7=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/attachments_controller.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/controllers/attachments_controller.rb b/app/controllers/attachments_controller.rb index 68d83f974..f7adf802b 100644 --- a/app/controllers/attachments_controller.rb +++ b/app/controllers/attachments_controller.rb @@ -69,9 +69,9 @@ class AttachmentsController < ApplicationController if candown || User.current.admin? || User.current.id == @attachment.author_id @attachment.increment_download if stale?(:etag => @attachment.digest) - if %w[doc docx pdf].any?{|word| @attachment.diskfile.downcase.end_with? word} - req = RestClient.post 'http://192.168.80.107/Any2HtmlHandler.ashx', :txtDes => File.new(@attachment.diskfile, 'rb') - render :text => req.body + convered_file = File.join(Rails.root, "files", "convered_office", a.disk_filename + ".html") + if File.exist?(convered_file) + render :text => File.open(convered_file).read else send_file @attachment.diskfile, :filename => filename_for_content_disposition(@attachment.filename), :type => detect_content_type(@attachment), From 670f9598e41b6cad4180690b3cf20f41f5dbc29b Mon Sep 17 00:00:00 2001 From: guange <8863824@gmail.com> Date: Wed, 3 Jun 2015 09:53:10 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=90=8E=E5=8F=B0?= =?UTF-8?q?=E8=BD=AC=E6=8D=A2office=E4=BB=BB=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/tasks/office.rake | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 lib/tasks/office.rake diff --git a/lib/tasks/office.rake b/lib/tasks/office.rake new file mode 100644 index 000000000..770379b1a --- /dev/null +++ b/lib/tasks/office.rake @@ -0,0 +1,24 @@ +namespace :office do + desc "conver any files to html" + task :conver => :environment do + Attachment.find_each do |a| + convered_file = File.join(Rails.root, "files", "convered_office", a.disk_filename + ".html") + unless File.exist?(convered_file) + if File.exist? a.diskfile + if %w(doc docx ppt pptx xls xlsx pdf).any?{|word| a.diskfile.downcase.end_with?(word)} + begin + req = RestClient.post 'http://192.168.80.107/Any2HtmlHandler.ashx', :txtDes => File.new(a.diskfile, 'rb') + File.new(convered_file, "ab+") do |f| + f.write(req.body) + end + rescue =>e + puts e.message + end + end + else + puts "can't find file #{a.diskfile}" + end + end + end + end +end