module DocsplitImages::Conversion
Public Class Methods
included(base)
click to toggle source
# File lib/docsplit_images/conversion.rb, line 3 def self.included(base) base.before_save :check_for_file_change base.after_save :docsplit_images def check_for_file_change @file_has_changed = self.send(self.class.docsplit_attachment_name).dirty? if @file_has_changed == true self.is_processing_image = true end true end def docsplit_images if self.send(self.class.docsplit_attachment_name).exists? and self.is_pdf_convertible? and @file_has_changed == true self.delay.docsplit_images_process end true end def docsplit_images_process parent_dir = File.dirname(File.dirname(self.send(self.class.docsplit_attachment_name).path)) FileUtils.rm_rf("#{parent_dir}/images") FileUtils.mkdir("#{parent_dir}/images") doc_path = self.send(self.class.docsplit_attachment_name).path ext = File.extname(doc_path) temp_pdf_path = if ext.downcase == '.pdf' doc_path else tempdir = File.join(Dir.tmpdir, 'docsplit') Docsplit.extract_pdf([doc_path], {:output => tempdir}) File.join(tempdir, File.basename(doc_path, ext) + '.pdf') end self.number_of_images_entry = Docsplit.extract_length(temp_pdf_path) self.save(validate: false) # Going to convert to images Docsplit::ImageExtractor.new.extract(temp_pdf_path, self.class.docsplit_attachment_options.merge({:output => "#{parent_dir}/images"})) @file_has_changed = false self.is_processing_image = false self.save(:validate => false) end def number_of_completed_images parent_dir = File.dirname(File.dirname(self.send(self.class.docsplit_attachment_name).path)) return Dir.entries("#{parent_dir}/images").size - 2 end # return the progress in term of percentage def images_conversion_progress return ("%.2f" % (number_of_completed_images * 1.0 / self.number_of_images_entry)).to_f if self.is_pdf_convertible? return 1 end ## paperclip overriding def prepare_for_destroy begin parent_dir = File.dirname(File.dirname(self.send(self.class.docsplit_attachment_name).path)) FileUtils.rm_rf("#{parent_dir}/images") rescue end super end def is_pdf_convertible? extname = File.extname(self.send("#{self.class.docsplit_attachment_name}_file_name")).downcase.gsub(".", "") return ['doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx', 'odt', 'ods', 'odp', 'pdf'].index( extname ) != nil end def document_images_folder return "#{File.dirname(File.dirname(self.send(self.class.docsplit_attachment_name).url))}/images" end def document_images_list if self.is_pdf_convertible? list = [] image_base_folder = document_images_folder original_file_name = File.basename(self.send(self.class.docsplit_attachment_name).url, ".*") for i in 1..self.number_of_images_entry list.push("#{image_base_folder}/#{original_file_name}_#{i}.png") end return list else return [] end end end
Public Instance Methods
check_for_file_change()
click to toggle source
# File lib/docsplit_images/conversion.rb, line 8 def check_for_file_change @file_has_changed = self.send(self.class.docsplit_attachment_name).dirty? if @file_has_changed == true self.is_processing_image = true end true end
docsplit_images()
click to toggle source
# File lib/docsplit_images/conversion.rb, line 16 def docsplit_images if self.send(self.class.docsplit_attachment_name).exists? and self.is_pdf_convertible? and @file_has_changed == true self.delay.docsplit_images_process end true end
docsplit_images_process()
click to toggle source
# File lib/docsplit_images/conversion.rb, line 23 def docsplit_images_process parent_dir = File.dirname(File.dirname(self.send(self.class.docsplit_attachment_name).path)) FileUtils.rm_rf("#{parent_dir}/images") FileUtils.mkdir("#{parent_dir}/images") doc_path = self.send(self.class.docsplit_attachment_name).path ext = File.extname(doc_path) temp_pdf_path = if ext.downcase == '.pdf' doc_path else tempdir = File.join(Dir.tmpdir, 'docsplit') Docsplit.extract_pdf([doc_path], {:output => tempdir}) File.join(tempdir, File.basename(doc_path, ext) + '.pdf') end self.number_of_images_entry = Docsplit.extract_length(temp_pdf_path) self.save(validate: false) # Going to convert to images Docsplit::ImageExtractor.new.extract(temp_pdf_path, self.class.docsplit_attachment_options.merge({:output => "#{parent_dir}/images"})) @file_has_changed = false self.is_processing_image = false self.save(:validate => false) end
document_images_folder()
click to toggle source
# File lib/docsplit_images/conversion.rb, line 72 def document_images_folder return "#{File.dirname(File.dirname(self.send(self.class.docsplit_attachment_name).url))}/images" end
document_images_list()
click to toggle source
# File lib/docsplit_images/conversion.rb, line 76 def document_images_list if self.is_pdf_convertible? list = [] image_base_folder = document_images_folder original_file_name = File.basename(self.send(self.class.docsplit_attachment_name).url, ".*") for i in 1..self.number_of_images_entry list.push("#{image_base_folder}/#{original_file_name}_#{i}.png") end return list else return [] end end
images_conversion_progress()
click to toggle source
return the progress in term of percentage
# File lib/docsplit_images/conversion.rb, line 52 def images_conversion_progress return ("%.2f" % (number_of_completed_images * 1.0 / self.number_of_images_entry)).to_f if self.is_pdf_convertible? return 1 end
is_pdf_convertible?()
click to toggle source
# File lib/docsplit_images/conversion.rb, line 67 def is_pdf_convertible? extname = File.extname(self.send("#{self.class.docsplit_attachment_name}_file_name")).downcase.gsub(".", "") return ['doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx', 'odt', 'ods', 'odp', 'pdf'].index( extname ) != nil end
number_of_completed_images()
click to toggle source
# File lib/docsplit_images/conversion.rb, line 46 def number_of_completed_images parent_dir = File.dirname(File.dirname(self.send(self.class.docsplit_attachment_name).path)) return Dir.entries("#{parent_dir}/images").size - 2 end
prepare_for_destroy()
click to toggle source
paperclip overriding
Calls superclass method
# File lib/docsplit_images/conversion.rb, line 58 def prepare_for_destroy begin parent_dir = File.dirname(File.dirname(self.send(self.class.docsplit_attachment_name).path)) FileUtils.rm_rf("#{parent_dir}/images") rescue end super end