diff --git a/backend/endpoints/rom.py b/backend/endpoints/rom.py index 066def054..944ed5238 100644 --- a/backend/endpoints/rom.py +++ b/backend/endpoints/rom.py @@ -17,14 +17,22 @@ from stat import S_IFREG from stream_zip import ZIP_64, stream_zip # type: ignore[import] +from config import LIBRARY_BASE_PATH from logger.logger import log +from models import Rom from handler import dbh -from utils import fs, get_file_name_with_no_tags -from utils.fs import _rom_exists, build_artwork_path, build_upload_roms_path from exceptions.fs_exceptions import RomNotFoundError, RomAlreadyExistsException from utils.oauth import protected_route -from models import Rom -from config import LIBRARY_BASE_PATH +from utils import get_file_name_with_no_tags +from utils.fs import ( + _rom_exists, + build_artwork_path, + build_upload_roms_path, + rename_rom, + get_cover, + get_screenshots, + remove_rom, +) from .utils import CustomStreamingResponse @@ -180,12 +188,12 @@ def roms( async def update_rom( request: Request, id: int, + rename_as_igdb: bool = False, artwork: Optional[UploadFile] = File(None), ) -> dict: """Updates rom details""" data = await request.form() - rename_as_igdb: bool = data["renameAsIGDB"] cleaned_data = {} cleaned_data["igdb_id"] = data["igdb_id"] @@ -205,18 +213,17 @@ async def update_rom( try: if file_name != db_rom.file_name: - fs.rename_rom(db_rom.platform_slug, db_rom.file_name, file_name) + rename_rom(db_rom.platform_slug, db_rom.file_name, file_name) except RomAlreadyExistsException as e: log.error(str(e)) raise HTTPException( status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=str(e) ) - cleaned_data["file_name_no_tags"] = get_file_name_with_no_tags( - cleaned_data["file_name"] - ) + cleaned_data["file_name"] = file_name + cleaned_data["file_name_no_tags"] = get_file_name_with_no_tags(file_name) cleaned_data.update( - fs.get_cover( + get_cover( overwrite=True, fs_slug=db_rom.platform_slug, rom_name=cleaned_data["file_name_no_tags"], @@ -224,7 +231,7 @@ async def update_rom( ) ) cleaned_data.update( - fs.get_screenshots( + get_screenshots( fs_slug=db_rom.platform_slug, rom_name=cleaned_data["file_name_no_tags"], url_screenshots=cleaned_data.get("url_screenshots", []), @@ -267,7 +274,7 @@ def _delete_single_rom(rom_id: int, delete_from_fs: bool = False): if delete_from_fs: log.info(f"Deleting {rom.file_name} from filesystem") try: - fs.remove_rom(rom.platform_slug, rom.file_name) + remove_rom(rom.platform_slug, rom.file_name) except RomNotFoundError as e: error = f"Couldn't delete from filesystem: {str(e)}" log.error(error) diff --git a/backend/endpoints/tests/test_rom.py b/backend/endpoints/tests/test_rom.py index ec722d706..5c95bc962 100644 --- a/backend/endpoints/tests/test_rom.py +++ b/backend/endpoints/tests/test_rom.py @@ -30,16 +30,16 @@ def test_get_all_roms(access_token, rom): assert body["items"][0]["id"] == rom.id -@patch("utils.fs.rename_rom") +@patch("endpoints.rom.rename_rom") def test_update_rom(rename_rom, access_token, rom): response = client.patch( f"/roms/{rom.id}", headers={"Authorization": f"Bearer {access_token}"}, + params={"rename_as_igdb": True}, data={ "igdb_id": "236663", "name": "Metroid Prime Remastered", "slug": "metroid-prime-remastered", - "file_name": "Metroid Prime Remastered.xci", "summary": "summary test", "url_cover": "https://images.igdb.com/igdb/image/upload/t_cover_big/co2l7z.jpg", "url_screenshots": json.dumps( @@ -53,7 +53,7 @@ def test_update_rom(rename_rom, access_token, rom): assert response.status_code == 200 body = response.json() - assert body["rom"]["file_name"] == "Metroid Prime Remastered.xci" + assert body["rom"]["file_name"] == "Metroid Prime Remastered.zip" assert rename_rom.called diff --git a/backend/handler/tests/conftest.py b/backend/handler/tests/conftest.py index cd3e6e5c6..d7282f001 100644 --- a/backend/handler/tests/conftest.py +++ b/backend/handler/tests/conftest.py @@ -40,7 +40,7 @@ def rom(platform): name="test_rom", slug="test_rom_slug", platform_slug="test_platform_slug", - file_name="test_rom", + file_name="test_rom.zip", file_name_no_tags="test_rom", file_extension="zip", file_path="test_platform_slug/roms", diff --git a/backend/handler/tests/test_db_handler.py b/backend/handler/tests/test_db_handler.py index 23d7d8a90..669db6886 100644 --- a/backend/handler/tests/test_db_handler.py +++ b/backend/handler/tests/test_db_handler.py @@ -45,7 +45,7 @@ def test_roms(rom): assert len(roms) == 2 rom = dbh.get_rom(roms[0].id) - assert rom.file_name == "test_rom" + assert rom.file_name == "test_rom.zip" dbh.update_rom(roms[1].id, {"file_name": "test_rom_2_updated"}) rom_2 = dbh.get_rom(roms[1].id) diff --git a/backend/handler/tests/test_igdb_handler.py b/backend/handler/tests/test_igdb_handler.py index 141520f49..4bf3f0503 100644 --- a/backend/handler/tests/test_igdb_handler.py +++ b/backend/handler/tests/test_igdb_handler.py @@ -13,7 +13,7 @@ def test_get_platform(): assert platform["name"] == "Nintendo 64" platform = igdbh.get_platform("not_real") - assert platform == {"igdb_id": "", "name": "not_real", "slug": "not_real"} + assert platform == {"igdb_id": "", "name": "not_real"} @pytest.mark.vcr() diff --git a/backend/models/tests/test_rom.py b/backend/models/tests/test_rom.py index a34a23d15..4e0d97db8 100644 --- a/backend/models/tests/test_rom.py +++ b/backend/models/tests/test_rom.py @@ -1,3 +1,3 @@ def test_rom(rom): assert rom.file_path == "test_platform_slug/roms" - assert rom.full_path == "test_platform_slug/roms/test_rom" + assert rom.full_path == "test_platform_slug/roms/test_rom.zip" diff --git a/backend/utils/fs.py b/backend/utils/fs.py index de0f91514..9302c00c1 100644 --- a/backend/utils/fs.py +++ b/backend/utils/fs.py @@ -70,10 +70,13 @@ def _get_cover_path(fs_slug: str, rom_name: str, size: str): return f"{fs_slug}/{rom_name}/cover/{size}.png?timestamp={strtime}" -def get_cover(overwrite: bool, fs_slug: str, rom_name: str, url_cover: str = "") -> dict: +def get_cover( + overwrite: bool, fs_slug: str, rom_name: str, url_cover: str = "" +) -> dict: # Cover small if (overwrite or not _cover_exists(fs_slug, rom_name, "small")) and url_cover: _store_cover(fs_slug, rom_name, url_cover, "small") + path_cover_s = ( _get_cover_path(fs_slug, rom_name, "small") if _cover_exists(fs_slug, rom_name, "small") @@ -300,7 +303,7 @@ def remove_rom(fs_slug: str, file_name: str): shutil.rmtree(f"{LIBRARY_BASE_PATH}/{rom_path}/{file_name}") except FileNotFoundError as exc: raise RomNotFoundError(file_name, fs_slug) from exc - + def build_upload_roms_path(fs_slug: str): rom_path = get_roms_structure(fs_slug) diff --git a/backend/utils/tests/test_fs.py b/backend/utils/tests/test_fs.py index 7571ece50..9971df583 100644 --- a/backend/utils/tests/test_fs.py +++ b/backend/utils/tests/test_fs.py @@ -29,9 +29,9 @@ def test_get_cover(): rom_name="Paper Mario", ) - assert "n64/Paper Mario/cover/small.png" in cover["path_cover_s"] - assert "n64/Paper Mario/cover/big.png" in cover["path_cover_l"] - assert cover["has_cover"] == 1 + assert DEFAULT_PATH_COVER_S in cover["path_cover_s"] + assert DEFAULT_PATH_COVER_L in cover["path_cover_l"] + assert cover["has_cover"] == 0 # Game: Paper Mario (USA).z64 cover = get_cover( diff --git a/frontend/src/services/api.js b/frontend/src/services/api.js index 0e94228fd..2f13f9f5d 100644 --- a/frontend/src/services/api.js +++ b/frontend/src/services/api.js @@ -89,10 +89,11 @@ async function updateRom({ rom, renameAsIGDB = false }) { formData.append("url_cover", rom.url_cover); formData.append("summary", rom.summary); formData.append("url_screenshots", JSON.stringify(rom.url_screenshots)); - formData.append("renameAsIGDB", renameAsIGDB); if (artwork) formData.append("artwork", rom.artwork[0]); - return api.patch(`/roms/${rom.id}`, formData); + return api.patch(`/roms/${rom.id}`, formData, { + params: { rename_as_igdb: renameAsIGDB }, + }); } async function deleteRom({ rom, deleteFromFs = false }) { @@ -114,7 +115,11 @@ async function deleteRoms({ roms, deleteFromFs = false }) { } async function searchIGDB({ romId, query, field }) { - return api.put("/search/roms/igdb", {}, { params: { rom_id: romId, query, field } }); + return api.put( + "/search/roms/igdb", + {}, + { params: { rom_id: romId, query, field } } + ); } async function fetchCurrentUser() {