feat: add url validation before trying to download

This commit is contained in:
2025-12-14 09:32:29 +01:00
parent 46c66e0d5c
commit 6824d00088

View File

@@ -3,6 +3,7 @@
import re import re
from pathlib import Path from pathlib import Path
from typing import Callable from typing import Callable
from urllib.parse import urlparse
import audible import audible
import httpx import httpx
@@ -44,6 +45,11 @@ class DownloadManager:
notify("Failed to get download link") notify("Failed to get download link")
return None return None
if not self._validate_download_url(dl_link):
if notify:
notify("Invalid download URL")
return None
if not self._download_file(dl_link, local_path, notify): if not self._download_file(dl_link, local_path, notify):
if notify: if notify:
notify("Download failed") notify("Download failed")
@@ -66,6 +72,14 @@ class DownloadManager:
except Exception: except Exception:
return None return None
def _validate_download_url(self, url: str) -> bool:
"""Validate that the URL is a valid HTTP/HTTPS URL."""
try:
parsed = urlparse(url)
return parsed.scheme in ("http", "https") and bool(parsed.netloc)
except Exception:
return False
def _sanitize_filename(self, filename: str) -> str: def _sanitize_filename(self, filename: str) -> str:
"""Remove invalid characters from filename.""" """Remove invalid characters from filename."""
return re.sub(r'[<>:"/\\|?*]', "_", filename) return re.sub(r'[<>:"/\\|?*]', "_", filename)
@@ -135,4 +149,3 @@ class DownloadManager:
return dest_path return dest_path
except Exception: except Exception:
return None return None