summaryrefslogtreecommitdiff
path: root/app/models/fax_document.rb
diff options
context:
space:
mode:
authorStefan Wintermeyer <stefan.wintermeyer@amooma.de>2013-02-27 12:22:29 +0100
committerStefan Wintermeyer <stefan.wintermeyer@amooma.de>2013-02-27 12:22:29 +0100
commit67c1a0a951403a546250ea860a3e3d199cae2fce (patch)
treef86ec6cedf081a1a57005eca1661e9bead34e4de /app/models/fax_document.rb
parent5d8ce5f4775ac8bc5f523964e6e36f63ff3c4683 (diff)
parent211f558a86ae30cdd5b392ab1376e1393f97e22c (diff)
Merge branch 'develop'5.1-beta6
Diffstat (limited to 'app/models/fax_document.rb')
-rw-r--r--app/models/fax_document.rb74
1 files changed, 61 insertions, 13 deletions
diff --git a/app/models/fax_document.rb b/app/models/fax_document.rb
index 564d3bb..5b27965 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,8 +17,10 @@ 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
+
+ after_destroy :remove_storage_dir
# Scopes
scope :inbound, where(:state => 'inbound')
@@ -57,7 +58,7 @@ class FaxDocument < ActiveRecord::Base
end
def create_thumbnails_and_save_them
- tmp_dir = "/tmp/fax_convertions/#{self.id}"
+ tmp_dir = "/var/spool/gemeinschaft/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|
@@ -68,17 +69,64 @@ class FaxDocument < ActiveRecord::Base
FileUtils.rm_rf tmp_dir
end
- private
- def convert_pdf_to_tiff
+ def tiff_to_pdf
+ if !File.exists?(self.tiff)
+ return nil
+ end
+
+ 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}\" \"#{self.tiff}\""
+
+ if !File.exists?(pdf_file)
+ return nil
+ end
+
+ return pdf_file
+ end
+
+ 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 = "/tmp/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.to_i}"
+ else
+ "/var/opt/gemeinschaft/fax/out/#{self.id.to_i}"
+ 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
+
+ def remove_storage_dir
+ if File.directory?(self.store_dir)
+ FileUtils.rm_rf(self.store_dir)
+ end
end
end