diff --git a/app/controllers/attachments_controller.rb b/app/controllers/attachments_controller.rb index b2364165b..f7adf802b 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 + 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), + :disposition => 'attachment' #inline can open in browser + end end else 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