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

add files integrity periodic check

parent 5c70ed97
Loading
Loading
Loading
Loading
+25 −0
Original line number Diff line number Diff line
@@ -5,11 +5,13 @@ from typing import List
from django.conf import settings
from django.urls import reverse
from django.utils import timezone
from django.db import connection
from resources import models
from PIL import Image
import os
import time
import shutil
import hashlib
from PIL import UnidentifiedImageError
from PIL.Image import DecompressionBombError

@@ -314,3 +316,26 @@ def denorm_sizes():
            logger.warning(f"can't identify {file.pk}")
        except DecompressionBombError:
            logger.warning(f"can't decompress {file.pk}")


@db_periodic_task(crontab(day="*/7"))
def check_files_integrity():
    def file_hash256(file_path: str) -> str:
        try:
            hsh = hashlib.sha256()
            with open(file_path, "rb") as f:
                for chnk in iter(lambda: f.read(8192), b""):
                    hsh.update(chnk)
            return hsh.hexdigest()
        except FileNotFoundError:
            return ""

    cursor = connection.cursor()
    cursor.execute(
        "select hash, resource_ptr_id from resources_file where resources_file.tiled = false order by resource_ptr_id"
    )
    for row in cursor.fetchall():
        new_hash = file_hash256(models.hash_to_local_path(row[0]))
        if new_hash != row[0]:
            logger.warning(f"FILE INTEGRITY CHECK FAILED. File id: {row[1]}")
    connection.close()