Commit f39e6434 authored by Mickaël Desfrênes's avatar Mickaël Desfrênes
Browse files

make file integrity check optional

parent a055a937
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
__version__ = "0.1.106"
__version__ = "0.1.107"
+3 −0
Original line number Diff line number Diff line
@@ -44,6 +44,9 @@ if os.getenv("GEOS_LIBRARY_PATH"):

MODSHIB_CREATE_ACCOUNT = True if os.getenv("MODSHIB_CREATE_ACCOUNT") == "1" else False
JAMA_SHOW_RPC_DOCS = False if os.getenv("JAMA_SHOW_RPC_DOCS") != "1" else True
JAMA_PERIODIC_FILE_INTEGRITY_CHECK = (
    True if os.getenv("JAMA_PERIODIC_FILE_INTEGRITY_CHECK") == "1" else False
)
JAMA_USE_POSTGIS = True if os.getenv("JAMA_USE_POSTGIS") == "1" else False
JAMA_ROOT_URL_REDIRECT = os.getenv("JAMA_ROOT_URL_REDIRECT")
JAMA_CACHE_BACKEND = os.getenv("JAMA_CACHE_BACKEND") or "file"
+15 −0
Original line number Diff line number Diff line
@@ -13,6 +13,7 @@ import shutil
import hashlib
from PIL import UnidentifiedImageError
from PIL.Image import DecompressionBombError
from django.core.mail import mail_admins

logger = logging.getLogger(__name__)

@@ -294,6 +295,7 @@ def update_data_from_xlsx_rows(

@db_periodic_task(crontab(hour="*/6"))
def ensureiiif():
    logger.info("ensure IIIF...")
    from resources.models import File
    from resources.helpers import iiif_destination_dir_from_hash

@@ -320,6 +322,7 @@ def ensureiiif():

@db_periodic_task(crontab(hour="*/6"))
def clean_partial_uploads():
    logger.info("clean partial uploads...")
    now = time.time()
    max_age = 60 * 60 * 6
    sub_folders = [
@@ -334,6 +337,7 @@ def clean_partial_uploads():

@db_periodic_task(crontab(hour="*/6"))
def denorm_sizes():
    logger.info("denorm image sizes...")
    from resources.models import File

    start_date = timezone.now() - timezone.timedelta(days=1)
@@ -364,6 +368,12 @@ def denorm_sizes():

@db_periodic_task(crontab(day="1"))
def check_files_integrity():
    logger.info("file integrity check...")
    if not settings.JAMA_PERIODIC_FILE_INTEGRITY_CHECK:
        logger.info(
            "skipping file integrity check as per JAMA_PERIODIC_FILE_INTEGRITY_CHECK"
        )

    def file_hash256(file_path: str) -> str:
        try:
            hsh = hashlib.sha256()
@@ -382,6 +392,11 @@ def check_files_integrity():
        new_hash = file_hash256(models.hash_to_local_path(row[0]))
        if new_hash != row[0]:
            logger.warning(f"FILE INTEGRITY CHECK FAILED. File hash: {row[0]}")
            mail_admins(
                f"[{settings.JAMA_SITE}] FILE INTEGRITY CHECK FAILED",
                f"File hash: {row[0]}",
                fail_silently=True,
            )
            models.File.objects.filter(
                hash=row[0], integrity_check_failed_at__isnull=True
            ).update(integrity_check_failed_at=timezone.now())