diff --git a/extensions/saves.py b/extensions/saves.py index 238db78..6f68ea6 100644 --- a/extensions/saves.py +++ b/extensions/saves.py @@ -1,6 +1,6 @@ from datetime import datetime from io import BytesIO -from os import listdir, makedirs, path, sep +from os import listdir, makedirs, path, rmdir, sep from typing import Dict, List from zipfile import ZipFile from xmltodict import parse @@ -10,7 +10,7 @@ from modules.utils import configGet, jsonLoad, jsonSave from fastapi import HTTPException, Depends, UploadFile from fastapi.responses import UJSONResponse, FileResponse, Response from fastapi.openapi.models import APIKey -from starlette.status import HTTP_404_NOT_FOUND, HTTP_406_NOT_ACCEPTABLE +from starlette.status import HTTP_204_NO_CONTENT, HTTP_404_NOT_FOUND, HTTP_406_NOT_ACCEPTABLE def zipfiles(filenames, save_name: str) -> Response: @@ -57,7 +57,7 @@ async def saves_get(apikey: APIKey = Depends(get_api_key)): output[str(id)].append(jsonLoad(path.join(configGet("data", "locations"), "users", apikey, id, dir, "index.json"))) # type: ignore return UJSONResponse(output) else: - return HTTPException(HTTP_404_NOT_FOUND, detail="Could not find any saves.") + raise HTTPException(HTTP_404_NOT_FOUND, detail="Could not find any saves.") @app.get("/saves/{id}", response_class=UJSONResponse, response_model=List[StardewSave], description="Get game saves by name") @@ -71,16 +71,45 @@ async def saves_get_by_id(id: str, apikey: APIKey = Depends(get_api_key)): output.append(jsonLoad(path.join(configGet("data", "locations"), "users", apikey, id, dir, "index.json"))) # type: ignore return UJSONResponse(output) else: - return HTTPException(HTTP_404_NOT_FOUND, detail="Could not find save with such id.") + raise HTTPException(HTTP_404_NOT_FOUND, detail="Could not find save with such id.") -@app.get("/saves/{id}/download", response_class=FileResponse, description="Get game save as .svsave file by its id and save date") +@app.delete("/saves/{id}", description="Get game saves by name") +async def saves_delete_by_id(id: str, apikey: APIKey = Depends(get_api_key)): + save_path = path.join(configGet("data", "locations"), "users", apikey, id) # type: ignore + if path.exists(save_path): + rmdir(save_path) + return Response(status_code=HTTP_204_NO_CONTENT) + else: + raise HTTPException(HTTP_404_NOT_FOUND, detail="Could not find save with such id.") + + +@app.delete("/saves/{id}/{save_date}", response_class=UJSONResponse, response_model=List[StardewSave], description="Get game saves by name") +async def saves_delete_by_both_ids(id: str, save_date: str, apikey: APIKey = Depends(get_api_key)): + save_path = path.join(configGet("data", "locations"), "users", apikey, id, save_date) # type: ignore + if path.exists(save_path): + rmdir(save_path) + return Response(status_code=HTTP_204_NO_CONTENT) + else: + raise HTTPException(HTTP_404_NOT_FOUND, detail="Could not find save with such id.") + + +@app.get("/saves/{id}/{save_date}", response_class=UJSONResponse, response_model=List[StardewSave], description="Get game saves by name") +async def saves_get_by_both_ids(id: str, save_date: str, apikey: APIKey = Depends(get_api_key)): + save_path = path.join(configGet("data", "locations"), "users", apikey, id, save_date) # type: ignore + if path.exists(save_path): + return UJSONResponse(jsonLoad(save_path+sep+"index.json")) + else: + raise HTTPException(HTTP_404_NOT_FOUND, detail="Could not find save with such id.") + + +@app.get("/saves/{id}/{save_date}/download", response_class=FileResponse, description="Get game save as .svsave file by its id and save date") async def saves_download(id: str, save_date: str, apikey: APIKey = Depends(get_api_key)): if path.exists(path.join(configGet("data", "locations"), "users", apikey, id, save_date)): # type: ignore save_path = path.join(configGet("data", "locations"), "users", apikey, id, save_date) # type: ignore return zipfiles([f"{save_path}{sep}{id}", f"{save_path}{sep}SaveGameInfo", f"{save_path}{sep}index.json"], save_name=f"{id}_{save_date}") else: - return HTTPException(HTTP_404_NOT_FOUND, detail="Could not find save with such id.") + raise HTTPException(HTTP_404_NOT_FOUND, detail="Could not find save with such id.") @app.post("/saves", response_class=UJSONResponse, response_model=StardewSave, description="Upload new save")