Enhancement: better try to catch db errors before unlink

This commit is contained in:
shamoon 2025-06-23 09:19:28 -07:00
parent 37267f3f04
commit 0f6e9d4343
No known key found for this signature in database

View File

@ -530,24 +530,31 @@ class ConsumerPlugin(
# Don't save with the lock active. Saving will cause the file # Don't save with the lock active. Saving will cause the file
# renaming logic to acquire the lock as well. # renaming logic to acquire the lock as well.
# This triggers things like file renaming # This triggers things like file renaming
document.save() 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()
# Delete the file only if it was successfully consumed # https://github.com/jonaswinkler/paperless-ng/discussions/1037
self.log.debug(f"Deleting file {self.working_copy}") shadow_file = (
self.input_doc.original_file.unlink() Path(self.input_doc.original_file).parent
self.working_copy.unlink() / f"._{Path(self.input_doc.original_file).name}"
if self.unmodified_original is not None: # pragma: no cover )
self.unmodified_original.unlink()
# https://github.com/jonaswinkler/paperless-ng/discussions/1037 if Path(shadow_file).is_file():
shadow_file = ( self.log.debug(f"Deleting file {shadow_file}")
Path(self.input_doc.original_file).parent Path(shadow_file).unlink()
/ f"._{Path(self.input_doc.original_file).name}" else:
) self.log.warning(
f"Document was not saved. Skipping file deletion for {self.filename}",
if Path(shadow_file).is_file(): )
self.log.debug(f"Deleting file {shadow_file}")
Path(shadow_file).unlink()
except Exception as e: except Exception as e:
self._fail( self._fail(