reformatting
formatting_check / formatting_check (push) Failing after 3s
Python Test / test (.ps1, windows-latest, 3.9) (push) Has been cancelled
Python Test / test (.sh, macos-latest, 3.9) (push) Has been cancelled
Python Test / test (.sh, ubuntu-latest, 3.9) (push) Has been cancelled

This commit is contained in:
2026-07-14 21:32:55 +12:00
parent 3d0658b483
commit 0157f462cc
17 changed files with 439 additions and 377 deletions
+22 -20
View File
@@ -24,11 +24,11 @@ class Resource:
self.content: Optional[bytes] = None
self.url = url
self.hash: Optional[_hashlib.HASH] = None
# Log the original extension before normalization
if extension:
logger.debug(f"Resource constructor received extension: '{extension}' for URL: {url}")
self.extension = self._normalize_extension(extension)
self.download_function = download_function
if not self.extension:
@@ -72,14 +72,14 @@ class Resource:
if self.url.startswith("https://www.reddit.com/media"):
logger.debug(f"Detected Reddit media URL: {self.url}")
parsed_url = urllib.parse.urlparse(self.url)
url_param = urllib.parse.parse_qs(parsed_url.query).get('url', [None])[0]
url_param = urllib.parse.parse_qs(parsed_url.query).get("url", [None])[0]
if url_param:
decoded_url = urllib.parse.unquote(url_param)
logger.debug(f"Reddit media URL decoded to: {decoded_url}")
stripped_url = urllib.parse.urlsplit(decoded_url).path
# Also handle preview.redd.it URLs which might not have extensions
elif "preview.redd.it" in self.url and not stripped_url.endswith(('.jpg', '.jpeg', '.png', '.gif', '.webp')):
elif "preview.redd.it" in self.url and not stripped_url.endswith((".jpg", ".jpeg", ".png", ".gif", ".webp")):
logger.debug(f"Detected preview.redd.it URL without extension: {self.url}")
# For preview URLs, try to infer from common patterns or add fallback logic
@@ -92,7 +92,7 @@ class Resource:
logger.warning(f"Could not determine extension for URL: {self.url} (path: {stripped_url})")
# As a last resort, if we have content, try to detect by magic numbers
if hasattr(self, 'content') and self.content:
if hasattr(self, "content") and self.content:
detected = self._detect_extension_by_content()
return self._normalize_extension(detected) if detected else None
@@ -104,21 +104,21 @@ class Resource:
return None
# Check for common image formats
if self.content.startswith(b'\xFF\xD8\xFF'):
if self.content.startswith(b"\xff\xd8\xff"):
logger.debug(f"Detected JPEG by magic number for URL: {self.url}")
return '.jpg'
elif self.content.startswith(b'\x89PNG\r\n\x1a\n'):
return ".jpg"
elif self.content.startswith(b"\x89PNG\r\n\x1a\n"):
logger.debug(f"Detected PNG by magic number for URL: {self.url}")
return '.png'
elif self.content.startswith(b'GIF87a') or self.content.startswith(b'GIF89a'):
return ".png"
elif self.content.startswith(b"GIF87a") or self.content.startswith(b"GIF89a"):
logger.debug(f"Detected GIF by magic number for URL: {self.url}")
return '.gif'
elif self.content.startswith(b'RIFF') and self.content[8:12] == b'WEBP':
return ".gif"
elif self.content.startswith(b"RIFF") and self.content[8:12] == b"WEBP":
logger.debug(f"Detected WebP by magic number for URL: {self.url}")
return '.webp'
elif self.content.startswith(b'BM'):
return ".webp"
elif self.content.startswith(b"BM"):
logger.debug(f"Detected BMP by magic number for URL: {self.url}")
return '.bmp'
return ".bmp"
logger.debug(f"Could not detect file type by magic number for URL: {self.url}")
return None
@@ -127,15 +127,17 @@ class Resource:
"""Normalize extension to lowercase for consistency"""
if not extension:
return None
original = extension
# Ensure extension starts with a dot
if not extension.startswith('.'):
extension = '.' + extension
if not extension.startswith("."):
extension = "." + extension
normalized = extension.lower()
if original != normalized:
logger.info(f"Extension normalization: '{original}' -> '{normalized}' for URL: {self.url if hasattr(self, 'url') else 'unknown'}")
logger.info(
f"Extension normalization: '{original}' -> '{normalized}' for URL: {self.url if hasattr(self, 'url') else 'unknown'}"
)
return normalized
@staticmethod