diff --git a/backend/endpoints/saves.py b/backend/endpoints/saves.py index 19b2e7830..0b977893c 100644 --- a/backend/endpoints/saves.py +++ b/backend/endpoints/saves.py @@ -95,9 +95,7 @@ async def add_save( else: scanned_screenshot.rom_id = rom.id scanned_screenshot.user_id = request.user.id - db_screenshot = db_screenshot_handler.add_screenshot( - screenshot=scanned_screenshot - ) + db_screenshot_handler.add_screenshot(screenshot=scanned_screenshot) # Set the last played time for the current user rom_user = db_rom_handler.get_rom_user(rom_id=rom.id, user_id=request.user.id) @@ -155,6 +153,35 @@ async def update_save(request: Request, id: int) -> SaveSchema: db_save.id, {"file_size_bytes": saveFile.size} ) + screenshotFile: UploadFile | None = data.get("screenshotFile", None) # type: ignore + if screenshotFile and screenshotFile.filename: + screenshots_path = fs_asset_handler.build_screenshots_file_path( + user=request.user, platform_fs_slug=db_save.rom.platform_slug + ) + + fs_asset_handler.write_file(file=screenshotFile, path=screenshots_path) + + # Scan or update screenshot + scanned_screenshot = scan_screenshot( + file_name=screenshotFile.filename, + user=request.user, + platform_fs_slug=db_save.rom.platform_slug, + ) + db_screenshot = db_screenshot_handler.get_screenshot_by_filename( + rom_id=db_save.rom.id, + user_id=request.user.id, + file_name=screenshotFile.filename, + ) + if db_screenshot: + db_screenshot = db_screenshot_handler.update_screenshot( + db_screenshot.id, + {"file_size_bytes": scanned_screenshot.file_size_bytes}, + ) + else: + scanned_screenshot.rom_id = db_save.rom.id + scanned_screenshot.user_id = request.user.id + db_screenshot_handler.add_screenshot(screenshot=scanned_screenshot) + # Set the last played time for the current user rom_user = db_rom_handler.get_rom_user(db_save.rom_id, request.user.id) if not rom_user: diff --git a/backend/endpoints/states.py b/backend/endpoints/states.py index 352018ff3..585dfe455 100644 --- a/backend/endpoints/states.py +++ b/backend/endpoints/states.py @@ -155,6 +155,37 @@ async def update_state(request: Request, id: int) -> StateSchema: db_state.id, {"file_size_bytes": stateFile.size} ) + screenshotFile: UploadFile | None = data.get("screenshotFile", None) # type: ignore + if screenshotFile and screenshotFile.filename: + screenshots_path = fs_asset_handler.build_screenshots_file_path( + user=request.user, platform_fs_slug=db_state.rom.platform_slug + ) + + fs_asset_handler.write_file(file=screenshotFile, path=screenshots_path) + + # Scan or update screenshot + scanned_screenshot = scan_screenshot( + file_name=screenshotFile.filename, + user=request.user, + platform_fs_slug=db_state.rom.platform_slug, + ) + db_screenshot = db_screenshot_handler.get_screenshot_by_filename( + rom_id=db_state.rom.id, + user_id=request.user.id, + file_name=screenshotFile.filename, + ) + if db_screenshot: + db_screenshot = db_screenshot_handler.update_screenshot( + db_screenshot.id, + {"file_size_bytes": scanned_screenshot.file_size_bytes}, + ) + else: + scanned_screenshot.rom_id = db_state.rom.id + scanned_screenshot.user_id = request.user.id + db_screenshot = db_screenshot_handler.add_screenshot( + screenshot=scanned_screenshot + ) + # Set the last played time for the current user rom_user = db_rom_handler.get_rom_user(db_state.rom_id, request.user.id) if not rom_user: diff --git a/frontend/src/services/api/save.ts b/frontend/src/services/api/save.ts index 0222a7218..0a65bc00c 100644 --- a/frontend/src/services/api/save.ts +++ b/frontend/src/services/api/save.ts @@ -44,12 +44,15 @@ async function uploadSaves({ async function updateSave({ save, saveFile, + screenshotFile, }: { save: SaveSchema; saveFile: File; + screenshotFile?: File; }): Promise<{ data: SaveSchema }> { const formData = new FormData(); formData.append("saveFile", saveFile); + if (screenshotFile) formData.append("screenshotFile", screenshotFile); return api.put(`/saves/${save.id}`, formData); } diff --git a/frontend/src/services/api/state.ts b/frontend/src/services/api/state.ts index 23881bedf..4dccd7a92 100644 --- a/frontend/src/services/api/state.ts +++ b/frontend/src/services/api/state.ts @@ -44,12 +44,15 @@ async function uploadStates({ async function updateState({ state, stateFile, + screenshotFile, }: { state: StateSchema; stateFile: File; + screenshotFile?: File; }): Promise<{ data: StateSchema }> { const formData = new FormData(); formData.append("stateFile", stateFile); + if (screenshotFile) formData.append("screenshotFile", screenshotFile); return api.put(`/states/${state.id}`, formData); } diff --git a/frontend/src/views/Player/EmulatorJS/utils.ts b/frontend/src/views/Player/EmulatorJS/utils.ts index 65038284c..5e700653f 100644 --- a/frontend/src/views/Player/EmulatorJS/utils.ts +++ b/frontend/src/views/Player/EmulatorJS/utils.ts @@ -72,6 +72,12 @@ export async function saveSave({ saveFile: new File([saveFile], save.file_name, { type: "application/octet-stream", }), + screenshotFile: + screenshotFile && save.screenshot + ? new File([screenshotFile], save.screenshot.file_name, { + type: "application/octet-stream", + }) + : undefined, }); // Update the save in the rom object