Module: DocsplitImages::Conversion
- Defined in:
- lib/docsplit_images/conversion.rb
Class Method Summary collapse
Instance Method Summary collapse
- #check_for_file_change ⇒ Object
- #docsplit_images ⇒ Object
- #docsplit_images_process ⇒ Object
- #document_images_folder ⇒ Object
- #document_images_list ⇒ Object
-
#images_conversion_progress ⇒ Object
return the progress in term of percentage.
- #is_pdf_convertible? ⇒ Boolean
- #number_of_completed_images ⇒ Object
-
#prepare_for_destroy ⇒ Object
paperclip overriding.
Class Method Details
.included(base) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# 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.).dirty? if @file_has_changed == true self.is_processing_image = true end true end def docsplit_images if self.send(self.class.).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.).path)) FileUtils.rm_rf("#{parent_dir}/images") FileUtils.mkdir("#{parent_dir}/images") doc_path = self.send(self.class.).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..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.).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.).path)) FileUtils.rm_rf("#{parent_dir}/images") rescue end super end def is_pdf_convertible? extname = File.extname(self.send("#{self.class.}_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.).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.).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 |
Instance Method Details
#check_for_file_change ⇒ Object
8 9 10 11 12 13 14 |
# File 'lib/docsplit_images/conversion.rb', line 8 def check_for_file_change @file_has_changed = self.send(self.class.).dirty? if @file_has_changed == true self.is_processing_image = true end true end |
#docsplit_images ⇒ Object
16 17 18 19 20 21 |
# File 'lib/docsplit_images/conversion.rb', line 16 def docsplit_images if self.send(self.class.).exists? and self.is_pdf_convertible? and @file_has_changed == true self.delay.docsplit_images_process end true end |
#docsplit_images_process ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/docsplit_images/conversion.rb', line 23 def docsplit_images_process parent_dir = File.dirname(File.dirname(self.send(self.class.).path)) FileUtils.rm_rf("#{parent_dir}/images") FileUtils.mkdir("#{parent_dir}/images") doc_path = self.send(self.class.).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..merge({:output => "#{parent_dir}/images"})) @file_has_changed = false self.is_processing_image = false self.save(:validate => false) end |
#document_images_folder ⇒ Object
72 73 74 |
# File 'lib/docsplit_images/conversion.rb', line 72 def document_images_folder return "#{File.dirname(File.dirname(self.send(self.class.).url))}/images" end |
#document_images_list ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 |
# 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.).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 ⇒ Object
return the progress in term of percentage
52 53 54 55 |
# 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? ⇒ Boolean
67 68 69 70 |
# File 'lib/docsplit_images/conversion.rb', line 67 def is_pdf_convertible? extname = File.extname(self.send("#{self.class.}_file_name")).downcase.gsub(".", "") return ['doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx', 'odt', 'ods', 'odp', 'pdf'].index( extname ) != nil end |
#number_of_completed_images ⇒ Object
46 47 48 49 |
# File 'lib/docsplit_images/conversion.rb', line 46 def number_of_completed_images parent_dir = File.dirname(File.dirname(self.send(self.class.).path)) return Dir.entries("#{parent_dir}/images").size - 2 end |
#prepare_for_destroy ⇒ Object
paperclip overriding
58 59 60 61 62 63 64 65 |
# File 'lib/docsplit_images/conversion.rb', line 58 def prepare_for_destroy begin parent_dir = File.dirname(File.dirname(self.send(self.class.).path)) FileUtils.rm_rf("#{parent_dir}/images") rescue end super end |