mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2026-12-03 17:58:41 +00:00
Fix: remove the superseded archive file when a reprocess changes its path
Reprocessing a document writes the new archive to the path its current metadata generates and updates archive_filename, but never removed the file the old archive_filename pointed at. Normally both are the same path. They differ when a committed edit has not had its file move applied yet, which deferred file moves make a real window: the new archive lands at the new path and the old one stays on disk with no document referencing it. The deferred move then finds the archive already at its target and only moves the original. Delete the replaced archive, and its directory if that leaves it empty, once the new one is in place and only when the two paths differ.
This commit is contained in:
1 parent
c8ce651111
commit
7560a91204
2 files changed
+120
No files matched your search
@@ -39,6 +39,7 @@ from documents.data_models import DocumentMetadataOverrides
|
||||
from documents.data_models import StoredBarcode
|
||||
from documents.double_sided import CollatePlugin
|
||||
from documents.file_handling import create_source_path_directory
|
||||
from documents.file_handling import delete_empty_directories
|
||||
from documents.file_handling import generate_unique_filename
|
||||
from documents.matching import prefilter_documents_by_workflowtrigger
|
||||
from documents.media_lock import media_lock
|
||||
@@ -516,6 +517,18 @@ def update_document_content_maybe_archive_file(
|
||||
if parser.get_archive_path():
|
||||
create_source_path_directory(document.archive_path)
|
||||
shutil.move(parser.get_archive_path(), document.archive_path)
|
||||
# A rename that has not been applied yet puts the new archive
|
||||
# at a different path, so the one it replaces must go.
|
||||
replaced_archive_path = oldDocument.archive_path
|
||||
if (
|
||||
replaced_archive_path is not None
|
||||
and replaced_archive_path != document.archive_path
|
||||
):
|
||||
replaced_archive_path.unlink(missing_ok=True)
|
||||
delete_empty_directories(
|
||||
replaced_archive_path.parent,
|
||||
root=settings.ARCHIVE_DIR,
|
||||
)
|
||||
shutil.move(thumbnail, document.thumbnail_path)
|
||||
|
||||
document.refresh_from_db()
|
||||
|
||||
@@ -0,0 +1,107 @@
|
||||
import shutil
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
from pytest_django.fixtures import Settings
|
||||
|
||||
from documents import tasks
|
||||
from documents.models import Correspondent
|
||||
from documents.models import Document
|
||||
from documents.utils import compute_checksum
|
||||
from paperless_testing.dirs import PaperlessDirs
|
||||
from paperless_testing.samples import SIMPLE_DIGITAL_PDF
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
class TestReprocessArchiveCleanup:
|
||||
def test_reprocess_with_pending_rename_leaves_one_archive(
|
||||
self,
|
||||
paperless_dirs: PaperlessDirs,
|
||||
settings: Settings,
|
||||
) -> None:
|
||||
"""
|
||||
GIVEN:
|
||||
- A document whose committed metadata already generates a different
|
||||
filename than the one its files still have, because its file move
|
||||
has not run yet
|
||||
WHEN:
|
||||
- The document is reprocessed and a new archive file is produced
|
||||
THEN:
|
||||
- Exactly one archive file remains, the one the document points at
|
||||
- The superseded archive file is not left behind
|
||||
"""
|
||||
settings.FILENAME_FORMAT = "{{ correspondent }}/{{ title }}"
|
||||
settings.ARCHIVE_FILE_GENERATION = "always"
|
||||
old_correspondent = Correspondent.objects.create(name="old")
|
||||
new_correspondent = Correspondent.objects.create(name="new")
|
||||
|
||||
original = paperless_dirs.originals_dir / "old" / "t.pdf"
|
||||
archive = paperless_dirs.archive_dir / "old" / "t.pdf"
|
||||
for path in (original, archive):
|
||||
path.parent.mkdir(parents=True)
|
||||
shutil.copy(SIMPLE_DIGITAL_PDF, path)
|
||||
doc = Document.objects.create(
|
||||
title="t",
|
||||
content="my document",
|
||||
checksum=compute_checksum(original),
|
||||
archive_checksum="stale",
|
||||
filename="old/t.pdf",
|
||||
archive_filename="old/t.pdf",
|
||||
mime_type="application/pdf",
|
||||
correspondent=old_correspondent,
|
||||
)
|
||||
# A queryset update sends no signals, so no file move is requested, as
|
||||
# with a committed edit whose deferred move has not run yet.
|
||||
Document.objects.filter(pk=doc.pk).update(correspondent=new_correspondent)
|
||||
|
||||
tasks.update_document_content_maybe_archive_file(doc.pk)
|
||||
|
||||
refreshed = Document.objects.get(pk=doc.pk)
|
||||
archives = sorted(
|
||||
p.relative_to(paperless_dirs.archive_dir).as_posix()
|
||||
for p in Path(paperless_dirs.archive_dir).rglob("*.pdf")
|
||||
)
|
||||
assert archives == [refreshed.archive_filename]
|
||||
assert refreshed.archive_filename == "new/t.pdf"
|
||||
assert (paperless_dirs.originals_dir / refreshed.filename).is_file()
|
||||
|
||||
def test_reprocess_without_pending_rename_keeps_archive_in_place(
|
||||
self,
|
||||
paperless_dirs: PaperlessDirs,
|
||||
settings: Settings,
|
||||
) -> None:
|
||||
"""
|
||||
GIVEN:
|
||||
- A document whose files already match its generated filename
|
||||
WHEN:
|
||||
- The document is reprocessed and a new archive file is produced
|
||||
THEN:
|
||||
- The new archive replaces the old one at the same path and is not
|
||||
deleted as a superseded file
|
||||
"""
|
||||
settings.FILENAME_FORMAT = "{{ correspondent }}/{{ title }}"
|
||||
settings.ARCHIVE_FILE_GENERATION = "always"
|
||||
correspondent = Correspondent.objects.create(name="same")
|
||||
|
||||
original = paperless_dirs.originals_dir / "same" / "t.pdf"
|
||||
archive = paperless_dirs.archive_dir / "same" / "t.pdf"
|
||||
for path in (original, archive):
|
||||
path.parent.mkdir(parents=True)
|
||||
shutil.copy(SIMPLE_DIGITAL_PDF, path)
|
||||
doc = Document.objects.create(
|
||||
title="t",
|
||||
content="my document",
|
||||
checksum=compute_checksum(original),
|
||||
archive_checksum="stale",
|
||||
filename="same/t.pdf",
|
||||
archive_filename="same/t.pdf",
|
||||
mime_type="application/pdf",
|
||||
correspondent=correspondent,
|
||||
)
|
||||
|
||||
tasks.update_document_content_maybe_archive_file(doc.pk)
|
||||
|
||||
refreshed = Document.objects.get(pk=doc.pk)
|
||||
assert refreshed.archive_filename == "same/t.pdf"
|
||||
assert archive.is_file()
|
||||
assert compute_checksum(archive) == refreshed.archive_checksum
|
||||
Reference in new issue
Block a user