A community-supported supercharged document management system: scan, index and archive all your documents
Python aiangulararchivingdjangodmsdocument-managementdocument-management-systemllmmachine-learningocroptical-character-recognitionpdf
use env variables in pre-|post-consume scripts
ziprandom08ae3f8771a6
diff --git a/docs/advanced_usage.rst b/docs/advanced_usage.rstindex 6449c478b..bc194ce5a 100644--- a/docs/advanced_usage.rst+++ b/docs/advanced_usage.rst@@ -121,10 +121,10 @@ Pre-consumption script======================Executed after the consumer sees a new document in the consumption folder, but-before any processing of the document is performed. This script receives exactly-one argument:+before any processing of the document is performed. This script can access the+following relevant environment variables set:-* Document file name+* ``DOCUMENT_SOURCE_PATH``A simple but common example for this would be creating a simple script likethis:@@ -134,7 +134,7 @@ this:.. code:: bash#!/usr/bin/env bash- pdf2pdfocr.py -i ${1}+ pdf2pdfocr.py -i ${DOCUMENT_SOURCE_PATH}``/etc/paperless.conf``@@ -157,16 +157,16 @@ Post-consumption script=======================Executed after the consumer has successfully processed a document and has moved it-into paperless. It receives the following arguments:--* Document id-* Generated file name-* Source path-* Thumbnail path-* Download URL-* Thumbnail URL-* Correspondent-* Tags+into paperless. It receives the following environment variables:++* ``DOCUMENT_ID``+* ``DOCUMENT_FILE_NAME``+* ``DOCUMENT_SOURCE_PATH``+* ``DOCUMENT_THUMBNAIL_PATH``+* ``DOCUMENT_DOWNLOAD_URL``+* ``DOCUMENT_THUMBNAIL_URL``+* ``DOCUMENT_CORRESPONDENT``+* ``DOCUMENT_TAGS``The script can be in any language, but for a simple shell scriptexample, you can take a look at `post-consumption-example.sh`_ in this project.diff --git a/scripts/post-consumption-example.sh b/scripts/post-consumption-example.shindex 6edd3e158..b4c40090c 100755--- a/scripts/post-consumption-example.sh+++ b/scripts/post-consumption-example.sh@@ -1,14 +1,5 @@#!/usr/bin/env bash-DOCUMENT_ID=${1}-DOCUMENT_FILE_NAME=${2}-DOCUMENT_SOURCE_PATH=${3}-DOCUMENT_THUMBNAIL_PATH=${4}-DOCUMENT_DOWNLOAD_URL=${5}-DOCUMENT_THUMBNAIL_URL=${6}-DOCUMENT_CORRESPONDENT=${7}-DOCUMENT_TAGS=${8}-echo "A document with an id of ${DOCUMENT_ID} was just consumed. I know thediff --git a/src/documents/consumer.py b/src/documents/consumer.pyindex e5794ce4f..42666e107 100644--- a/src/documents/consumer.py+++ b/src/documents/consumer.py@@ -134,8 +134,11 @@ class Consumer(LoggingMixin):self.log("info", f"Executing pre-consume script {settings.PRE_CONSUME_SCRIPT}")+ script_env = os.environ.copy()+ script_env["DOCUMENT_SOURCE_PATH"] = os.path.normpath(self.path)+try:- Popen((settings.PRE_CONSUME_SCRIPT, self.path)).wait()+ Popen(settings.PRE_CONSUME_SCRIPT, env=script_env).wait()except Exception as e:self._fail(MESSAGE_PRE_CONSUME_SCRIPT_ERROR,@@ -159,19 +162,29 @@ class Consumer(LoggingMixin):f"Executing post-consume script {settings.POST_CONSUME_SCRIPT}",)+ script_env = os.environ.copy()++ script_env["DOCUMENT_ID"] = str(document.pk)+ script_env["DOCUMENT_FILE_NAME"] = document.get_public_filename()+ script_env["DOCUMENT_SOURCE_PATH"] = os.path.normpath(document.source_path)+ script_env["DOCUMENT_THUMBNAIL_PATH"] = os.path.normpath(+ document.thumbnail_path+ )+ script_env["DOCUMENT_DOWNLOAD_URL"] = reverse(+ "document-download", kwargs={"pk": document.pk}+ )+ script_env["DOCUMENT_THUMBNAIL_URL"] = reverse(+ "document-thumb", kwargs={"pk": document.pk}+ )+ script_env["DOCUMENT_CORRESPONDENT"] = str(document.correspondent)+ script_env["DOCUMENT_TAGS"] = str(+ ",".join(document.tags.all().values_list("name", flat=True))+ )+try:Popen(- (- settings.POST_CONSUME_SCRIPT,- str(document.pk),- document.get_public_filename(),- os.path.normpath(document.source_path),- os.path.normpath(document.thumbnail_path),- reverse("document-download", kwargs={"pk": document.pk}),- reverse("document-thumb", kwargs={"pk": document.pk}),- str(document.correspondent),- str(",".join(document.tags.all().values_list("name", flat=True))),- ),+ settings.POST_CONSUME_SCRIPT,+ env=script_env,).wait()except Exception as e:self._fail(