mirror of
				https://github.com/paperless-ngx/paperless-ngx.git
				synced 2025-10-30 03:56:23 -05:00 
			
		
		
		
	Refactor after feedback:
- rename PAPERLESS_TIKA to PAPERLESS_TIKA_ENABLED - all other env params now start with PAPERLESS_TIKA - convert_to_pdf as class instance method - smaller details Signed-off-by: Jo Vandeginste <Jo.Vandeginste@kuleuven.be>
This commit is contained in:
		| @@ -24,9 +24,9 @@ services: | ||||
|     env_file: docker-compose.env | ||||
|     environment: | ||||
|       PAPERLESS_REDIS: redis://broker:6379 | ||||
|       PAPERLESS_TIKA: 1 | ||||
|       GOTENBERG_SERVER_ENDPOINT: http://gotenberg:3000 | ||||
|       TIKA_SERVER_ENDPOINT: http://tika:9998 | ||||
|       PAPERLESS_TIKA_ENABLED: 1 | ||||
|       PAPERLESS_TIKA_GOTENBERG_ENDPOINT: http://gotenberg:3000 | ||||
|       PAPERLESS_TIKA_ENDPOINT: http://tika:9998 | ||||
|  | ||||
|   gotenberg: | ||||
|     image: thecodingmachine/gotenberg | ||||
|   | ||||
| @@ -24,9 +24,9 @@ services: | ||||
|     env_file: docker-compose.env | ||||
|     environment: | ||||
|       PAPERLESS_REDIS: redis://broker:6379 | ||||
|       PAPERLESS_TIKA: 1 | ||||
|       GOTENBERG_SERVER_ENDPOINT: http://gotenberg:3000 | ||||
|       TIKA_SERVER_ENDPOINT: http://tika:9998 | ||||
|       PAPERLESS_TIKA_ENABLED: 1 | ||||
|       PAPERLESS_TIKA_GOTENBERG_ENDPOINT: http://gotenberg:3000 | ||||
|       PAPERLESS_TIKA_ENDPOINT: http://tika:9998 | ||||
|  | ||||
|   gotenberg: | ||||
|     image: thecodingmachine/gotenberg | ||||
|   | ||||
| @@ -291,17 +291,17 @@ configure their endpoints, and enable the feature. | ||||
| If you run paperless on docker, you can add those services to the docker-compose | ||||
| file (see the examples provided). | ||||
|  | ||||
| PAPERLESS_TIKA=<bool> | ||||
| PAPERLESS_TIKA_ENABLED=<bool> | ||||
|     Enable (or disable) the Tika parser. | ||||
|  | ||||
|     Defaults to false. | ||||
|  | ||||
| TIKA_SERVER_ENDPOINT=<url> | ||||
| PAPERLESS_TIKA_ENDPOINT=<url> | ||||
|     Set the endpoint URL were Paperless can reach your Tika server. | ||||
|  | ||||
|     Defaults to "http://localhost:9998". | ||||
|  | ||||
| GOTENBERG_SERVER_ENDPOINT=<url> | ||||
| PAPERLESS_TIKA_GOTENBERG_ENDPOINT=<url> | ||||
|     Set the endpoint URL were Paperless can reach your Gotenberg server. | ||||
|  | ||||
|     Defaults to "http://localhost:3000". | ||||
|   | ||||
| @@ -427,5 +427,8 @@ PAPERLESS_FILENAME_FORMAT = os.getenv("PAPERLESS_FILENAME_FORMAT") | ||||
| THUMBNAIL_FONT_NAME = os.getenv("PAPERLESS_THUMBNAIL_FONT_NAME", "/usr/share/fonts/liberation/LiberationSerif-Regular.ttf") | ||||
|  | ||||
| # Tika settings | ||||
| PAPERLESS_TIKA = __get_boolean("PAPERLESS_TIKA", "NO") | ||||
| GOTENBERG_SERVER_ENDPOINT = os.getenv("GOTENBERG_SERVER_ENDPOINT", "http://localhost:3000") | ||||
| PAPERLESS_TIKA_ENABLED = __get_boolean("PAPERLESS_TIKA_ENABLED", "NO") | ||||
| PAPERLESS_TIKA_ENDPOINT = os.getenv("PAPERLESS_TIKA_ENDPOINT", "http://localhost:9998") | ||||
| PAPERLESS_TIKA_GOTENBERG_ENDPOINT = os.getenv( | ||||
|     "PAPERLESS_TIKA_GOTENBERG_ENDPOINT", "http://localhost:3000" | ||||
| ) | ||||
|   | ||||
| @@ -9,6 +9,6 @@ class PaperlessTikaConfig(AppConfig): | ||||
|     def ready(self): | ||||
|         from documents.signals import document_consumer_declaration | ||||
|  | ||||
|         if settings.PAPERLESS_TIKA: | ||||
|         if settings.PAPERLESS_TIKA_ENABLED: | ||||
|             document_consumer_declaration.connect(tika_consumer_declaration) | ||||
|         AppConfig.ready(self) | ||||
|   | ||||
| @@ -70,49 +70,46 @@ class TikaDocumentParser(DocumentParser): | ||||
|  | ||||
|     def parse(self, document_path, mime_type): | ||||
|         self.log("info", f"[TIKA_PARSE] Sending {document_path} to Tika server") | ||||
|         tika_server = settings.PAPERLESS_TIKA_ENDPOINT | ||||
|  | ||||
|         try: | ||||
|             parsed = parser.from_file(document_path) | ||||
|             parsed = parser.from_file(document_path, tika_server) | ||||
|         except requests.exceptions.HTTPError as err: | ||||
|             raise ParseError(f"Could not parse {document_path} with tika server: {err}") | ||||
|  | ||||
|         try: | ||||
|             content = parsed["content"].strip() | ||||
|         except: | ||||
|             content = "" | ||||
|  | ||||
|         try: | ||||
|             creation_date = dateutil.parser.isoparse( | ||||
|                 parsed["metadata"]["Creation-Date"] | ||||
|             raise ParseError( | ||||
|                 f"Could not parse {document_path} with tika server at {tika_server}: {err}" | ||||
|             ) | ||||
|  | ||||
|         try: | ||||
|             self.text = parsed["content"].strip() | ||||
|         except: | ||||
|             creation_date = None | ||||
|             pass | ||||
|  | ||||
|         try: | ||||
|             self.date = dateutil.parser.isoparse(parsed["metadata"]["Creation-Date"]) | ||||
|         except: | ||||
|             pass | ||||
|  | ||||
|         archive_path = os.path.join(self.tempdir, "convert.pdf") | ||||
|         convert_to_pdf(self, document_path, archive_path) | ||||
|  | ||||
|         convert_to_pdf(document_path, archive_path) | ||||
|         self.archive_path = archive_path | ||||
|         self.date = creation_date | ||||
|         self.text = content | ||||
|  | ||||
|     def convert_to_pdf(document_path, pdf_path): | ||||
|         pdf_path = os.path.join(self.tempdir, "convert.pdf") | ||||
|         gotenberg_server = settings.PAPERLESS_TIKA_GOTENBERG_ENDPOINT | ||||
|         url = gotenberg_server + "/convert/office" | ||||
|  | ||||
| def convert_to_pdf(self, document_path, pdf_path): | ||||
|     pdf_path = os.path.join(self.tempdir, "convert.pdf") | ||||
|     gotenberg_server = settings.GOTENBERG_SERVER_ENDPOINT | ||||
|     url = gotenberg_server + "/convert/office" | ||||
|         self.log("info", f"[TIKA] Converting {document_path} to PDF as {pdf_path}") | ||||
|         files = {"files": open(document_path, "rb")} | ||||
|         headers = {} | ||||
|  | ||||
|     self.log("info", f"[TIKA] Converting {document_path} to PDF as {pdf_path}") | ||||
|     files = {"files": open(document_path, "rb")} | ||||
|     headers = {} | ||||
|         try: | ||||
|             response = requests.post(url, files=files, headers=headers) | ||||
|             response.raise_for_status()  # ensure we notice bad responses | ||||
|         except requests.exceptions.HTTPError as err: | ||||
|             raise ParseError( | ||||
|                 f"Could not contact gotenberg server at {gotenberg_server}: {err}" | ||||
|             ) | ||||
|  | ||||
|     try: | ||||
|         response = requests.post(url, files=files, headers=headers) | ||||
|         response.raise_for_status()  # ensure we notice bad responses | ||||
|     except requests.exceptions.HTTPError as err: | ||||
|         raise ParseError( | ||||
|             f"Could not contact gotenberg server at {gotenberg_server}: {err}" | ||||
|         ) | ||||
|  | ||||
|     file = open(pdf_path, "wb") | ||||
|     file.write(response.content) | ||||
|     file.close() | ||||
|         file = open(pdf_path, "wb") | ||||
|         file.write(response.content) | ||||
|         file.close() | ||||
|   | ||||
| @@ -1,3 +0,0 @@ | ||||
| import magic | ||||
| m = magic.from_file("/nfsstorage/jo/syncthing/Documenten/20R-309.153.052.pdf") | ||||
| print(m) | ||||
		Reference in New Issue
	
	Block a user
	 Jo Vandeginste
					Jo Vandeginste