From 347968536ce7357d7ec34d1ab94c52b372341c8c Mon Sep 17 00:00:00 2001 From: pk5ls20 Date: Sat, 30 Dec 2023 21:46:10 +0800 Subject: [PATCH] Use uuid5 associated with file SHA1 instead of randomly generated uuid4 --- app/util/generate_uuid.py | 20 ++++++++++++++++++++ scripts/local_indexing.py | 4 ++-- 2 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 app/util/generate_uuid.py diff --git a/app/util/generate_uuid.py b/app/util/generate_uuid.py new file mode 100644 index 0000000..9861205 --- /dev/null +++ b/app/util/generate_uuid.py @@ -0,0 +1,20 @@ +import io +import pathlib +import hashlib +from uuid import UUID, uuid5, NAMESPACE_DNS + +NAMESPACE_STR = 'github.com/hv0905/NekoImageGallary' + + +def generate(file_input: pathlib.Path | io.BytesIO) -> UUID: + namespace_uuid = uuid5(NAMESPACE_DNS, NAMESPACE_STR) + if isinstance(file_input, pathlib.Path): + with open(file_input, 'rb') as f: + file_content = f.read() + elif isinstance(file_input, io.BytesIO): + file_input.seek(0) + file_content = file_input.read() + else: + raise ValueError("Unsupported file type. Must be pathlib.Path or io.BytesIO.") + file_hash = hashlib.sha1(file_content).hexdigest() + return uuid5(namespace_uuid, file_hash) diff --git a/scripts/local_indexing.py b/scripts/local_indexing.py index 74e0920..fcfdefb 100644 --- a/scripts/local_indexing.py +++ b/scripts/local_indexing.py @@ -1,7 +1,6 @@ from datetime import datetime from pathlib import Path from shutil import copy2 -from uuid import uuid4 import PIL from PIL import Image @@ -10,6 +9,7 @@ from app.Models.img_data import ImageData from app.Services.provider import index_service from app.config import config +from app.util import generate_uuid from .local_utility import gather_valid_files @@ -19,7 +19,7 @@ async def copy_and_index(file_path: Path): except PIL.UnidentifiedImageError as e: logger.error("Error when opening image {}: {}", file_path, e) return - image_id = uuid4() + image_id = generate_uuid.generate(file_path) img_ext = file_path.suffix imgdata = ImageData(id=image_id, url=f'/static/{image_id}{img_ext}',