diff options
Diffstat (limited to 'app/models/fax_document.rb')
-rw-r--r-- | app/models/fax_document.rb | 24 |
1 files changed, 13 insertions, 11 deletions
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 |