]> git.ipfire.org Git - thirdparty/paperless-ngx.git/commitdiff
When splitting via barcodes, cleanup the split documents better
authorTrenton Holmes <797416+stumpylog@users.noreply.github.com>
Sun, 12 Feb 2023 02:19:02 +0000 (18:19 -0800)
committerTrenton H <797416+stumpylog@users.noreply.github.com>
Sun, 12 Feb 2023 16:20:12 +0000 (08:20 -0800)
src/documents/barcodes.py
src/documents/tasks.py

index 9adb8aeeaf4c4e78d922852040cf22d7af471367..416cf6b2d7352f4cad51d3a8d52508964f124e2e 100644 (file)
@@ -325,11 +325,10 @@ def save_to_dir(
     Optionally rename the file.
     """
     if os.path.isfile(filepath) and os.path.isdir(target_dir):
-        dst = shutil.copy(filepath, target_dir)
-        logging.debug(f"saved {str(filepath)} to {str(dst)}")
-        if newname:
-            dst_new = os.path.join(target_dir, newname)
-            logger.debug(f"moving {str(dst)} to {str(dst_new)}")
-            os.rename(dst, dst_new)
+        dest = target_dir
+        if newname is not None:
+            dest = os.path.join(dest, newname)
+        shutil.copy(filepath, dest)
+        logging.debug(f"saved {str(filepath)} to {str(dest)}")
     else:
         logger.warning(f"{str(filepath)} or {str(target_dir)} don't exist.")
index 34b75ce121fff28b7bc52507a5e21a7d1661788a..1f9c94917b3f293303ce7a01af914fc0726277e3 100644 (file)
@@ -128,6 +128,18 @@ def consume_file(
                 )
 
                 if document_list:
+
+                    # If the file is an upload, it's in the scratch directory
+                    # Move it to consume directory to be picked up
+                    # Otherwise, use the current parent to keep possible tags
+                    # from subdirectories
+                    try:
+                        # is_relative_to would be nicer, but new in 3.9
+                        _ = path.relative_to(settings.SCRATCH_DIR)
+                        save_to_dir = settings.CONSUMPTION_DIR
+                    except ValueError:
+                        save_to_dir = path.parent
+
                     for n, document in enumerate(document_list):
                         # save to consumption dir
                         # rename it to the original filename  with number prefix
@@ -136,23 +148,18 @@ def consume_file(
                         else:
                             newname = None
 
-                        # If the file is an upload, it's in the scratch directory
-                        # Move it to consume directory to be picked up
-                        # Otherwise, use the current parent to keep possible tags
-                        # from subdirectories
-                        try:
-                            # is_relative_to would be nicer, but new in 3.9
-                            _ = path.relative_to(settings.SCRATCH_DIR)
-                            save_to_dir = settings.CONSUMPTION_DIR
-                        except ValueError:
-                            save_to_dir = path.parent
-
                         barcodes.save_to_dir(
                             document,
                             newname=newname,
                             target_dir=save_to_dir,
                         )
 
+                        # Split file has been copied safely, remove it
+                        os.remove(document)
+
+                    # And clean up the directory as well, now it's empty
+                    shutil.rmtree(os.path.dirname(document_list[0]))
+
                     # Delete the PDF file which was split
                     os.remove(doc_barcode_info.pdf_path)