]> git.ipfire.org Git - thirdparty/paperless-ngx.git/commitdiff
Simplifies file upload naming to use the document name, instead in needing to keep...
authorTrenton Holmes <797416+stumpylog@users.noreply.github.com>
Mon, 26 Dec 2022 21:33:43 +0000 (13:33 -0800)
committerTrenton H <797416+stumpylog@users.noreply.github.com>
Wed, 11 Jan 2023 15:52:58 +0000 (07:52 -0800)
src/documents/tests/test_api.py
src/documents/views.py

index dace4857888455403005e68de86bb05a2d7ccd7d..4547288d2a4d32b52a9098b1f4e119eb8f7b715f 100644 (file)
@@ -7,6 +7,7 @@ import tempfile
 import urllib.request
 import uuid
 import zipfile
+from pathlib import Path
 from unittest import mock
 from unittest.mock import MagicMock
 
@@ -808,7 +809,9 @@ class TestDocumentApi(DirectoriesMixin, APITestCase):
         m.assert_called_once()
 
         args, kwargs = m.call_args
-        self.assertEqual(kwargs["override_filename"], "simple.pdf")
+        file_path = Path(args[0])
+        self.assertEqual(file_path.name, "simple.pdf")
+        self.assertIn(Path(settings.SCRATCH_DIR), file_path.parents)
         self.assertIsNone(kwargs["override_title"])
         self.assertIsNone(kwargs["override_correspondent_id"])
         self.assertIsNone(kwargs["override_document_type_id"])
@@ -833,7 +836,9 @@ class TestDocumentApi(DirectoriesMixin, APITestCase):
         m.assert_called_once()
 
         args, kwargs = m.call_args
-        self.assertEqual(kwargs["override_filename"], "simple.pdf")
+        file_path = Path(args[0])
+        self.assertEqual(file_path.name, "simple.pdf")
+        self.assertIn(Path(settings.SCRATCH_DIR), file_path.parents)
         self.assertIsNone(kwargs["override_title"])
         self.assertIsNone(kwargs["override_correspondent_id"])
         self.assertIsNone(kwargs["override_document_type_id"])
index 46cf06cfd4728c70607f021b747e57fe4ca2ac05..147dd2e078551b5fd339a7dde65c24553de38759 100644 (file)
@@ -7,6 +7,7 @@ import urllib
 import uuid
 import zipfile
 from datetime import datetime
+from pathlib import Path
 from time import mktime
 from unicodedata import normalize
 from urllib.parse import quote
@@ -623,20 +624,18 @@ class PostDocumentView(GenericAPIView):
 
         os.makedirs(settings.SCRATCH_DIR, exist_ok=True)
 
-        with tempfile.NamedTemporaryFile(
-            prefix="paperless-upload-",
-            dir=settings.SCRATCH_DIR,
-            delete=False,
-        ) as f:
-            f.write(doc_data)
-            os.utime(f.name, times=(t, t))
-            temp_filename = f.name
+        temp_file_path = Path(tempfile.mkdtemp(dir=settings.SCRATCH_DIR)) / Path(
+            doc_name,
+        )
+
+        temp_file_path.write_bytes(doc_data)
+
+        os.utime(temp_file_path, times=(t, t))
 
         task_id = str(uuid.uuid4())
 
         async_task = consume_file.delay(
-            temp_filename,
-            override_filename=doc_name,
+            temp_file_path,
             override_title=title,
             override_correspondent_id=correspondent_id,
             override_document_type_id=document_type_id,