]> git.ipfire.org Git - thirdparty/paperless-ngx.git/commitdiff
Handle a special case where the none marker exists in a way which could create an...
authorTrenton H <797416+stumpylog@users.noreply.github.com>
Mon, 28 Oct 2024 01:51:07 +0000 (18:51 -0700)
committerGitHub <noreply@github.com>
Mon, 28 Oct 2024 01:51:07 +0000 (01:51 +0000)
src/documents/file_handling.py
src/documents/tests/test_file_handling.py

index 6d02bf68468079a024b8768f5a708dcd8bec4eb2..4198ecabbb1047d814184a1c62433c03eeb23d09 100644 (file)
@@ -111,6 +111,7 @@ def generate_filename(
             rendered_filename = rendered_filename.replace("/-none-/", "/")
             rendered_filename = rendered_filename.replace(" -none-", "")
             rendered_filename = rendered_filename.replace("-none-", "")
+            rendered_filename = rendered_filename.strip(os.sep)
 
         rendered_filename = rendered_filename.replace(
             "-none-",
index 037e47f06268f75db1859f776bac5e6be4577a23..e942b4daa6eee9e522b23d62a8bf49b24d923a59 100644 (file)
@@ -960,18 +960,28 @@ class TestFilenameGeneration(DirectoriesMixin, TestCase):
             - the generated filename uses the defined storage path for the document
             - the generated filename does not include "none" in the place undefined field
         """
+        sp = StoragePath.objects.create(
+            path="TestFolder/{{asn}}/{{created}}",
+        )
         doc = Document.objects.create(
             title="does not matter",
             created=timezone.make_aware(datetime.datetime(2020, 6, 25, 7, 36, 51, 153)),
             mime_type="application/pdf",
             pk=2,
             checksum="2",
-            storage_path=StoragePath.objects.create(
-                path="TestFolder/{{asn}}/{{created}}",
-            ),
+            storage_path=sp,
         )
         self.assertEqual(generate_filename(doc), "TestFolder/2020-06-25.pdf")
 
+        # Special case, undefined variable, then defined at the start of the template
+        # This could lead to an absolute path after we remove the leading -none-, but leave the leading /
+        # -none-/2020/ -> /2020/
+        sp.path = (
+            "{{ owner_username }}/{{ created_year }}/{{ correspondent }}/{{ title }}"
+        )
+        sp.save()
+        self.assertEqual(generate_filename(doc), "2020/does not matter.pdf")
+
     def test_multiple_doc_paths(self):
         """
         GIVEN: