Repository: paperless-ngx/paperless-ngx

Issue:
[BUG] Failure to move items to trash -- [Errno 18] Invalid cross-device link
<!---
=> Before opening an issue, please check the documentation and see if it helps you resolve your issue: https://paperless-ngx.readthedocs.io/en/latest/troubleshooting.html
=> Please also make sure that you followed the installation instructions.
=> Please search the issues and look for similar issues before opening a bug report.

=> If you would like to submit a feature request please submit one under https://github.com/paperless-ngx/paperless-ngx/discussions/categories/feature-requests

=> If you encounter issues while installing of configuring Paperless-ngx, please post that in the "Support" section of the discussions. Remember that Paperless successfully runs on a variety of different systems. If paperless does not start, it's probably an issue with your system, and not an issue of paperless.

=> Don't remove the [BUG] prefix from the title.
-->

**Describe the bug**
While running within docker via the `linuxserver/paperless-ngx` image on a Synology NAS host system, `PAPERLESS_TRASH_DIR` and therefore the new "move to trash" feature that was introduced does not work and the files are still permanently deleted instead of moving to a specified trash directory.

**To Reproduce**
Steps to reproduce the behavior:
1. Within the settings for the `linuxserver/paperless-ngx` image, mount a volume from a Synology recycle folder to a folder called /recycle within the container. In my case I'm mounting the host folder `/Network Scans/#recycle` to the container folder `/recycle`.
2. Set the `PAPERLESS_TRASH_DIR:` environmental value to the recycle folder name that you specified in the previous step, in this case, `/recycle`.
3. Start the container
4. Attempt to delete a file within the Paperless interface. It deletes the file completely instead of moving the file to the specified trash directory and spits out an error in the logs. See error below:

**Expected behavior**
Paperless moves the file to a predetermined "recycle" folder as intended and does not spit an error within the log file.

**Webserver logs**
```
[ERROR] [paperless.handlers] Failed to move /media/documents/originals/0000726.pdf to trash at /recycle/0000726.pdf: [Errno 18] Invalid cross-device link: '/media/documents/originals/0000726.pdf' -> '/recycle/0000726.pdf'. Skipping cleanup!
```

**Relevant information**
 - Host OS: `DSM 7.0.1-42218 Update 3`
 - Docker Image: `linuxserver/paperless-ngx`
 - Browser: `Brave v1.37.109 - Chromium: 100.0.4896.60 (Official Build) (64-bit)`
 - Paperless-ngx verison: `1.6.0`
 - Installation method: Docker

**Note:** From the brief Google searching I did, [this stackoverflow page](https://stackoverflow.com/questions/42392600/oserror-errno-18-invalid-cross-device-link) references the error and talks about using the call `shutil.move` to move files instead of `os.rename`, as `os.rename` apparently only works when moving files within the same file system. 

I'm assuming maybe this is happening because the file system within the docker image and the native file system from the host (DSM 7 in this case) are different. I'm not sure if that applies to how paperless-ngx operates, but I thought I'd include it. Also I'm not sure if this is related to the docker image itself, or paperless-ngx. 


Discussion:
Related to #24 
@tribut interested in looking at this?
Quick search suggests using `shutil.move` instead of a rename to support cross file system

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
@@ -1,5 +1,6 @@
 import logging
 import os
+import shutil
 
 from django.conf import settings
 from django.contrib.admin.models import ADDITION
@@ -252,7 +253,7 @@ def cleanup_document_deletion(sender, instance, using, **kwargs):
 
             logger.debug(f"Moving {instance.source_path} to trash at {new_file_path}")
             try:
-                os.rename(instance.source_path, new_file_path)
+                shutil.move(instance.source_path, new_file_path)
             except OSError as e:
                 logger.error(
                     f"Failed to move {instance.source_path} to trash at "


Test patch: