Skip to content

Commit

Permalink
Allow checking file sha256
Browse files Browse the repository at this point in the history
  • Loading branch information
Willy-JL committed Aug 27, 2024
1 parent f6e8f91 commit c7291a9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
4 changes: 2 additions & 2 deletions indexer/src/directories.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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,
)
Expand Down
17 changes: 16 additions & 1 deletion indexer/src/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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


Expand Down

0 comments on commit c7291a9

Please sign in to comment.