diff --git a/docs/configuration.rst b/docs/configuration.rst index 248d7a492..20cd2941f 100644 --- a/docs/configuration.rst +++ b/docs/configuration.rst @@ -624,12 +624,17 @@ PAPERLESS_CONSUMER_ENABLE_BARCODES= If no barcodes are detected in the uploaded file, no page separation will happen. + The original document will be removed and the separated pages will be + saved as pdf. + Defaults to false. PAPERLESS_CONSUMER_BARCODE_TIFF_SUPPORT= Whether TIFF image files should be scanned for barcodes. This will automatically convert any TIFF image(s) to pdfs for later processing. + This only has an effect, if PAPERLESS_CONSUMER_ENABLE_BARCODES has been + enabled. Defaults to false. diff --git a/src/documents/tasks.py b/src/documents/tasks.py index dfa2c6bcd..e20554f76 100644 --- a/src/documents/tasks.py +++ b/src/documents/tasks.py @@ -119,20 +119,21 @@ def convert_from_tiff_to_pdf(filepath: str) -> str: images[0].save(newpath) else: images[0].save(newpath, save_all=True, append_images=images[1:]) - os.unlink(filepath) except OSError as e: logger.warning( f"Could not save the file as pdf. " f"The original image file was not deleted. Error: " f"{str(e)}", ) + return "" + os.unlink(filepath) image.close() return newpath def scan_file_for_separating_barcodes(filepath: str) -> List[int]: """ - Scan the provided file for page separating barcodes + Scan the provided pdf file for page separating barcodes Returns a list of pagenumbers, which separate the file """ separator_page_numbers = [] @@ -149,7 +150,7 @@ def scan_file_for_separating_barcodes(filepath: str) -> List[int]: def separate_pages(filepath: str, pages_to_split_on: List[int]) -> List[str]: """ - Separate the provided file on the pages_to_split_on. + Separate the provided pdf file on the pages_to_split_on. The pages which are defined by page_numbers will be removed. Returns a list of (temporary) filepaths to consume. These will need to be deleted later. diff --git a/src/documents/tests/test_tasks.py b/src/documents/tests/test_tasks.py index df9e10077..baff2f534 100644 --- a/src/documents/tests/test_tasks.py +++ b/src/documents/tests/test_tasks.py @@ -445,6 +445,43 @@ class TestTasks(DirectoriesMixin, TestCase): self.assertEqual(tasks.consume_file(dst), "File successfully split") + @override_settings( + CONSUMER_ENABLE_BARCODES=True, + CONSUMER_BARCODE_TIFF_SUPPORT=True, + ) + @mock.patch("documents.consumer.Consumer.try_consume_file") + def test_consume_barcode_unsupported_jpg_file(self, m): + """ + This test assumes barcode and TIFF support are enabled and + the user uploads an unsupported image file (e.g. jpg) + + The function shouldn't try to scan for separating barcodes + and continue archiving the file as is. + """ + test_file = os.path.join( + os.path.dirname(__file__), + "samples", + "simple.jpg", + ) + dst = os.path.join(settings.SCRATCH_DIR, "simple.jpg") + shutil.copy(test_file, dst) + with self.assertLogs("paperless.tasks", level="WARNING") as cm: + self.assertIn("Success", tasks.consume_file(dst)) + self.assertEqual( + cm.output, + [ + "WARNING:paperless.tasks:Unsupported file format for barcode reader: .jpg", + ], + ) + m.assert_called_once() + + args, kwargs = m.call_args + self.assertIsNone(kwargs["override_filename"]) + self.assertIsNone(kwargs["override_title"]) + self.assertIsNone(kwargs["override_correspondent_id"]) + self.assertIsNone(kwargs["override_document_type_id"]) + self.assertIsNone(kwargs["override_tag_ids"]) + @mock.patch("documents.tasks.sanity_checker.check_sanity") def test_sanity_check_success(self, m): m.return_value = SanityCheckMessages()