From 6368b32ef4c53449d9e96c1eaab1df7bfe0d2cfd Mon Sep 17 00:00:00 2001 From: Georges-Antoine Assi Date: Sat, 5 Apr 2025 19:53:04 -0400 Subject: [PATCH] [ROMM-1795] Dont generate m3u file if folder already has one --- backend/endpoints/rom.py | 45 ++++++++++++++++++++++------------------ backend/models/rom.py | 7 +++++++ 2 files changed, 32 insertions(+), 20 deletions(-) diff --git a/backend/endpoints/rom.py b/backend/endpoints/rom.py index 3d56fd3e0..9e54ea86c 100644 --- a/backend/endpoints/rom.py +++ b/backend/endpoints/rom.py @@ -361,15 +361,18 @@ async def get_rom_content( log.error(f"File {file_path} not found!") raise - # Add M3U file - m3u_encoded_content = "\n".join( - [f.file_name_for_download(rom, hidden_folder) for f in files] - ).encode() - m3u_filename = f"{rom.fs_name}.m3u" - m3u_info = ZipInfo(filename=m3u_filename, date_time=now.timetuple()[:6]) - m3u_info.external_attr = S_IFREG | 0o600 - m3u_info.compress_type = ZIP_STORED - zip_file.writestr(m3u_info, m3u_encoded_content) + # Add M3U file if not already present + if not rom.has_m3u_file(): + m3u_encoded_content = "\n".join( + [f.file_name_for_download(rom, hidden_folder) for f in files] + ).encode() + m3u_filename = f"{rom.fs_name}.m3u" + m3u_info = ZipInfo( + filename=m3u_filename, date_time=now.timetuple()[:6] + ) + m3u_info.external_attr = S_IFREG | 0o600 + m3u_info.compress_type = ZIP_STORED + zip_file.writestr(m3u_info, m3u_encoded_content) # Get the completed ZIP file bytes zip_buffer.seek(0) @@ -402,19 +405,21 @@ async def get_rom_content( content_lines = [await create_zip_content(f, "/library-zip") for f in files] - m3u_encoded_content = "\n".join( - [f.file_name_for_download(rom, hidden_folder) for f in files] - ).encode() - m3u_base64_content = b64encode(m3u_encoded_content).decode() - m3u_line = ZipContentLine( - crc32=crc32_to_hex(binascii.crc32(m3u_encoded_content)), - size_bytes=len(m3u_encoded_content), - encoded_location=f"/decode?value={m3u_base64_content}", - filename=f"{file_name}.m3u", - ) + if not rom.has_m3u_file(): + m3u_encoded_content = "\n".join( + [f.file_name_for_download(rom, hidden_folder) for f in files] + ).encode() + m3u_base64_content = b64encode(m3u_encoded_content).decode() + m3u_line = ZipContentLine( + crc32=crc32_to_hex(binascii.crc32(m3u_encoded_content)), + size_bytes=len(m3u_encoded_content), + encoded_location=f"/decode?value={m3u_base64_content}", + filename=f"{file_name}.m3u", + ) + content_lines.append(m3u_line) return ZipResponse( - content_lines=content_lines + [m3u_line], + content_lines=content_lines, filename=f"{quote(file_name)}.zip", ) diff --git a/backend/models/rom.py b/backend/models/rom.py index ad5eb0f90..c6bc0430f 100644 --- a/backend/models/rom.py +++ b/backend/models/rom.py @@ -337,6 +337,13 @@ class Rom(BaseModel): def is_fully_identified(self) -> bool: return bool(self.igdb_id) and bool(self.moby_id) and bool(self.ss_id) + def has_m3u_file(self) -> bool: + """ + Check if the ROM has an M3U file associated with it. + This is used for multi-disc games. + """ + return any(file.file_extension.lower() == "m3u" for file in self.files) + def __repr__(self) -> str: return self.fs_name