From 3db09ff394b92b5d168c32c355d4529fd3861d36 Mon Sep 17 00:00:00 2001 From: Stefan Wintermeyer Date: Wed, 13 Feb 2013 15:29:43 +0100 Subject: Improvements in the fax_document model and in FaxDocument#show. --- app/models/fax_document.rb | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) (limited to 'app/models/fax_document.rb') diff --git a/app/models/fax_document.rb b/app/models/fax_document.rb index 16fdc70..4db4980 100644 --- a/app/models/fax_document.rb +++ b/app/models/fax_document.rb @@ -47,19 +47,21 @@ class FaxDocument < ActiveRecord::Base transition [:new] => :inbound end end + + def to_s + "#{self.remote_station_id}-#{self.created_at}-#{self.id}".gsub(/[^a-zA-Z0-9]/,'') + end def render_thumbnails - directory = "/tmp/GS-#{GsParameter.get('GEMEINSCHAFT_VERSION')}/fax_thumbnails/#{self.id}" - system('mkdir -p ' + directory) - system("cd #{directory} && convert #{Rails.root.to_s}/public#{self.document.to_s}[0-100] -colorspace Gray PNG:'fax_page.png'") - number_of_thumbnails = Dir["#{directory}/fax_page-*.png"].count - (0..(number_of_thumbnails-1)).each do |i| + tmp_dir = "/tmp/fax_convertions/#{self.id}" + FileUtils.mkdir_p tmp_dir + system("cd #{tmp_dir} && convert #{self.document.path} -colorspace Gray PNG:'fax_page.png'") + Dir.glob("#{tmp_dir}/fax_page*.png").each do |thumbnail| fax_thumbnail = self.fax_thumbnails.build - fax_thumbnail.thumbnail = File.open("#{directory}/fax_page-#{i}.png") - fax_thumbnail.save! + fax_thumbnail.thumbnail = File.open(thumbnail) + fax_thumbnail.save end - system("rm -rf #{directory}") - self.update_attributes(:document_total_pages => number_of_thumbnails) if self.document_total_pages.nil? + FileUtils.rm_rf tmp_dir end private @@ -67,12 +69,12 @@ class FaxDocument < ActiveRecord::Base 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 = "/tmp/GS-#{GsParameter.get('GEMEINSCHAFT_VERSION')}/faxes/#{self.id}" - system('mkdir -p ' + directory) + 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 - system("rm -rf #{directory}") + FileUtils.rm_rf directory end end -- cgit v1.2.3 From ed8e0ac76bddab0d74095313f36bd836d30dcb84 Mon Sep 17 00:00:00 2001 From: Stefan Wintermeyer Date: Wed, 13 Feb 2013 17:33:32 +0100 Subject: The convertion of PDFs to fax thumbnails now gets done by a delayed_job. --- app/models/fax_document.rb | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'app/models/fax_document.rb') diff --git a/app/models/fax_document.rb b/app/models/fax_document.rb index 4db4980..564d3bb 100644 --- a/app/models/fax_document.rb +++ b/app/models/fax_document.rb @@ -18,8 +18,8 @@ class FaxDocument < ActiveRecord::Base has_many :fax_thumbnails, :order => :position, :dependent => :destroy - after_create :render_thumbnails after_create :convert_pdf_to_tiff + after_create :render_thumbnails # Scopes scope :inbound, where(:state => 'inbound') @@ -51,8 +51,12 @@ class FaxDocument < ActiveRecord::Base def to_s "#{self.remote_station_id}-#{self.created_at}-#{self.id}".gsub(/[^a-zA-Z0-9]/,'') end - + def render_thumbnails + self.delay.create_thumbnails_and_save_them + end + + def create_thumbnails_and_save_them tmp_dir = "/tmp/fax_convertions/#{self.id}" FileUtils.mkdir_p tmp_dir system("cd #{tmp_dir} && convert #{self.document.path} -colorspace Gray PNG:'fax_page.png'") -- cgit v1.2.3