A community-supported supercharged document management system: scan, index and archive all your documents
Python aiangulararchivingdjangodmsdocument-managementdocument-management-systemllmmachine-learningocroptical-character-recognitionpdf
code review changes
Florian Brandesa7b1658ee1a9
diff --git a/src/documents/tasks.py b/src/documents/tasks.pyindex c25b24342..9fe58b325 100644--- a/src/documents/tasks.py+++ b/src/documents/tasks.py@@ -108,23 +108,22 @@ def convert_from_tiff_to_pdf(filepath: str) -> str:newpath = os.path.join(tempdir, file_name + ".pdf")else:logger.warning(f"Cannot convert from {str(file_extension)} to pdf.")- return ""- image = Image.open(filepath)- images = []- for i, page in enumerate(ImageSequence.Iterator(image)):- page = page.convert("RGB")- images.append(page)- try:- if len(images) == 1:- images[0].save(newpath)- else:- images[0].save(newpath, save_all=True, append_images=images[1:])- except OSError as e:- logger.warning(- f"Could not save the file as pdf. Error: {str(e)}",- )- return ""- image.close()+ return None+ with Image.open(filepath) as image:+ images = []+ for i, page in enumerate(ImageSequence.Iterator(image)):+ page = page.convert("RGB")+ images.append(page)+ try:+ if len(images) == 1:+ images[0].save(newpath)+ else:+ images[0].save(newpath, save_all=True, append_images=images[1:])+ except OSError as e:+ logger.warning(+ f"Could not save the file as pdf. Error: {str(e)}",+ )+ return Nonereturn newpath@@ -242,23 +241,19 @@ def consume_file(f"Unsupported file format for barcode reader: {str(file_extension)}",)else:- if file_extension == ".tif" or file_extension == ".tiff":- converted_tiff = convert_from_tiff_to_pdf(path)- if converted_tiff:- separators = scan_file_for_separating_barcodes(converted_tiff)+ if file_extension in {".tif", ".tiff"}:+ file_to_process = convert_from_tiff_to_pdf(path)else:- separators = scan_file_for_separating_barcodes(path)+ file_to_process = path++ separators = scan_file_for_separating_barcodes(file_to_process)+if separators:- if converted_tiff:- logger.debug(- f"Pages with separators found in: {str(converted_tiff)}",- )- document_list = separate_pages(converted_tiff, separators)- else:- logger.debug(- f"Pages with separators found in: {str(path)}",- )- document_list = separate_pages(path, separators)+ logger.debug(+ f"Pages with separators found in: {str(path)}",+ )+ document_list = separate_pages(file_to_process, separators)+if document_list:for n, document in enumerate(document_list):# save to consumption dir@@ -271,8 +266,8 @@ def consume_file(# if we got here, the document was successfully split# and can safely be deletedif converted_tiff:- logger.debug("Deleting file {}".format(converted_tiff))- os.unlink(converted_tiff)+ logger.debug("Deleting file {}".format(file_to_process))+ os.unlink(file_to_process)logger.debug("Deleting file {}".format(path))os.unlink(path)# notify the sender, otherwise the progress bardiff --git a/src/documents/tests/test_tasks.py b/src/documents/tests/test_tasks.pyindex baff2f534..7e5381def 100644--- a/src/documents/tests/test_tasks.py+++ b/src/documents/tests/test_tasks.py@@ -225,8 +225,7 @@ class TestTasks(DirectoriesMixin, TestCase):)dst = os.path.join(settings.SCRATCH_DIR, "simple.pdf")shutil.copy(test_file, dst)- target_file = tasks.convert_from_tiff_to_pdf(dst)- self.assertFalse(os.path.isfile(target_file))+ self.assertIsNone(tasks.convert_from_tiff_to_pdf(dst))def test_scan_file_for_separating_barcodes(self):test_file = os.path.join(