mirror of
				https://github.com/paperless-ngx/paperless-ngx.git
				synced 2025-11-03 03:16:10 -06:00 
			
		
		
		
	Merge pull request #1154 from ziprandom/feature-use-env-vars-in-pre-post-scripts
Feature use env vars in pre post scripts
This commit is contained in:
		@@ -121,10 +121,10 @@ Pre-consumption script
 | 
				
			|||||||
======================
 | 
					======================
 | 
				
			||||||
 | 
					
 | 
				
			||||||
Executed after the consumer sees a new document in the consumption folder, but
 | 
					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
 | 
					before any processing of the document is performed. This script can access the
 | 
				
			||||||
one argument:
 | 
					following relevant environment variables set:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
* Document file name
 | 
					* ``DOCUMENT_SOURCE_PATH``
 | 
				
			||||||
 | 
					
 | 
				
			||||||
A simple but common example for this would be creating a simple script like
 | 
					A simple but common example for this would be creating a simple script like
 | 
				
			||||||
this:
 | 
					this:
 | 
				
			||||||
@@ -134,7 +134,7 @@ this:
 | 
				
			|||||||
.. code:: bash
 | 
					.. code:: bash
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    #!/usr/bin/env bash
 | 
					    #!/usr/bin/env bash
 | 
				
			||||||
    pdf2pdfocr.py -i ${1}
 | 
					    pdf2pdfocr.py -i ${DOCUMENT_SOURCE_PATH}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
``/etc/paperless.conf``
 | 
					``/etc/paperless.conf``
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -157,16 +157,20 @@ Post-consumption script
 | 
				
			|||||||
=======================
 | 
					=======================
 | 
				
			||||||
 | 
					
 | 
				
			||||||
Executed after the consumer has successfully processed a document and has moved it
 | 
					Executed after the consumer has successfully processed a document and has moved it
 | 
				
			||||||
into paperless. It receives the following arguments:
 | 
					into paperless. It receives the following environment variables:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
* Document id
 | 
					* ``DOCUMENT_ID``
 | 
				
			||||||
* Generated file name
 | 
					* ``DOCUMENT_FILE_NAME``
 | 
				
			||||||
* Source path
 | 
					* ``DOCUMENT_CREATED``
 | 
				
			||||||
* Thumbnail path
 | 
					* ``DOCUMENT_MODIFIED``
 | 
				
			||||||
* Download URL
 | 
					* ``DOCUMENT_ADDED``
 | 
				
			||||||
* Thumbnail URL
 | 
					* ``DOCUMENT_SOURCE_PATH``
 | 
				
			||||||
* Correspondent
 | 
					* ``DOCUMENT_ARCHIVE_PATH``
 | 
				
			||||||
* Tags
 | 
					* ``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 script
 | 
					The script can be in any language, but for a simple shell script
 | 
				
			||||||
example, you can take a look at `post-consumption-example.sh`_ in this project.
 | 
					example, you can take a look at `post-consumption-example.sh`_ in this project.
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,21 +1,16 @@
 | 
				
			|||||||
#!/usr/bin/env bash
 | 
					#!/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 "
 | 
					echo "
 | 
				
			||||||
 | 
					
 | 
				
			||||||
A document with an id of ${DOCUMENT_ID} was just consumed.  I know the
 | 
					A document with an id of ${DOCUMENT_ID} was just consumed.  I know the
 | 
				
			||||||
following additional information about it:
 | 
					following additional information about it:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
* Generated File Name: ${DOCUMENT_FILE_NAME}
 | 
					* Generated File Name: ${DOCUMENT_FILE_NAME}
 | 
				
			||||||
 | 
					* Archive Path: ${DOCUMENT_ARCHIVE_PATH}
 | 
				
			||||||
* Source Path: ${DOCUMENT_SOURCE_PATH}
 | 
					* Source Path: ${DOCUMENT_SOURCE_PATH}
 | 
				
			||||||
 | 
					* Created: ${DOCUMENT_CREATED}
 | 
				
			||||||
 | 
					* Added: ${DOCUMENT_ADDED}
 | 
				
			||||||
 | 
					* Modified: ${DOCUMENT_MODIFIED}
 | 
				
			||||||
* Thumbnail Path: ${DOCUMENT_THUMBNAIL_PATH}
 | 
					* Thumbnail Path: ${DOCUMENT_THUMBNAIL_PATH}
 | 
				
			||||||
* Download URL: ${DOCUMENT_DOWNLOAD_URL}
 | 
					* Download URL: ${DOCUMENT_DOWNLOAD_URL}
 | 
				
			||||||
* Thumbnail URL: ${DOCUMENT_THUMBNAIL_URL}
 | 
					* Thumbnail URL: ${DOCUMENT_THUMBNAIL_URL}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -134,8 +134,19 @@ class Consumer(LoggingMixin):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        self.log("info", f"Executing pre-consume script {settings.PRE_CONSUME_SCRIPT}")
 | 
					        self.log("info", f"Executing pre-consume script {settings.PRE_CONSUME_SCRIPT}")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        filepath_arg = os.path.normpath(self.path)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        script_env = os.environ.copy()
 | 
				
			||||||
 | 
					        script_env["DOCUMENT_SOURCE_PATH"] = filepath_arg
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        try:
 | 
					        try:
 | 
				
			||||||
            Popen((settings.PRE_CONSUME_SCRIPT, self.path)).wait()
 | 
					            Popen(
 | 
				
			||||||
 | 
					                (
 | 
				
			||||||
 | 
					                    settings.PRE_CONSUME_SCRIPT,
 | 
				
			||||||
 | 
					                    filepath_arg,
 | 
				
			||||||
 | 
					                ),
 | 
				
			||||||
 | 
					                env=script_env,
 | 
				
			||||||
 | 
					            ).wait()
 | 
				
			||||||
        except Exception as e:
 | 
					        except Exception as e:
 | 
				
			||||||
            self._fail(
 | 
					            self._fail(
 | 
				
			||||||
                MESSAGE_PRE_CONSUME_SCRIPT_ERROR,
 | 
					                MESSAGE_PRE_CONSUME_SCRIPT_ERROR,
 | 
				
			||||||
@@ -159,6 +170,33 @@ class Consumer(LoggingMixin):
 | 
				
			|||||||
            f"Executing post-consume script {settings.POST_CONSUME_SCRIPT}",
 | 
					            f"Executing post-consume script {settings.POST_CONSUME_SCRIPT}",
 | 
				
			||||||
        )
 | 
					        )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        script_env = os.environ.copy()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        script_env["DOCUMENT_ID"] = str(document.pk)
 | 
				
			||||||
 | 
					        script_env["DOCUMENT_CREATED"] = str(document.created)
 | 
				
			||||||
 | 
					        script_env["DOCUMENT_MODIFIED"] = str(document.modified)
 | 
				
			||||||
 | 
					        script_env["DOCUMENT_ADDED"] = str(document.added)
 | 
				
			||||||
 | 
					        script_env["DOCUMENT_FILE_NAME"] = document.get_public_filename()
 | 
				
			||||||
 | 
					        script_env["DOCUMENT_SOURCE_PATH"] = os.path.normpath(document.source_path)
 | 
				
			||||||
 | 
					        script_env["DOCUMENT_ARCHIVE_PATH"] = os.path.normpath(
 | 
				
			||||||
 | 
					            str(document.archive_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:
 | 
					        try:
 | 
				
			||||||
            Popen(
 | 
					            Popen(
 | 
				
			||||||
                (
 | 
					                (
 | 
				
			||||||
@@ -172,6 +210,7 @@ class Consumer(LoggingMixin):
 | 
				
			|||||||
                    str(document.correspondent),
 | 
					                    str(document.correspondent),
 | 
				
			||||||
                    str(",".join(document.tags.all().values_list("name", flat=True))),
 | 
					                    str(",".join(document.tags.all().values_list("name", flat=True))),
 | 
				
			||||||
                ),
 | 
					                ),
 | 
				
			||||||
 | 
					                env=script_env,
 | 
				
			||||||
            ).wait()
 | 
					            ).wait()
 | 
				
			||||||
        except Exception as e:
 | 
					        except Exception as e:
 | 
				
			||||||
            self._fail(
 | 
					            self._fail(
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user