added timeout

This commit is contained in:
guange 2015-06-03 10:54:40 +08:00
parent 032bd86367
commit 57f8c7d04f
2 changed files with 54 additions and 16 deletions

View File

@ -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

View File

@ -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