Repository: paperless-ngx/paperless-ngx

Issue:
Filename format, Storage paths and handling of too long file names
### Description

Hi,
I started using the new storage paths, which is really useful.
I noticed that a few files were not moved correctly and the default path was used instead. I tried changing the storage path back and forth but no errors were produced and nothing else than an indication of the new storage path in the debug log. document_renamer did not move these few documents to correct location.

I then tried deleting the files to re-import them. That produced an error of too long file names. I had included {tag_list} in the storage path which for these files with a lot of tags gave quite long file names. Removing {tag_list} from the storage path sorted it out and the files now located correctly.

I would assume just changing the storage path in the UI should produce an error similar to below (as was produced when importing), or maybe ideally truncate file name if it gets too long.

### Steps to reproduce

1. Include {tag_list} in a storage path
2. Add lots of (long) tags to a document
3. Change the storage path for the document

### Webserver logs

```bash
During re-import: 
[2022-11-28 13:19:49,128] [INFO] [paperless.handlers] Assigning correspondent Danske Bank to 2020-12-31 2020-12-31 Danske Bank 103 rsoppgave
[2022-11-28 13:19:49,194] [INFO] [paperless.handlers] Tagging "2020-12-31 Danske Bank 2020-12-31 Danske Bank 103 rsoppgave" with "konto-f-db-sparekonto, bank, konto-k-db-brukskonto, konto-k-db-sparekonto, konto-f-db-lån-boligkreditt, konto-f-db-lån-fastrente, therese, skatt, konto-v-db-brukskonto, vilde, konto-k-db-365direkte, konto-k-db-mastercard, konto-f-db-brukskonto, kristian"
[2022-11-28 13:19:49,206] [INFO] [paperless.handlers] Assigning storage path År-Korrespondent to 2020-12-31 Danske Bank 2020-12-31 Danske Bank 103 rsoppgave
[2022-11-28 13:19:49,578] [ERROR] [paperless.consumer] The following error occurred while consuming 2020-12-31 Danske Bank 103 rsoppgave.pdf: [Errno 36] File name too long: '/usr/src/paperless/media/documents/originals/2020/Danske Bank/none/2020-12-31 - Danske Bank - 2020-12-31 Danske Bank 103 \x8frsoppgave - bank,innboks,konto-f-db-brukskonto,konto-f-db-lån-boligkreditt,konto-f-db-lån-fastrente,konto-f-db-sparekonto,konto-k-db-365direkte,konto-k-db-brukskonto,konto-k-db-mastercard,konto-k-db-sparekonto,konto-v-db-brukskonto,kristian,skatt,therese,vilde - none.pdf'
Traceback (most recent call last):
  File "/usr/src/paperless/src/documents/consumer.py", line 379, in try_consume_file
    self._write(document.storage_type, self.path, document.source_path)
  File "/usr/src/paperless/src/documents/consumer.py", line 505, in _write
    with open(target, "wb") as write_file:
OSError: [Errno 36] File name too long: '/usr/src/paperless/media/documents/originals/2020/Danske Bank/none/2020-12-31 - Danske Bank - 2020-12-31 Danske Bank 103 \x8frsoppgave - bank,innboks,konto-f-db-brukskonto,konto-f-db-lån-boligkreditt,konto-f-db-lån-fastrente,konto-f-db-sparekonto,konto-k-db-365direkte,konto-k-db-brukskonto,konto-k-db-mastercard,konto-k-db-sparekonto,konto-v-db-brukskonto,kristian,skatt,therese,vilde - none.pdf'
13:19:49 [Q] INFO Process-1:30 stopped doing work
13:19:49 [Q] ERROR Failed [2020-12-31 Danske Bank 103 rsoppgave.pdf] - 2020-12-31 Danske Bank 103 rsoppgave.pdf: The following error occurred while consuming 2020-12-31 Danske Bank 103 rsoppgave.pdf: [Errno 36] File name too long: '/usr/src/paperless/media/documents/originals/2020/Danske Bank/none/2020-12-31 - Danske Bank - 2020-12-31 Danske Bank 103 \x8frsoppgave - bank,innboks,konto-f-db-brukskonto,konto-f-db-lån-boligkreditt,konto-f-db-lån-fastrente,konto-f-db-sparekonto,konto-k-db-365direkte,konto-k-db-brukskonto,konto-k-db-mastercard,konto-k-db-sparekonto,konto-v-db-brukskonto,kristian,skatt,therese,vilde - none.pdf' : Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/asgiref/sync.py", line 280, in main_wrap
    raise exc_info[1]
  File "/usr/src/paperless/src/documents/consumer.py", line 379, in try_consume_file
    self._write(document.storage_type, self.path, document.source_path)
  File "/usr/src/paperless/src/documents/consumer.py", line 505, in _write
    with open(target, "wb") as write_file:
OSError: [Errno 36] File name too long: '/usr/src/paperless/media/documents/originals/2020/Danske Bank/none/2020-12-31 - Danske Bank - 2020-12-31 Danske Bank 103 \x8frsoppgave - bank,innboks,konto-f-db-brukskonto,konto-f-db-lån-boligkreditt,konto-f-db-lån-fastrente,konto-f-db-sparekonto,konto-k-db-365direkte,konto-k-db-brukskonto,konto-k-db-mastercard,konto-k-db-sparekonto,konto-v-db-brukskonto,kristian,skatt,therese,vilde - none.pdf'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
  File "/usr/src/paperless/src/src/django-q/django_q/cluster.py", line 454, in worker
    res = f(*task["args"], **task["kwargs"])
  File "/usr/src/paperless/src/documents/tasks.py", line 154, in consume_file
    document = Consumer().try_consume_file(
  File "/usr/src/paperless/src/documents/consumer.py", line 423, in try_consume_file
    self._fail(
  File "/usr/src/paperless/src/documents/consumer.py", line 90, in _fail
    raise ConsumerError(f"{self.filename}: {log_message or message}") from exception
documents.consumer.ConsumerError: 2020-12-31 Danske Bank 103 rsoppgave.pdf: The following error occurred while consuming 2020-12-31 Danske Bank 103 rsoppgave.pdf: [Errno 36] File name too long: '/usr/src/paperless/media/documents/originals/2020/Danske Bank/none/2020-12-31 - Danske Bank - 2020-12-31 Danske Bank 103 \x8frsoppgave - bank,innboks,konto-f-db-brukskonto,konto-f-db-lån-boligkreditt,konto-f-db-lån-fastrente,konto-f-db-sparekonto,konto-k-db-365direkte,konto-k-db-brukskonto,konto-k-db-mastercard,konto-k-db-sparekonto,konto-v-db-brukskonto,kristian,skatt,therese,vilde - none.pdf'
```


### Browser logs

_No response_

### Paperless-ngx version

1.9.2

### Host OS

linux x86_64 Debian GNU/Linux 11 (bullseye)

### Installation method

Docker - official image

### Browser

Chrome

### Configuration changes

_No response_

### Other

_No response_


Discussion:

Resolving patch:
diff --git a/src/documents/signals/handlers.py b/src/documents/signals/handlers.py
--- a/src/documents/signals/handlers.py
+++ b/src/documents/signals/handlers.py
@@ -447,7 +447,8 @@ def update_filename_and_move_files(sender, instance, **kwargs):
                 archive_filename=instance.archive_filename,
             )
 
-        except (OSError, DatabaseError, CannotMoveFilesException):
+        except (OSError, DatabaseError, CannotMoveFilesException) as e:
+            logger.warn(f"Exception during file handling: {e}")
             # This happens when either:
             #  - moving the files failed due to file system errors
             #  - saving to the database failed due to database errors
@@ -456,9 +457,11 @@ def update_filename_and_move_files(sender, instance, **kwargs):
             # Try to move files to their original location.
             try:
                 if move_original and os.path.isfile(instance.source_path):
+                    logger.info("Restoring previous original path")
                     os.rename(instance.source_path, old_source_path)
 
                 if move_archive and os.path.isfile(instance.archive_path):
+                    logger.info("Restoring previous archive path")
                     os.rename(instance.archive_path, old_archive_path)
 
             except Exception:
@@ -468,7 +471,7 @@ def update_filename_and_move_files(sender, instance, **kwargs):
                 #  issue that's going to get caught by the santiy checker.
                 #  All files remain in place and will never be overwritten,
                 #  so this is not the end of the world.
-                # B: if moving the orignal file failed, nothing has changed
+                # B: if moving the original file failed, nothing has changed
                 #  anyway.
                 pass
 


Test patch: