paperless-ngx/src/paperless/checks.py

96 lines
2.8 KiB
Python
Raw Normal View History

import os
import shutil
import stat
from django.conf import settings
2018-03-18 15:42:19 +00:00
from django.core.checks import Error, Warning, register
2020-10-26 00:35:24 +01:00
exists_message = "{} is set but doesn't exist."
exists_hint = "Create a directory at {}"
writeable_message = "{} is not writeable"
writeable_hint = (
"Set the permissions of {} to be writeable by the user running the "
"Paperless services"
)
2020-11-12 21:09:45 +01:00
2020-12-19 15:16:42 +01:00
def path_check(var, directory):
2020-10-26 00:35:24 +01:00
messages = []
if directory:
if not os.path.isdir(directory):
2022-02-27 15:26:41 +01:00
messages.append(
Error(exists_message.format(var), exists_hint.format(directory))
)
else:
2021-04-05 21:33:04 +02:00
test_file = os.path.join(
2022-02-27 15:26:41 +01:00
directory, f"__paperless_write_test_{os.getpid()}__"
2021-04-05 21:33:04 +02:00
)
try:
2022-02-27 15:26:41 +01:00
with open(test_file, "w"):
2021-04-05 21:33:04 +02:00
pass
except PermissionError:
2022-02-27 15:26:41 +01:00
messages.append(
Error(
writeable_message.format(var),
writeable_hint.format(
f"\n{stat.filemode(os.stat(directory).st_mode)} "
f"{directory}\n"
),
)
)
2021-04-05 21:33:04 +02:00
finally:
if os.path.isfile(test_file):
os.remove(test_file)
2020-10-26 00:35:24 +01:00
return messages
2020-11-12 21:09:45 +01:00
@register()
def paths_check(app_configs, **kwargs):
"""
Check the various paths for existence, readability and writeability
"""
2022-02-27 15:26:41 +01:00
return (
path_check("PAPERLESS_DATA_DIR", settings.DATA_DIR)
+ path_check("PAPERLESS_TRASH_DIR", settings.TRASH_DIR)
+ path_check("PAPERLESS_MEDIA_ROOT", settings.MEDIA_ROOT)
+ path_check("PAPERLESS_CONSUMPTION_DIR", settings.CONSUMPTION_DIR)
)
@register()
def binaries_check(app_configs, **kwargs):
"""
Paperless requires the existence of a few binaries, so we do some checks
for those here.
"""
error = "Paperless can't find {}. Without it, consumption is impossible."
hint = "Either it's not in your ${PATH} or it's not installed."
2022-02-27 15:26:41 +01:00
binaries = (settings.CONVERT_BINARY, settings.OPTIPNG_BINARY, "tesseract")
check_messages = []
for binary in binaries:
if shutil.which(binary) is None:
check_messages.append(Warning(error.format(binary), hint))
return check_messages
2020-11-12 21:20:12 +01:00
@register()
def debug_mode_check(app_configs, **kwargs):
if settings.DEBUG:
2022-02-27 15:26:41 +01:00
return [
Warning(
"DEBUG mode is enabled. Disable Debug mode. This is a serious "
"security issue, since it puts security overides in place which "
"are meant to be only used during development. This "
"also means that paperless will tell anyone various "
"debugging information when something goes wrong."
)
]
2020-11-12 21:20:12 +01:00
else:
return []