2015-06-03 17:52:59 +08:00
|
|
|
#coding=utf-8
|
|
|
|
|
2015-06-03 10:54:40 +08:00
|
|
|
module Trustie
|
|
|
|
module Utils
|
|
|
|
class Office
|
|
|
|
|
|
|
|
def initialize(file)
|
|
|
|
@file = file
|
|
|
|
end
|
|
|
|
|
|
|
|
def office?
|
2015-06-03 17:42:41 +08:00
|
|
|
%w(doc docx ppt pptx xls xlsx).any?{|word| @file.downcase.end_with?(word)}
|
2015-06-03 10:54:40 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def conver(saved_file, force=false)
|
|
|
|
if force || !File.exist?(saved_file)
|
|
|
|
if File.exist? @file
|
|
|
|
if office?
|
|
|
|
begin
|
2015-06-04 10:24:05 +08:00
|
|
|
# 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')
|
2015-06-03 17:42:41 +08:00
|
|
|
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
|
2015-06-03 10:54:40 +08:00
|
|
|
end
|
|
|
|
return true
|
|
|
|
rescue =>e
|
|
|
|
puts e.message
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
puts "can't find file #{@file}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|