Repository: paperless-ngx/paperless-ngx

Issue:
[BUG] Debian 11 32bit: document_exporter fails when document creation date <=1901-12-13  or >= 2038-01-20
### Description

I invoked the document exporter with

     sudo -Hu paperless python3 manage.py document_exporter /target/directory --delete --use-filename-format --no-thumbnail --use-folder-prefix --split-manifest

I got the following error message: 
```

 27%|███████████████████████████████▉                                                                                       | 40/149 [00:02<00:07, 14.99it/s]
Traceback (most recent call last):
  File "/opt/paperless/paperless-ngx/src/manage.py", line 11, in <module>
    execute_from_command_line(sys.argv)
  File "/opt/paperless/.local/lib/python3.9/site-packages/django/core/management/__init__.py", line 446, in execute_from_command_line
    utility.execute()
  File "/opt/paperless/.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 "/opt/paperless/.local/lib/python3.9/site-packages/django/core/management/base.py", line 402, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/opt/paperless/.local/lib/python3.9/site-packages/django/core/management/base.py", line 448, in execute
    output = self.handle(*args, **options)
  File "/opt/paperless/paperless-ngx/src/documents/management/commands/document_exporter.py", line 181, in handle
    self.dump(options["no_progress_bar"])
  File "/opt/paperless/paperless-ngx/src/documents/management/commands/document_exporter.py", line 314, in dump
    t = int(time.mktime(document.created.timetuple()))
OverflowError: mktime argument out of range
```

After some research I found out that the reason is the following: I had some old historic documents without a creation date. I decided to set it to 01.01.1900 to mark them as just "old". Apparently this confused the script. When I set the date to 01.01.1970 and restarted the exporter, everything worked fine.

### Steps to reproduce

1. Set the creation date of a document to 01.01.1900
2. Invoke the document_exporter

### Webserver logs

```bash
none
```


### Browser logs

_No response_

### Paperless-ngx version

1.12.2

### Host OS

Debian 11

### Installation method

Bare metal

### Browser

_No response_

### Configuration changes

PAPERLESS_FILENAME_FORMAT={correspondent}/{created_year}{created_month}{created_day} {correspondent} {title} PAPERLESS_FILENAME_FORMAT_REMOVE_NONE=true

### Other

_No response_


Discussion:
It must be another date besides 1.1.1900.  I'm able to export that just fine, as well as call the same code from the command line.  I'm able to go to 1.1.1 without problems in the conversion, which is the lowest range supported by Python.
Strange, I just tested it again and was able to recreate the error with another document. Are there any logs I could provide? 

One idea, though: I installed it on an old thin client which is 32bit. Could this make a difference?

Edit: WIth trial and error I was able to narrow it down: 

Creation date 
14.12.1901 --> OK
13.12.1901 --> fails

Edit 2: I found this: [Python mktime overflow error](https://stackoverflow.com/questions/2518706/python-mktime-overflow-error)

Edit 3: And also this:[mktime in C Documentation]( https://bs2manuals.ts.fujitsu.com/psCRTEV101en/c-library-functions-for-posix-applications-reference-manual-20975-1/functions-and-variables-in-alphabetical-order-c-libfuncpos-4-0a50-125/m-c-libfuncpos-4-0a50-813/mktime-mktime64-convert-local-time-into-time-since-the-epoch-c-libfuncpos-4-0a50-445)

To confirm I also tested the upper limit, and indeed: 

Creation date
19.01.2038 --> OK
20.01.2038 --> fails

So its pretty sure that it's a variant of the Y2K38. 

I know that 32bit is kind of old now, but the hardware is still usable, so I would be glad if you could fix it somehow nevertheless.

Thanks! 
It also happens with Windows, not that we support Windows.  A weird little thing for sure.  I do think it can be worked around it in this case, as it is only applicate to encrypted files (which no one should be using anyway)

Resolving patch:
diff --git a/src/documents/management/commands/document_exporter.py b/src/documents/management/commands/document_exporter.py
--- a/src/documents/management/commands/document_exporter.py
+++ b/src/documents/management/commands/document_exporter.py
@@ -311,8 +311,8 @@ def dump(self, progress_bar_disable=False):
                 archive_target = None
 
             # 3.4. write files to target folder
-            t = int(time.mktime(document.created.timetuple()))
             if document.storage_type == Document.STORAGE_TYPE_GPG:
+                t = int(time.mktime(document.created.timetuple()))
 
                 original_target.parent.mkdir(parents=True, exist_ok=True)
                 with document.source_file as out_file:


Test patch: