From 57f8c7d04f6d24c29c79dfd314e5a6062db7368c Mon Sep 17 00:00:00 2001 From: guange <8863824@gmail.com> Date: Wed, 3 Jun 2015 10:54:40 +0800 Subject: [PATCH 1/2] added timeout --- lib/tasks/office.rake | 28 +++++++++++-------------- lib/trustie/utils/office.rb | 42 +++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 16 deletions(-) create mode 100644 lib/trustie/utils/office.rb diff --git a/lib/tasks/office.rake b/lib/tasks/office.rake index 770379b1a..3e4c0a286 100644 --- a/lib/tasks/office.rake +++ b/lib/tasks/office.rake @@ -1,23 +1,19 @@ namespace :office do desc "conver any files to html" task :conver => :environment do + all_count = Attachment.count + i = 0 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 + i += 1 + puts "process [#{i}/#{all_count}] => id #{a.id}" + 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, a.disk_filename + ".html") + office = Trustie::Utils::Office.new(a.diskfile) + if office.conver(convered_file) + puts "process ok: #{convered_file} " end end end diff --git a/lib/trustie/utils/office.rb b/lib/trustie/utils/office.rb new file mode 100644 index 000000000..3d58bb047 --- /dev/null +++ b/lib/trustie/utils/office.rb @@ -0,0 +1,42 @@ +module Trustie + module Utils + class Office + + def initialize(file) + @file = file + end + + def office? + %w(doc docx ppt pptx xls xlsx pdf).any?{|word| @file.downcase.end_with?(word)} + end + + def conver(saved_file, force=false) + if force || !File.exist?(saved_file) + 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') + File.new(saved_file, "ab+") do |f| + f.write(req.body) + end + return true + rescue =>e + puts e.message + end + end + else + puts "can't find file #{@file}" + end + end + false + end + + end + end +end + From 87e7db1a4b3f349e61d5fe42652bd870251976cb Mon Sep 17 00:00:00 2001 From: guange <8863824@gmail.com> Date: Wed, 3 Jun 2015 11:14:33 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E9=99=84=E4=BB=B6?= =?UTF-8?q?=E6=97=B6=E8=BD=AC=E6=8D=A2office=E6=96=87=E6=A1=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/models/attachment.rb | 11 ++++++++++- app/tasks/office_conver_task.rb | 12 ++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 app/tasks/office_conver_task.rb diff --git a/app/models/attachment.rb b/app/models/attachment.rb index 7ac7d786d..1e477ed04 100644 --- a/app/models/attachment.rb +++ b/app/models/attachment.rb @@ -72,7 +72,7 @@ class Attachment < ActiveRecord::Base @@thumbnails_storage_path = File.join(Rails.root, "tmp", "thumbnails") before_save :files_to_final_location - after_create :be_user_score ,:act_as_forge_activity# user_score + after_create :office_conver, :be_user_score ,:act_as_forge_activity# user_score after_update :be_user_score after_destroy :delete_from_disk,:down_user_score @@ -258,6 +258,15 @@ class Attachment < ActiveRecord::Base filename end + def office_conver + 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 + ".html") + OfficeConverTask.new.conver(self.diskfile, convered_file) + end + # Copies the temporary file to its final location # and computes its MD5 hash def files_to_final_location diff --git a/app/tasks/office_conver_task.rb b/app/tasks/office_conver_task.rb new file mode 100644 index 000000000..da950bf2e --- /dev/null +++ b/app/tasks/office_conver_task.rb @@ -0,0 +1,12 @@ +#coding=utf-8 +# +class OfficeConverTask + def conver(source_file, saved_file) + office = Trustie::Utils::Office.new(source_file) + if office.conver(saved_file) + Rails.logger.info "process ok: #{saved_file} " + end + end + handle_asynchronously :conver,:queue => 'office_conver' +end +