# This Source Code Form is subject to the terms of the Mozilla Public # License, v. 2.0. If a copy of the MPL was not distributed with this # file, You can obtain one at https://mozilla.org/MPL/2.0/. import uuid import xxhash from classquiz.config import redis, storage from tempfile import SpooledTemporaryFile from classquiz.db.models import StorageItem from classquiz.storage.errors import DeletionFailedError async def clean_editor_images_up(ctx): print("Cleaning images up") edit_sessions = await redis.smembers("edit_sessions") for session_id in edit_sessions: session = await redis.get(f"edit_session:{session_id}") if session is None: images = await redis.lrange(f"edit_session:{session_id}:images", 0, 3000) if len(images) != 0: try: await storage.delete(images) except DeletionFailedError: print("Deletion Error", images) await redis.srem("edit_sessions", session_id) await redis.delete(f"edit_session:{session_id}:images") async def calculate_hash(ctx, file_id_as_str: str): print("Calculating hash") file_id = uuid.UUID(file_id_as_str) file_data = await StorageItem.objects.get(id=file_id) file_path = file_id.hex if file_data.storage_path is not None: file_path = file_data.storage_path file = SpooledTemporaryFile() file.write((await storage.download(file_path)).getbuffer().tobytes()) hash_obj = xxhash.xxh3_128() # assert hash_obj.block_size == 64 while chunk := file.read(6400): hash_obj.update(chunk) file_data.hash = hash_obj.digest() print("Got hash!") await file_data.update() file.close()