Skip to content

Commit

Permalink
Prepend file hash to path when uploading local files
Browse files Browse the repository at this point in the history
To prevent overlaps with local files that have the same name but are different files, the path for artifacts is now `<artifact_dir>/<file_hash>/<filename>`.
Files with the same version and compatibility wil still raise an error to the frontend.

Fixes: #104
  • Loading branch information
UpstreamData committed Sep 5, 2024
1 parent 27bd862 commit a5708df
Showing 1 changed file with 5 additions and 15 deletions.
20 changes: 5 additions & 15 deletions goosebit/updates/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import shutil
from pathlib import Path
from urllib.parse import unquote, urlparse
Expand All @@ -10,6 +12,7 @@
from goosebit.db.models import Hardware, Software
from goosebit.updater.manager import UpdateManager

from ..settings import config
from . import swdesc


Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit a5708df

Please sign in to comment.