From 192acb1479b7bb4989864d1a707e60ebf8e4003d Mon Sep 17 00:00:00 2001 From: Sakura Akeno Isayeki Date: Fri, 21 Jul 2023 12:41:42 +0200 Subject: [PATCH] feat(cli): Remove unused import and variable The commit removes an unused import and variable in the `cli.py` file. The import for `Content` from the models module is removed, as it is not being used in the code. fix(config): Add temporary work directory for replay processing This commit adds a new configuration option to the `default.toml` file. The `temp_workdir` setting specifies the temporary directory path for replay processing. The default value is set to "/tmp/wowskarma/minimap". refactor(routes): Update filepath handling in render_replay function In this commit, the filepath handling in the `render_replay` function of the `render.py` file is updated. Instead of using a hardcoded path, it now uses the configured temporary work directory from settings. Additionally, pathlib.Path is used to concatenate filepaths and ensure platform compatibility. --- wowskarma_api_minimap/src/cli.py | 2 -- wowskarma_api_minimap/src/default.toml | 3 ++- wowskarma_api_minimap/src/routes/render.py | 8 +++++--- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/wowskarma_api_minimap/src/cli.py b/wowskarma_api_minimap/src/cli.py index c1715418..f5403433 100644 --- a/wowskarma_api_minimap/src/cli.py +++ b/wowskarma_api_minimap/src/cli.py @@ -5,7 +5,6 @@ from .app import app from .config import settings from .db import create_db_and_tables, engine -from .models.content import Content from .security import User cli = typer.Typer(name="WOWS Karma - Minimap API") @@ -53,7 +52,6 @@ def shell(): # pragma: no cover "create_user": create_user, "select": select, "session": Session(engine), - "Content": Content, } typer.echo(f"Auto imports: {list(_vars.keys())}") try: diff --git a/wowskarma_api_minimap/src/default.toml b/wowskarma_api_minimap/src/default.toml index 9ed4cd8c..c7c4a479 100644 --- a/wowskarma_api_minimap/src/default.toml +++ b/wowskarma_api_minimap/src/default.toml @@ -19,4 +19,5 @@ connect_args = {check_same_thread=false} echo = false [default.replay] -max_file_size = 5242880 # Max replay file size in bytes. Default is 5MB \ No newline at end of file +max_file_size = 5242880 # Max replay file size in bytes. Default is 5MB +temp_workdir = "/tmp/wowskarma/minimap" # Temporary directory for replay processing diff --git a/wowskarma_api_minimap/src/routes/render.py b/wowskarma_api_minimap/src/routes/render.py index 4da5c9bb..7005cca7 100644 --- a/wowskarma_api_minimap/src/routes/render.py +++ b/wowskarma_api_minimap/src/routes/render.py @@ -4,6 +4,7 @@ import tempfile import os +import pathlib from fastapi import APIRouter, HTTPException, status, UploadFile, BackgroundTasks from fastapi.responses import FileResponse from renderer.data import ReplayData @@ -43,20 +44,21 @@ async def render_replay( # /tmp/wows-karma/minimap/ # Ensure the folder exists, if not, create it. - os.makedirs("/tmp/wows-karma/minimap/", exist_ok=True) + os.makedirs(settings.replay.temp_workdir, exist_ok=True) # Render time! replay_info = ReplayParser(file.file, True).get_info() replay_data: ReplayData = replay_info["hidden"]["replay_data"] - filepath = f"/tmp/wows-karma/minimap/{binascii.b2a_hex(os.urandom(7))}.mp4" + # Concat filepaths to get the full path to the replay file. + filepath = pathlib.Path(settings.replay.temp_workdir, f"{binascii.b2a_hex(os.urandom(7))}.mp4") renderer = Renderer( replay_data, enable_chat=True, team_tracers=True, target_player_id=target_player_id ) - renderer.start(filepath) + renderer.start(str(filepath)) background_tasks.add_task(os.remove, filepath) return FileResponse(filepath, media_type="video/mp4", filename=f"{replay_id or 'replay'}.mp4") \ No newline at end of file