From c7291a9fa65d687130c0eef5d3b4fbb59e290375 Mon Sep 17 00:00:00 2001 From: Willy-JL <49810075+Willy-JL@users.noreply.github.com> Date: Tue, 27 Aug 2024 02:56:56 +0200 Subject: [PATCH] Allow checking file sha256 --- indexer/src/directories.py | 4 ++-- indexer/src/repository.py | 17 ++++++++++++++++- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/indexer/src/directories.py b/indexer/src/directories.py index 0b516b1..6dfb0e1 100644 --- a/indexer/src/directories.py +++ b/indexer/src/directories.py @@ -83,7 +83,7 @@ async def repository_file_request(channel, file_name): elif isinstance(index, PacksCatalog): @router.get(prefix + "/{pack}/{file_type}/{file_name}") - async def pack_file_request(pack, file_type, file_name): + async def pack_file_request(pack, file_type, file_name, sha256: str = None): """ A method for retrieving a file from a specific pack Args: @@ -98,7 +98,7 @@ async def pack_file_request(pack, file_type, file_name): return JSONResponse("No packs found!", status_code=404) try: return FileResponse( - index.get_file_path(pack, file_type, file_name), + index.get_file_path(pack, file_type, file_name, sha256), media_type="application/octet-stream", status_code=200, ) diff --git a/indexer/src/repository.py b/indexer/src/repository.py index f4205cf..7387b24 100644 --- a/indexer/src/repository.py +++ b/indexer/src/repository.py @@ -212,7 +212,9 @@ def reindex(self): logging.exception(e) raise e - def get_file_path(self: str, pack: str, file_type: str, file_name: str) -> str: + def get_file_path( + self: str, pack: str, file_type: str, file_name: str, sha256: str + ) -> str: """ A method to get a specific file by type and name in the specified pack Args: @@ -228,6 +230,19 @@ def get_file_path(self: str, pack: str, file_type: str, file_name: str) -> str: ) if file_type not in ("download", "preview") or not os.path.isfile(file_path): raise FileNotFoundError("File not found, try a newer link!") + if sha256 and file_type == "download": + matches = False + for pack_i in self.index["packs"]: + if pack_i["id"] != pack: + continue + for file_i in pack_i["files"]: + if not file_i["url"].endswith(file_name): + continue + matches = file_i["sha256"] == sha256 + break + break + if not matches: + raise FileNotFoundError("File not found, try a newer link!") return file_path