From a5708dfefc74f6ca9020da587ae1a0f71381d15e Mon Sep 17 00:00:00 2001 From: Upstream Data Date: Thu, 5 Sep 2024 09:15:57 -0600 Subject: [PATCH] Prepend file hash to path when uploading local files To prevent overlaps with local files that have the same name but are different files, the path for artifacts is now `//`. Files with the same version and compatibility wil still raise an error to the frontend. Fixes: #104 --- goosebit/updates/__init__.py | 20 +++++--------------- 1 file changed, 5 insertions(+), 15 deletions(-) diff --git a/goosebit/updates/__init__.py b/goosebit/updates/__init__.py index b2c08d1e..51c1c6dd 100644 --- a/goosebit/updates/__init__.py +++ b/goosebit/updates/__init__.py @@ -1,3 +1,5 @@ +from __future__ import annotations + import shutil from pathlib import Path from urllib.parse import unquote, urlparse @@ -10,6 +12,7 @@ from goosebit.db.models import Hardware, Software from goosebit.updater.manager import UpdateManager +from ..settings import config from . import swdesc @@ -42,7 +45,8 @@ async def create_software_update(uri: str, temp_file: Path | None) -> Software: # for local file: rename temp file to final name if parsed_uri.scheme == "file": - path = _unique_path(parsed_uri) + filename = Path(url2pathname(unquote(parsed_uri.path))).name + path = config.artifacts_dir.joinpath(update_info["hash"], filename) path.parent.mkdir(parents=True, exist_ok=True) shutil.copy(temp_file, path) uri = path.absolute().as_uri() @@ -85,20 +89,6 @@ async def _is_software_colliding(update_info): return is_colliding -def _unique_path(uri): - path = Path(url2pathname(unquote(uri.path))) - if not path.exists(): - return path - - counter = 1 - new_path = path.with_name(f"{path.stem}-{counter}{path.suffix}") - while new_path.exists(): - counter += 1 - new_path = path.with_name(f"{path.stem}-{counter}{path.suffix}") - - return new_path - - async def generate_chunk(request: Request, updater: UpdateManager) -> list: _, software = await updater.get_update() if software is None: