Repository: paperless-ngx/paperless-ngx

Issue:
[BUG] Could not parse docx document
### Description

I can't upload a docx document after updating to the latest release candidate.

### Steps to reproduce

1. Update paperless-ngx to 1.9.0-rc1 (ghcr.io/paperless-ngx/paperless-ngx:beta)
2. Upload a docx-document and an error occurs "_`Error while consuming document khms-immoconsult_20170924_2.docx: Could not parse /tmp/paperless/paperless-upload-iwgwe9h0 with tika server at http://tika:9998: 'PosixPath' object has no attribute 'decode'`_"

### Webserver logs

```bash
2022-09-14 05:45:14,789] [INFO] [paperless.consumer] Consuming khms-immoconsult_20170924_2.docx

[2022-09-14 05:45:14,791] [DEBUG] [paperless.consumer] Detected mime type: application/vnd.openxmlformats-officedocument.wordprocessingml.document

[2022-09-14 05:45:14,795] [DEBUG] [paperless.consumer] Parser: TikaDocumentParser

[2022-09-14 05:45:14,800] [DEBUG] [paperless.consumer] Parsing khms-immoconsult_20170924_2.docx...

[2022-09-14 05:45:14,801] [INFO] [paperless.parsing.tika] Sending /tmp/paperless/paperless-upload-iwgwe9h0 to Tika server

[2022-09-14 05:45:14,802] [DEBUG] [paperless.parsing.tika] Deleting directory /tmp/paperless/paperless-86t5km9q

[2022-09-14 05:45:14,806] [ERROR] [paperless.consumer] Error while consuming document khms-immoconsult_20170924_2.docx: Could not parse /tmp/paperless/paperless-upload-iwgwe9h0 with tika server at http://tika:9998: 'PosixPath' object has no attribute 'decode'

Traceback (most recent call last):

  File "/usr/src/paperless/src/paperless_tika/parsers.py", line 55, in parse

    parsed = parser.from_file(document_path, tika_server)

  File "/usr/local/lib/python3.9/site-packages/tika/parser.py", line 40, in from_file

    output = parse1(service, filename, serverEndpoint, headers=headers, config_path=config_path, requestOptions=requestOptions)

  File "/usr/local/lib/python3.9/site-packages/tika/tika.py", line 327, in parse1

    path, file_type = getRemoteFile(urlOrPath, TikaFilesPath)

  File "/usr/local/lib/python3.9/site-packages/tika/tika.py", line 762, in getRemoteFile

    urlp = urlparse(urlOrPath)

  File "/usr/local/lib/python3.9/urllib/parse.py", line 392, in urlparse

    url, scheme, _coerce_result = _coerce_args(url, scheme)

  File "/usr/local/lib/python3.9/urllib/parse.py", line 128, in _coerce_args

    return _decode_args(args) + (_encode_result,)

  File "/usr/local/lib/python3.9/urllib/parse.py", line 112, in _decode_args

    return tuple(x.decode(encoding, errors) if x else '' for x in args)

  File "/usr/local/lib/python3.9/urllib/parse.py", line 112, in <genexpr>

    return tuple(x.decode(encoding, errors) if x else '' for x in args)

AttributeError: 'PosixPath' object has no attribute 'decode'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):

  File "/usr/src/paperless/src/documents/consumer.py", line 320, in try_consume_file

    document_parser.parse(self.path, mime_type, self.filename)

  File "/usr/src/paperless/src/paperless_tika/parsers.py", line 57, in parse

    raise ParseError(

documents.parsers.ParseError: Could not parse /tmp/paperless/paperless-upload-iwgwe9h0 with tika server at http://tika:9998: 'PosixPath' object has no attribute 'decode'
```


### Paperless-ngx version

1.9.0-rc1

### Host OS

Ubuntu 22.04 LTS

### Installation method

Docker - official image

### Browser

Google Chrome

### Configuration changes

_No response_

### Other

_No response_


Discussion:
Sorry for what may be an obvious question, but you’re able to consume this same doc on your v1.8.0 install?
> Sorry for what may be an obvious question, but you’re able to consume this same doc on your v1.8.0 install?

This is a legitimate question :-)

Just tested it and with 1.8.0 the upload is possible.

``` [2022-09-14 10:07:18,753] [INFO] [paperless.management.consumer] Using inotify to watch directory for changes: /usr/src/paperless/consume
[2022-09-14 10:10:25,701] [INFO] [paperless.consumer] Consuming khms-immoconsult_20170924_2.docx
[2022-09-14 10:10:25,702] [DEBUG] [paperless.consumer] Detected mime type: application/vnd.openxmlformats-officedocument.wordprocessingml.document
[2022-09-14 10:10:25,706] [DEBUG] [paperless.consumer] Parser: TikaDocumentParser
[2022-09-14 10:10:25,711] [DEBUG] [paperless.consumer] Parsing khms-immoconsult_20170924_2.docx...
[2022-09-14 10:10:25,712] [INFO] [paperless.parsing.tika] Sending /tmp/paperless/paperless-upload-uvjey215 to Tika server
[2022-09-14 10:10:26,360] [WARNING] [paperless.parsing.tika] Unable to extract date for document /tmp/paperless/paperless-upload-uvjey215: 'Creation-Date'
[2022-09-14 10:10:26,360] [INFO] [paperless.parsing.tika] Converting /tmp/paperless/paperless-upload-uvjey215 to PDF as /tmp/paperless/paperless-8yo3x_a9/convert.pdf
[2022-09-14 10:10:27,584] [DEBUG] [paperless.consumer] Generating thumbnail for khms-immoconsult_20170924_2.docx...
[2022-09-14 10:10:27,589] [DEBUG] [paperless.parsing] Execute: convert -density 300 -scale 500x5000> -alpha remove -strip -auto-orient /tmp/paperless/paperless-8yo3x_a9/convert.pdf[0] /tmp/paperless/paperless-8yo3x_a9/convert.webp
[2022-09-14 10:10:28,793] [DEBUG] [paperless.classifier] Document classification model does not exist (yet), not performing automatic matching.
[2022-09-14 10:10:28,798] [DEBUG] [paperless.consumer] Saving record to database
[2022-09-14 10:10:28,798] [DEBUG] [paperless.consumer] Creation date from parse_date: 2017-09-25 00:00:00+02:00
[2022-09-14 10:10:28,841] [DEBUG] [paperless.matching] DocumentType Rechnung matched on document 2017-09-25 khms-immoconsult_20170924_2 because it contains this word: rechnung
[2022-09-14 10:10:28,842] [INFO] [paperless.handlers] Assigning document type Rechnung to 2017-09-25 khms-immoconsult_20170924_2
[2022-09-14 10:10:28,930] [DEBUG] [paperless.consumer] Deleting file /tmp/paperless/paperless-upload-uvjey215
[2022-09-14 10:10:28,981] [DEBUG] [paperless.parsing.tika] Deleting directory /tmp/paperless/paperless-8yo3x_a9
[2022-09-14 10:10:28,982] [INFO] [paperless.consumer] Executing post-consume script /usr/src/paperless/data/post_consume.sh
[2022-09-14 10:10:29,094] [INFO] [paperless.consumer] Document 2017-09-25 khms-immoconsult_20170924_2 consumption finished```
Well, something is expecting a string, but getting a `PathLike`.  Should be easy to fix once I find out where it's coming from.

Resolving patch:
diff --git a/src/paperless_tika/parsers.py b/src/paperless_tika/parsers.py
--- a/src/paperless_tika/parsers.py
+++ b/src/paperless_tika/parsers.py
@@ -1,4 +1,5 @@
 import os
+from pathlib import Path
 
 import dateutil.parser
 import requests
@@ -28,6 +29,11 @@ def get_thumbnail(self, document_path, mime_type, file_name=None):
 
     def extract_metadata(self, document_path, mime_type):
         tika_server = settings.TIKA_ENDPOINT
+
+        # tika does not support a PathLike, only strings
+        # ensure this is a string
+        document_path = str(document_path)
+
         try:
             parsed = parser.from_file(document_path, tika_server)
         except Exception as e:
@@ -47,10 +53,14 @@ def extract_metadata(self, document_path, mime_type):
             for key in parsed["metadata"]
         ]
 
-    def parse(self, document_path, mime_type, file_name=None):
+    def parse(self, document_path: Path, mime_type, file_name=None):
         self.log("info", f"Sending {document_path} to Tika server")
         tika_server = settings.TIKA_ENDPOINT
 
+        # tika does not support a PathLike, only strings
+        # ensure this is a string
+        document_path = str(document_path)
+
         try:
             parsed = parser.from_file(document_path, tika_server)
         except Exception as err:


Test patch: