mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2025-04-02 13:45:10 -05:00
Replace usages of os.rename with shutil.move to properly handle cases where the source and dest arent't on the same filesystem
This commit is contained in:
parent
613b71d23b
commit
b7577038a0
@ -10,8 +10,8 @@ from documents.models import Document
|
|||||||
from documents.parsers import get_parser_class_for_mime_type
|
from documents.parsers import get_parser_class_for_mime_type
|
||||||
|
|
||||||
|
|
||||||
def _process_document(doc_in):
|
def _process_document(doc_id):
|
||||||
document: Document = Document.objects.get(id=doc_in)
|
document: Document = Document.objects.get(id=doc_id)
|
||||||
parser_class = get_parser_class_for_mime_type(document.mime_type)
|
parser_class = get_parser_class_for_mime_type(document.mime_type)
|
||||||
|
|
||||||
if parser_class:
|
if parser_class:
|
||||||
|
@ -429,12 +429,12 @@ def update_filename_and_move_files(sender, instance: Document, **kwargs):
|
|||||||
if move_original:
|
if move_original:
|
||||||
validate_move(instance, old_source_path, instance.source_path)
|
validate_move(instance, old_source_path, instance.source_path)
|
||||||
create_source_path_directory(instance.source_path)
|
create_source_path_directory(instance.source_path)
|
||||||
os.rename(old_source_path, instance.source_path)
|
shutil.move(old_source_path, instance.source_path)
|
||||||
|
|
||||||
if move_archive:
|
if move_archive:
|
||||||
validate_move(instance, old_archive_path, instance.archive_path)
|
validate_move(instance, old_archive_path, instance.archive_path)
|
||||||
create_source_path_directory(instance.archive_path)
|
create_source_path_directory(instance.archive_path)
|
||||||
os.rename(old_archive_path, instance.archive_path)
|
shutil.move(old_archive_path, instance.archive_path)
|
||||||
|
|
||||||
# Don't save() here to prevent infinite recursion.
|
# Don't save() here to prevent infinite recursion.
|
||||||
Document.objects.filter(pk=instance.pk).update(
|
Document.objects.filter(pk=instance.pk).update(
|
||||||
@ -453,11 +453,11 @@ def update_filename_and_move_files(sender, instance: Document, **kwargs):
|
|||||||
try:
|
try:
|
||||||
if move_original and os.path.isfile(instance.source_path):
|
if move_original and os.path.isfile(instance.source_path):
|
||||||
logger.info("Restoring previous original path")
|
logger.info("Restoring previous original path")
|
||||||
os.rename(instance.source_path, old_source_path)
|
shutil.move(instance.source_path, old_source_path)
|
||||||
|
|
||||||
if move_archive and os.path.isfile(instance.archive_path):
|
if move_archive and os.path.isfile(instance.archive_path):
|
||||||
logger.info("Restoring previous archive path")
|
logger.info("Restoring previous archive path")
|
||||||
os.rename(instance.archive_path, old_archive_path)
|
shutil.move(instance.archive_path, old_archive_path)
|
||||||
|
|
||||||
except Exception:
|
except Exception:
|
||||||
# This is fine, since:
|
# This is fine, since:
|
||||||
|
@ -697,7 +697,7 @@ class TestFileHandlingWithArchive(DirectoriesMixin, FileSystemAssertsMixin, Test
|
|||||||
self.assertIsFile(doc.archive_path)
|
self.assertIsFile(doc.archive_path)
|
||||||
|
|
||||||
@override_settings(FILENAME_FORMAT="{correspondent}/{title}")
|
@override_settings(FILENAME_FORMAT="{correspondent}/{title}")
|
||||||
@mock.patch("documents.signals.handlers.os.rename")
|
@mock.patch("documents.signals.handlers.shutil.move")
|
||||||
def test_move_archive_error(self, m):
|
def test_move_archive_error(self, m):
|
||||||
def fake_rename(src, dst):
|
def fake_rename(src, dst):
|
||||||
if "archive" in str(src):
|
if "archive" in str(src):
|
||||||
@ -748,7 +748,7 @@ class TestFileHandlingWithArchive(DirectoriesMixin, FileSystemAssertsMixin, Test
|
|||||||
self.assertIsFile(doc.archive_path)
|
self.assertIsFile(doc.archive_path)
|
||||||
|
|
||||||
@override_settings(FILENAME_FORMAT="{correspondent}/{title}")
|
@override_settings(FILENAME_FORMAT="{correspondent}/{title}")
|
||||||
@mock.patch("documents.signals.handlers.os.rename")
|
@mock.patch("documents.signals.handlers.shutil.move")
|
||||||
def test_move_file_error(self, m):
|
def test_move_file_error(self, m):
|
||||||
def fake_rename(src, dst):
|
def fake_rename(src, dst):
|
||||||
if "original" in str(src):
|
if "original" in str(src):
|
||||||
|
@ -47,12 +47,16 @@ class TestMakeThumbnails(DirectoriesMixin, FileSystemAssertsMixin, TestCase):
|
|||||||
self.assertIsFile(self.d1.thumbnail_path)
|
self.assertIsFile(self.d1.thumbnail_path)
|
||||||
|
|
||||||
@mock.patch("documents.management.commands.document_thumbnails.shutil.move")
|
@mock.patch("documents.management.commands.document_thumbnails.shutil.move")
|
||||||
def test_process_document_invalid_mime_type(self, m):
|
def test_process_document_invalid_mime_type(self, m: mock.Mock):
|
||||||
self.d1.mime_type = "asdasdasd"
|
self.d1.mime_type = "asdasdasd"
|
||||||
self.d1.save()
|
self.d1.save()
|
||||||
|
|
||||||
|
# .save() triggers filename handling
|
||||||
|
m.reset_mock()
|
||||||
|
|
||||||
_process_document(self.d1.id)
|
_process_document(self.d1.id)
|
||||||
|
|
||||||
|
# Not called during processing of document
|
||||||
m.assert_not_called()
|
m.assert_not_called()
|
||||||
|
|
||||||
def test_command(self):
|
def test_command(self):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user