From 0e2e5f3413ba265ac209ec9e755702671e47f30a Mon Sep 17 00:00:00 2001 From: Trenton Holmes Date: Tue, 19 Jul 2022 13:57:00 -0700 Subject: [PATCH] Creates utiliy to ensure all paths in settings are normalized and absolute --- src/paperless/settings.py | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/src/paperless/settings.py b/src/paperless/settings.py index bfb9507ba..5c698532c 100644 --- a/src/paperless/settings.py +++ b/src/paperless/settings.py @@ -4,6 +4,7 @@ import math import multiprocessing import os import re +import tempfile from typing import Final from typing import Optional from typing import Set @@ -56,6 +57,13 @@ def __get_float(key: str, default: float) -> float: return float(os.getenv(key, default)) +def __get_path(key: str, default: str) -> str: + """ + Return a normalized, absolute path based on the environment variable or a default + """ + return os.path.abspath(os.path.normpath(os.environ.get(key, default))) + + # NEVER RUN WITH DEBUG IN PRODUCTION. DEBUG = __get_boolean("PAPERLESS_DEBUG", "NO") @@ -66,14 +74,14 @@ DEBUG = __get_boolean("PAPERLESS_DEBUG", "NO") BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) -STATIC_ROOT = os.getenv("PAPERLESS_STATICDIR", os.path.join(BASE_DIR, "..", "static")) +STATIC_ROOT = __get_path("PAPERLESS_STATICDIR", os.path.join(BASE_DIR, "..", "static")) -MEDIA_ROOT = os.getenv("PAPERLESS_MEDIA_ROOT", os.path.join(BASE_DIR, "..", "media")) +MEDIA_ROOT = __get_path("PAPERLESS_MEDIA_ROOT", os.path.join(BASE_DIR, "..", "media")) ORIGINALS_DIR = os.path.join(MEDIA_ROOT, "documents", "originals") ARCHIVE_DIR = os.path.join(MEDIA_ROOT, "documents", "archive") THUMBNAIL_DIR = os.path.join(MEDIA_ROOT, "documents", "thumbnails") -DATA_DIR = os.getenv("PAPERLESS_DATA_DIR", os.path.join(BASE_DIR, "..", "data")) +DATA_DIR = __get_path("PAPERLESS_DATA_DIR", os.path.join(BASE_DIR, "..", "data")) TRASH_DIR = os.getenv("PAPERLESS_TRASH_DIR") @@ -83,15 +91,18 @@ MEDIA_LOCK = os.path.join(MEDIA_ROOT, "media.lock") INDEX_DIR = os.path.join(DATA_DIR, "index") MODEL_FILE = os.path.join(DATA_DIR, "classification_model.pickle") -LOGGING_DIR = os.getenv("PAPERLESS_LOGGING_DIR", os.path.join(DATA_DIR, "log")) +LOGGING_DIR = __get_path("PAPERLESS_LOGGING_DIR", os.path.join(DATA_DIR, "log")) -CONSUMPTION_DIR = os.getenv( +CONSUMPTION_DIR = __get_path( "PAPERLESS_CONSUMPTION_DIR", os.path.join(BASE_DIR, "..", "consume"), ) # This will be created if it doesn't exist -SCRATCH_DIR = os.getenv("PAPERLESS_SCRATCH_DIR", "/tmp/paperless") +SCRATCH_DIR = __get_path( + "PAPERLESS_SCRATCH_DIR", + os.path.join(tempfile.gettempdir(), "paperless"), +) ############################################################################### # Application Definition #