From 2b3892ac24bb61c4814e0afe141bf53765fb1e4b Mon Sep 17 00:00:00 2001 From: spag Date: Tue, 19 Feb 2013 13:16:12 +0100 Subject: fax storage directory --- app/controllers/trigger_controller.rb | 2 +- app/models/fax_document.rb | 55 ++++++++++++++++++--------- app/uploaders/fax_document_uploader.rb | 19 +++++++++ app/views/fax_documents/_index_core.html.haml | 2 +- app/views/fax_documents/show.html.haml | 2 +- misc/freeswitch/scripts/dialplan/fax.lua | 8 ++-- misc/freeswitch/scripts/send_fax.lua | 5 +-- 7 files changed, 63 insertions(+), 30 deletions(-) create mode 100644 app/uploaders/fax_document_uploader.rb diff --git a/app/controllers/trigger_controller.rb b/app/controllers/trigger_controller.rb index 136c3d0..894c18b 100644 --- a/app/controllers/trigger_controller.rb +++ b/app/controllers/trigger_controller.rb @@ -65,7 +65,7 @@ class TriggerController < ApplicationController if fax_account fax_account.fax_documents.where(:state => 'received').each do |fax_document| - pdf_file, tiff_file = fax_document.tiff_to_pdf + pdf_file = fax_document.tiff_to_pdf if !pdf_file fax_document.state = 'unsuccessful' diff --git a/app/models/fax_document.rb b/app/models/fax_document.rb index c73fa3d..e9bb5f1 100644 --- a/app/models/fax_document.rb +++ b/app/models/fax_document.rb @@ -1,8 +1,7 @@ class FaxDocument < ActiveRecord::Base # attr_accessible :inbound, :transmission_time, :sent_at, :document_total_pages, :document_transferred_pages, :ecm_requested, :ecm_used, :image_resolution, :image_size, :local_station_id, :result_code, :result_text, :remote_station_id, :success, :transfer_rate, :t38_gateway_format, :t38_peer, :document - mount_uploader :document, DocumentUploader - mount_uploader :tiff, TiffUploader + mount_uploader :document, FaxDocumentUploader validates_presence_of :document validates_numericality_of :retry_counter, :only_integer => true, :greater_than_or_equal_to => 0 @@ -18,7 +17,7 @@ class FaxDocument < ActiveRecord::Base has_many :fax_thumbnails, :order => :position, :dependent => :destroy - after_create :convert_pdf_to_tiff + after_save :convert_to_tiff after_create :render_thumbnails # Scopes @@ -68,40 +67,58 @@ class FaxDocument < ActiveRecord::Base FileUtils.rm_rf tmp_dir end - def tiff_to_pdf() - tiff_file = self.tiff.to_s.gsub(self.tiff.store_path, '') - if !File.exists?(tiff_file) + def tiff_to_pdf + if !File.exists?(self.tiff) return nil end - working_path, file_name = File.split(tiff_file) - pdf_file = "#{working_path}/#{File.basename(tiff_file, '.tiff')}.pdf" + working_path, file_name = File.split(self.tiff) + pdf_file = "#{working_path}/#{File.basename(self.tiff, '.tiff')}.pdf" system "tiff2pdf \\ -o \"#{pdf_file}\" \\ -p letter \\ -a \"#{self.remote_station_id}\" \\ -c \"AMOOMA Gemeinschaft version #{GsParameter.get('GEMEINSCHAFT_VERSION')}\" \\ - -t \"#{self.remote_station_id}\" \"#{tiff_file}\"" + -t \"#{self.remote_station_id}\" \"#{self.tiff}\"" if !File.exists?(pdf_file) return nil end - return pdf_file, tiff_file + return pdf_file end - private - def convert_pdf_to_tiff + def to_tiff page_size_a4 = '595 842' page_size_command = "<< /Policies << /PageSize 3 >> /InputAttributes currentpagedevice /InputAttributes get dup { pop 1 index exch undef } forall dup 0 << /PageSize [ #{page_size_a4} ] >> put >> setpagedevice" - directory = "/var/spool/gemeinschaft/GS-#{GsParameter.get('GEMEINSCHAFT_VERSION')}/faxes/#{self.id}" - FileUtils.mkdir_p directory - tiff_file_name = File.basename(self.document.to_s.downcase, ".pdf") + '.tiff' - system "cd #{directory} && gs -q -r#{self.fax_resolution.resolution_value} -dNOPAUSE -dBATCH -dSAFER -sDEVICE=tiffg3 -sOutputFile=\"#{tiff_file_name}\" -c \"#{page_size_command}\" -- \"#{Rails.root.to_s}/public#{self.document.to_s}\"" - self.tiff = File.open("#{directory}/#{tiff_file_name}") - self.save - FileUtils.rm_rf directory + working_path, file_name = File.split(self.document.to_s) + tiff_file = File.basename(file_name.to_s.downcase, File.extname(file_name)) + '.tiff' + result = system "cd #{store_dir} && gs -q -r#{self.fax_resolution.resolution_value} -dNOPAUSE -dBATCH -dSAFER -sDEVICE=tiffg3 -sOutputFile=\"#{tiff_file}\" -c \"#{page_size_command}\" -- \"#{self.document.to_s}\"" + + if !File.exists?("#{store_dir}/#{tiff_file}") + return nil + end + + return "#{store_dir}/#{tiff_file}" + end + + def store_dir + if self.try(:inbound) + "/var/opt/gemeinschaft/fax/in/#{self.id}" + else + "/var/opt/gemeinschaft/fax/out/#{self.id}" + end + end + + private + def convert_to_tiff + if self.tiff.blank? + self.tiff = self.to_tiff + if self.tiff + return self.save + end + end end end diff --git a/app/uploaders/fax_document_uploader.rb b/app/uploaders/fax_document_uploader.rb new file mode 100644 index 0000000..bfb7e07 --- /dev/null +++ b/app/uploaders/fax_document_uploader.rb @@ -0,0 +1,19 @@ +# encoding: utf-8 + +class FaxDocumentUploader < CarrierWave::Uploader::Base + include CarrierWave::MiniMagick + + storage :file + + def store_dir + model.store_dir + end + + def cache_dir + '/tmp/gs_fax_uploader' + end + + def extension_white_list + %w(pdf ps jpg jpeg gif png tif tiff) + end +end diff --git a/app/views/fax_documents/_index_core.html.haml b/app/views/fax_documents/_index_core.html.haml index 5355521..2f9b214 100644 --- a/app/views/fax_documents/_index_core.html.haml +++ b/app/views/fax_documents/_index_core.html.haml @@ -50,7 +50,7 @@ - if fax_document.document? %p - %a{:href => fax_document.document.url} + %a{:href => fax_account_fax_document_path(@fax_account, fax_document, :format => :pdf), :method => :get} %i{:class => 'icon-download'} = t("fax_documents.index.actions.download_pdf") + " (#{number_to_human_size(fax_document.document.size, :precision => 2)})" diff --git a/app/views/fax_documents/show.html.haml b/app/views/fax_documents/show.html.haml index 9925c2f..b8f3e9e 100644 --- a/app/views/fax_documents/show.html.haml +++ b/app/views/fax_documents/show.html.haml @@ -46,7 +46,7 @@ - if @fax_document.document? %p - %a{:href => @fax_document.document.url} + %a{:href => fax_account_fax_document_path(@fax_account, @fax_document, :format => :pdf), :method => :get} %i{:class => 'icon-download'} = t("fax_documents.index.actions.download_pdf") + " (#{number_to_human_size(@fax_document.document.size, :precision => 2)})" diff --git a/misc/freeswitch/scripts/dialplan/fax.lua b/misc/freeswitch/scripts/dialplan/fax.lua index 8cfa9e5..6dce0a9 100644 --- a/misc/freeswitch/scripts/dialplan/fax.lua +++ b/misc/freeswitch/scripts/dialplan/fax.lua @@ -4,7 +4,7 @@ module(...,package.seeall) -FAX_DOCUMENTS_DIRECTORY = '/var/spool/freeswitch/' +FAX_SPOOL_DIRECTORY = '/var/spool/freeswitch/' FAX_PARALLEL_MAX = 8; Fax = {} @@ -18,7 +18,7 @@ function Fax.new(self, arg) self.log = arg.log; self.database = arg.database; self.record = arg.record; - self.fax_directory = arg.fax_directory or FAX_DOCUMENTS_DIRECTORY; + self.fax_spool_directory = arg.fax_spool_directory or FAX_SPOOL_DIRECTORY; return object; end @@ -79,7 +79,7 @@ end -- List waiting fax documents function Fax.queued_for_sending(self, limit) limit = limit or FAX_PARALLEL_MAX; - local sql_query = 'SELECT * FROM `fax_documents` WHERE `state` IN ("queued_for_sending","unsuccessful") AND `retry_counter` > 0 ORDER BY `sent_at` ASC LIMIT ' .. limit; + local sql_query = 'SELECT * FROM `fax_documents` WHERE `state` IN ("queued_for_sending","unsuccessful") AND `retry_counter` > 0 AND `tiff` IS NOT NULL AND `tiff` != "" ORDER BY `sent_at` ASC LIMIT ' .. limit; local fax_documents = {} self.database:query(sql_query, function(fax_entry) fax_entry['destination_numbers'] = Fax:destination_numbers(fax_entry.id) @@ -136,7 +136,7 @@ end -- Receive Fax function Fax.receive(self, caller, file_name) - file_name = file_name or self.fax_directory .. 'fax_in_' .. caller.uuid .. '.tiff'; + file_name = file_name or self.fax_spool_directory .. 'fax_in_' .. caller.uuid .. '.tiff'; caller:set_variable('fax_ident', self.record.station_id) caller:set_variable('fax_verbose', 'false') diff --git a/misc/freeswitch/scripts/send_fax.lua b/misc/freeswitch/scripts/send_fax.lua index 4898cb8..d717f93 100644 --- a/misc/freeswitch/scripts/send_fax.lua +++ b/misc/freeswitch/scripts/send_fax.lua @@ -2,7 +2,6 @@ -- (c) AMOOMA GmbH 2012-2013 -- -local FAX_FILE_PATH = "/opt/GS5/public/uploads/fax_document/tiff/"; local FAX_ANSWERING_TIMEOUT = 20; -- Set logger @@ -142,13 +141,11 @@ end if session and session:answered() then log:info('FAX_SEND - sending fax_document=' .. fax_document.id .. ' (' .. fax_document.tiff .. ')'); - local file_name = FAX_FILE_PATH .. fax_document.id .. "/" .. fax_document.tiff; - session:setVariable('fax_ident', fax_account.record.station_id) session:setVariable('fax_header', fax_account.record.name) session:setVariable('fax_verbose', 'false') local start_time = os.time(); - session:execute('txfax', file_name); + session:execute('txfax', fax_document.tiff); fax_state = { state = nil, -- cgit v1.2.3