summaryrefslogtreecommitdiff
path: root/app/models/fax_document.rb
diff options
context:
space:
mode:
authorspag <spag@golwen.net>2013-02-14 10:42:39 +0100
committerspag <spag@golwen.net>2013-02-14 10:42:39 +0100
commit41303b0a2a07b897f7373eebba25708da89085a1 (patch)
tree88e48da31bdd92d80af76c18d0df992f7d09f91b /app/models/fax_document.rb
parenta1aafe9eec98f91fd76d104964ad98fcafc94a80 (diff)
parenta9bf3a7e1fd802025824668e477a5ec14ce81d5a (diff)
Merge branch 'develop' of github.com:amooma/GS5 into develop
Diffstat (limited to 'app/models/fax_document.rb')
-rw-r--r--app/models/fax_document.rb32
1 files changed, 19 insertions, 13 deletions
diff --git a/app/models/fax_document.rb b/app/models/fax_document.rb
index 16fdc70..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')
@@ -47,19 +47,25 @@ 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|
+ 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'")
+ 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 +73,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