Skip to content

Commit d7163f9

Browse files
committed
use signature detection first on archives to cover off misnamed files with wrong extensions. Actual file extension is now a fallback.
1 parent 7cfcbd5 commit d7163f9

1 file changed

Lines changed: 16 additions & 1 deletion

File tree

app/services/archive.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,22 @@ def __init__(self, filepath: Path):
3232
self.archive = self._open_archive()
3333

3434
def _open_archive(self):
35-
"""Open the appropriate archive handler"""
35+
"""Open the appropriate archive handler with fallback for mislabeled extensions"""
36+
# 1. Try to detect the format based on file signatures first (most reliable)
37+
if zipfile.is_zipfile(self.filepath):
38+
if self.extension != ".cbz":
39+
logger.info(f"Mislabeled archive: {self.filepath.name} is ZIP but labeled as {self.extension}")
40+
self.extension = ".cbz"
41+
return zipfile.ZipFile(self.filepath)
42+
43+
if rarfile.is_rarfile(self.filepath):
44+
if self.extension != ".cbr":
45+
logger.info(f"Mislabeled archive: {self.filepath.name} is RAR but labeled as {self.extension}")
46+
self.extension = ".cbr"
47+
return rarfile.RarFile(self.filepath)
48+
49+
# 2. Fall back to extension-based opening if detection failed
50+
# This provides standard library error messages if the file is corrupted
3651
if self.extension == ".cbz":
3752
return zipfile.ZipFile(self.filepath)
3853
elif self.extension == ".cbr":

0 commit comments

Comments
 (0)