diff --git a/src/documents/consumer.py b/src/documents/consumer.py index 49ce0c06a..20ce22e3b 100644 --- a/src/documents/consumer.py +++ b/src/documents/consumer.py @@ -467,6 +467,7 @@ class ConsumerPlugin( ) # now that everything is done, we can start to store the document # in the system. This will be a transaction and reasonably fast. + success = False try: with transaction.atomic(): # store the document. @@ -530,31 +531,8 @@ class ConsumerPlugin( # Don't save with the lock active. Saving will cause the file # renaming logic to acquire the lock as well. # This triggers things like file renaming - try: - document.save() - success = True - finally: - if success: - # Delete the file only if it was successfully consumed - self.log.debug(f"Deleting file {self.working_copy}") - self.input_doc.original_file.unlink() - self.working_copy.unlink() - if self.unmodified_original is not None: # pragma: no cover - self.unmodified_original.unlink() - - # https://github.com/jonaswinkler/paperless-ng/discussions/1037 - shadow_file = ( - Path(self.input_doc.original_file).parent - / f"._{Path(self.input_doc.original_file).name}" - ) - - if Path(shadow_file).is_file(): - self.log.debug(f"Deleting file {shadow_file}") - Path(shadow_file).unlink() - else: - self.log.warning( - f"Document was not saved. Skipping file deletion for {self.filename}", - ) + document.save() + success = True except Exception as e: self._fail( @@ -565,25 +543,49 @@ class ConsumerPlugin( exception=e, ) finally: + if success: + # Delete the file only if it was successfully consumed + self.log.debug(f"Deleting file {self.working_copy}") + self.input_doc.original_file.unlink() + self.working_copy.unlink() + if self.unmodified_original is not None: # pragma: no cover + self.unmodified_original.unlink() + + # https://github.com/jonaswinkler/paperless-ng/discussions/1037 + shadow_file = ( + Path(self.input_doc.original_file).parent + / f"._{Path(self.input_doc.original_file).name}" + ) + + if Path(shadow_file).is_file(): + self.log.debug(f"Deleting file {shadow_file}") + Path(shadow_file).unlink() + + self.run_post_consume_script(document) + + self.log.info(f"Document {document} consumption finished") + + self._send_progress( + 100, + 100, + ProgressStatusOptions.SUCCESS, + ConsumerStatusShortMessage.FINISHED, + document.id, + ) + + # Return the most up to date fields + document.refresh_from_db() + + result = f"Success. New document id {document.pk} created" + else: + self._fail( + ConsumerStatusShortMessage.FAILED, + f"Document {self.filename} was not saved.", + ) + document_parser.cleanup() tempdir.cleanup() - - self.run_post_consume_script(document) - - self.log.info(f"Document {document} consumption finished") - - self._send_progress( - 100, - 100, - ProgressStatusOptions.SUCCESS, - ConsumerStatusShortMessage.FINISHED, - document.id, - ) - - # Return the most up to date fields - document.refresh_from_db() - - return f"Success. New document id {document.pk} created" + return result def _parse_title_placeholders(self, title: str) -> str: local_added = timezone.localtime(timezone.now())