mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2026-01-26 22:49:01 -06:00
Compare commits
1 Commits
feature-pw
...
feature/lo
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b36cfab43e |
@@ -19,6 +19,7 @@ from django.db import DatabaseError
|
||||
from django.db import close_old_connections
|
||||
from django.db import connections
|
||||
from django.db import models
|
||||
from django.db import transaction
|
||||
from django.db.models import Q
|
||||
from django.dispatch import receiver
|
||||
from django.utils import timezone
|
||||
@@ -453,8 +454,17 @@ def update_filename_and_move_files(
|
||||
# This will in turn cause this logic to move the file where it belongs.
|
||||
return
|
||||
|
||||
with FileLock(settings.MEDIA_LOCK):
|
||||
move_original = False
|
||||
move_archive = False
|
||||
old_filename = None
|
||||
old_archive_filename = None
|
||||
old_source_path = None
|
||||
old_archive_path = None
|
||||
|
||||
try:
|
||||
with transaction.atomic():
|
||||
Document.global_objects.select_for_update().get(pk=instance.pk)
|
||||
with FileLock(settings.MEDIA_LOCK):
|
||||
# If this was waiting for the lock, the filename or archive_filename
|
||||
# of this document may have been updated. This happens if multiple updates
|
||||
# get queued from the UI for the same document
|
||||
@@ -487,7 +497,10 @@ def update_filename_and_move_files(
|
||||
old_archive_path = instance.archive_path
|
||||
|
||||
if instance.has_archive_version:
|
||||
archive_candidate = generate_filename(instance, archive_filename=True)
|
||||
archive_candidate = generate_filename(
|
||||
instance,
|
||||
archive_filename=True,
|
||||
)
|
||||
archive_candidate_path = (
|
||||
settings.ARCHIVE_DIR / archive_candidate
|
||||
).resolve()
|
||||
@@ -510,13 +523,6 @@ def update_filename_and_move_files(
|
||||
else:
|
||||
move_archive = False
|
||||
|
||||
if not move_original and not move_archive:
|
||||
# Just update modified. Also, don't save() here to prevent infinite recursion.
|
||||
Document.objects.filter(pk=instance.pk).update(
|
||||
modified=timezone.now(),
|
||||
)
|
||||
return
|
||||
|
||||
if move_original:
|
||||
validate_move(
|
||||
instance,
|
||||
@@ -537,6 +543,13 @@ def update_filename_and_move_files(
|
||||
create_source_path_directory(instance.archive_path)
|
||||
shutil.move(old_archive_path, instance.archive_path)
|
||||
|
||||
if not move_original and not move_archive:
|
||||
# Just update modified. Also, don't save() here to prevent infinite recursion.
|
||||
Document.objects.filter(pk=instance.pk).update(
|
||||
modified=timezone.now(),
|
||||
)
|
||||
return
|
||||
|
||||
# Don't save() here to prevent infinite recursion.
|
||||
Document.global_objects.filter(pk=instance.pk).update(
|
||||
filename=instance.filename,
|
||||
@@ -553,8 +566,10 @@ def update_filename_and_move_files(
|
||||
# - saving to the database failed due to database errors
|
||||
# In both cases, we need to revert to the original state.
|
||||
|
||||
if move_original or move_archive:
|
||||
# Try to move files to their original location.
|
||||
try:
|
||||
with FileLock(settings.MEDIA_LOCK):
|
||||
if move_original and instance.source_path.is_file():
|
||||
logger.info("Restoring previous original path")
|
||||
shutil.move(instance.source_path, old_source_path)
|
||||
@@ -575,18 +590,24 @@ def update_filename_and_move_files(
|
||||
pass
|
||||
|
||||
# restore old values on the instance
|
||||
if old_filename is not None:
|
||||
instance.filename = old_filename
|
||||
if old_archive_filename is not None:
|
||||
instance.archive_filename = old_archive_filename
|
||||
|
||||
# finally, remove any empty sub folders. This will do nothing if
|
||||
# something has failed above.
|
||||
if not old_source_path.is_file():
|
||||
if old_source_path and not old_source_path.is_file():
|
||||
delete_empty_directories(
|
||||
Path(old_source_path).parent,
|
||||
root=settings.ORIGINALS_DIR,
|
||||
)
|
||||
|
||||
if instance.has_archive_version and not old_archive_path.is_file():
|
||||
if (
|
||||
instance.has_archive_version
|
||||
and old_archive_path
|
||||
and not old_archive_path.is_file()
|
||||
):
|
||||
delete_empty_directories(
|
||||
Path(old_archive_path).parent,
|
||||
root=settings.ARCHIVE_DIR,
|
||||
|
||||
Reference in New Issue
Block a user