Repository: paperless-ngx/paperless-ngx

Issue:
[BUG] document_thumbnails tool moves default thumbnail
### Description

I'm running the `document_thumbnails` from the command line. Some of my PDF's are password protected, so the thumbnail generation fails. When the thumbnail cannot be generated, it [falls back](https://github.com/paperless-ngx/paperless-ngx/blob/dev/src/documents/parsers.py#L205) to the returning the [default thumbnail](https://github.com/paperless-ngx/paperless-ngx/blob/dev/src/documents/parsers.py#L171) located at `src/documents/resources/document.png`. Unfortunately the `document_thumbnails` always [moves the thumbnail](https://github.com/paperless-ngx/paperless-ngx/blob/dev/src/documents/management/commands/document_thumbnails.py#L30), assuming that it was a generated one. Now `src/documents/resources/document.png` is gone, moved to the user's thumbnail path.

### Steps to reproduce

1. Create two encrypted pdfs
2. Consume them in paperless
3. Execute the `document_thumbnails` command

On the 2nd PDF it should fail.

### Webserver logs

```bash
As this field is mandatory, I'll put the logs I got when executing `document_thumbnails` here. These are not webserver logs!


During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.9/multiprocessing/pool.py", line 125, in worker
    result = (True, func(*args, **kwds))
  File "/usr/src/paperless/src/documents/management/commands/document_thumbnails.py", line 30, in _process_document
    shutil.move(thumb, document.thumbnail_path)
  File "/usr/local/lib/python3.9/shutil.py", line 845, in move
    copy_function(src, real_dst)
  File "/usr/local/lib/python3.9/shutil.py", line 444, in copy2
    copyfile(src, dst, follow_symlinks=follow_symlinks)
  File "/usr/local/lib/python3.9/shutil.py", line 264, in copyfile
    with open(src, 'rb') as fsrc:
FileNotFoundError: [Errno 2] No such file or directory: '/usr/src/paperless/src/documents/resources/document.png'
"""

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/src/paperless/src/manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python3.9/site-packages/django/core/management/__init__.py", line 446, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python3.9/site-packages/django/core/management/__init__.py", line 440, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python3.9/site-packages/django/core/management/base.py", line 402, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/local/lib/python3.9/site-packages/django/core/management/base.py", line 448, in execute
    output = self.handle(*args, **options)
  File "/usr/src/paperless/src/documents/management/commands/document_thumbnails.py", line 76, in handle
    list(
  File "/usr/local/lib/python3.9/site-packages/tqdm/std.py", line 1178, in __iter__
    for obj in iterable:
  File "/usr/local/lib/python3.9/multiprocessing/pool.py", line 870, in next
    raise value
FileNotFoundError: [Errno 2] No such file or directory: '/usr/src/paperless/src/documents/resources/document.png'
```
```


### Browser logs

_No response_

### Paperless-ngx version

1.16.1

### Host OS

Debian GNU/Linux 12

### Installation method

Docker - official image

### Browser

Safari Version 16.3 (18614.4.6.1.6)

### Configuration changes

_No response_

### Other

_No response_
[BUG] document_thumbnails tool moves default thumbnail
### Description

I'm running the `document_thumbnails` from the command line. Some of my PDF's are password protected, so the thumbnail generation fails. When the thumbnail cannot be generated, it [falls back](https://github.com/paperless-ngx/paperless-ngx/blob/dev/src/documents/parsers.py#L205) to the returning the [default thumbnail](https://github.com/paperless-ngx/paperless-ngx/blob/dev/src/documents/parsers.py#L171) located at `src/documents/resources/document.png`. Unfortunately the `document_thumbnails` always [moves the thumbnail](https://github.com/paperless-ngx/paperless-ngx/blob/dev/src/documents/management/commands/document_thumbnails.py#L30), assuming that it was a generated one. Now `src/documents/resources/document.png` is gone, moved to the user's thumbnail path.

### Steps to reproduce

1. Create two encrypted pdfs
2. Consume them in paperless
3. Execute the `document_thumbnails` command

On the 2nd PDF it should fail.

### Webserver logs

```bash
As this field is mandatory, I'll put the logs I got when executing `document_thumbnails` here. These are not webserver logs!


During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.9/multiprocessing/pool.py", line 125, in worker
    result = (True, func(*args, **kwds))
  File "/usr/src/paperless/src/documents/management/commands/document_thumbnails.py", line 30, in _process_document
    shutil.move(thumb, document.thumbnail_path)
  File "/usr/local/lib/python3.9/shutil.py", line 845, in move
    copy_function(src, real_dst)
  File "/usr/local/lib/python3.9/shutil.py", line 444, in copy2
    copyfile(src, dst, follow_symlinks=follow_symlinks)
  File "/usr/local/lib/python3.9/shutil.py", line 264, in copyfile
    with open(src, 'rb') as fsrc:
FileNotFoundError: [Errno 2] No such file or directory: '/usr/src/paperless/src/documents/resources/document.png'
"""

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/src/paperless/src/manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python3.9/site-packages/django/core/management/__init__.py", line 446, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python3.9/site-packages/django/core/management/__init__.py", line 440, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python3.9/site-packages/django/core/management/base.py", line 402, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/local/lib/python3.9/site-packages/django/core/management/base.py", line 448, in execute
    output = self.handle(*args, **options)
  File "/usr/src/paperless/src/documents/management/commands/document_thumbnails.py", line 76, in handle
    list(
  File "/usr/local/lib/python3.9/site-packages/tqdm/std.py", line 1178, in __iter__
    for obj in iterable:
  File "/usr/local/lib/python3.9/multiprocessing/pool.py", line 870, in next
    raise value
FileNotFoundError: [Errno 2] No such file or directory: '/usr/src/paperless/src/documents/resources/document.png'
```
```


### Browser logs

_No response_

### Paperless-ngx version

1.16.1

### Host OS

Debian GNU/Linux 12

### Installation method

Docker - official image

### Browser

Safari Version 16.3 (18614.4.6.1.6)

### Configuration changes

_No response_

### Other

_No response_


Discussion:

Resolving patch:
diff --git a/src/documents/parsers.py b/src/documents/parsers.py
--- a/src/documents/parsers.py
+++ b/src/documents/parsers.py
@@ -202,7 +202,12 @@ def make_thumbnail_from_pdf_gs_fallback(in_path, temp_dir, logging_group=None) -
         return out_path
 
     except ParseError:
-        return get_default_thumbnail()
+        # The caller might expect a generated thumbnail that can be moved,
+        # so we need to copy it before it gets moved.
+        # https://github.com/paperless-ngx/paperless-ngx/issues/3631
+        default_thumbnail_path = os.path.join(temp_dir, "document.png")
+        shutil.copy2(get_default_thumbnail(), default_thumbnail_path)
+        return default_thumbnail_path
 
 
 def make_thumbnail_from_pdf(in_path, temp_dir, logging_group=None) -> str:


Test patch: