Repository: paperless-ngx/paperless-ngx

Issue:
[BUG] FILENAME_FORMAT_REMOVE_NONE breaks certain storage paths
### Description

While investigating https://github.com/paperless-ngx/paperless-ngx/discussions/4856 I came across the following bug: 
The storage path `XX{correspondent}/{title}` will not work correctly, if the correspondent is missing and  `FILENAME_FORMAT_REMOVE_NONE` is `true`

In other words, the following test will fail, but should not:
```python
    @override_settings(
        FILENAME_FORMAT="XX{correspondent}/{title}",
        FILENAME_FORMAT_REMOVE_NONE=True,
    )
    def test_nested_directory_cleanup2(self):
        document = Document.objects.create(
            title="doc1",
            mime_type="application/pdf",
        )
        document.storage_type = Document.STORAGE_TYPE_UNENCRYPTED
        document.save()

        # Ensure that filename is properly generated
        document.filename = generate_filename(document)
        self.assertEqual(document.filename, "XX/doc1.pdf")
```

### Steps to reproduce

1. Make sure `FILENAME_FORMAT_REMOVE_NONE=true`
2. Create a storage path `XX{correspondent}/{title}`
3. make sure the correspondent is missing
4. Apply the storage path to a document
5. observe that the generated path is `XXtitle.pdf`, not `XX/title.pdf` 

### Webserver logs

```bash
none
```


### Browser logs

_No response_

### Paperless-ngx version

2.2.1

### Host OS

any

### Installation method

Docker - official image

### Browser

_No response_

### Configuration changes

_No response_

### Other

Happy to fix this myself, but I am unsure what the correct approach is. I would usually suggest to use a regex here, but I saw that none are used in that code and I am wondering if there is a reason for this?

### Please confirm the following

- [X] I believe this issue is a bug that affects all users of Paperless-ngx, not something specific to my installation.
- [X] I have already searched for relevant existing issues and discussions before opening this report.
- [X] I have updated the title field above with a concise description.


Discussion:

Resolving patch:
diff --git a/src/documents/file_handling.py b/src/documents/file_handling.py
--- a/src/documents/file_handling.py
+++ b/src/documents/file_handling.py
@@ -222,7 +222,7 @@ def generate_filename(
             ).strip()
 
             if settings.FILENAME_FORMAT_REMOVE_NONE:
-                path = path.replace("-none-/", "")  # remove empty directories
+                path = path.replace("/-none-/", "/")  # remove empty directories
                 path = path.replace(" -none-", "")  # remove when spaced, with space
                 path = path.replace("-none-", "")  # remove rest of the occurences
 


Test patch:
diff --git a/src/documents/tests/test_file_handling.py b/src/documents/tests/test_file_handling.py
--- a/src/documents/tests/test_file_handling.py
+++ b/src/documents/tests/test_file_handling.py
@@ -1007,6 +1007,9 @@ def test_multiple_doc_paths(self):
         self.assertEqual(generate_filename(doc_a), "ThisIsAFolder/4/2020-06-25.pdf")
         self.assertEqual(generate_filename(doc_b), "SomeImportantNone/2020-07-25.pdf")
 
+    @override_settings(
+        FILENAME_FORMAT=None,
+    )
     def test_no_path_fallback(self):
         """
         GIVEN:
@@ -1157,3 +1160,28 @@ def test_document_original_filename(self):
 
         self.assertEqual(generate_filename(text_doc), "logs.txt")
         self.assertEqual(generate_filename(text_doc, archive_filename=True), "logs.pdf")
+
+    @override_settings(
+        FILENAME_FORMAT="XX{correspondent}/{title}",
+        FILENAME_FORMAT_REMOVE_NONE=True,
+    )
+    def test_remove_none_not_dir(self):
+        """
+        GIVEN:
+            - A document with & filename format that includes correspondent as part of directory name
+            - FILENAME_FORMAT_REMOVE_NONE is True
+        WHEN:
+            - the filename is generated for the document
+        THEN:
+            - the missing correspondent is removed but directory structure retained
+        """
+        document = Document.objects.create(
+            title="doc1",
+            mime_type="application/pdf",
+        )
+        document.storage_type = Document.STORAGE_TYPE_UNENCRYPTED
+        document.save()
+
+        # Ensure that filename is properly generated
+        document.filename = generate_filename(document)
+        self.assertEqual(document.filename, "XX/doc1.pdf")