diff --git a/cyberdrop_dl/downloader/downloader.py b/cyberdrop_dl/downloader/downloader.py index ff8d21fa9..43692d3f1 100644 --- a/cyberdrop_dl/downloader/downloader.py +++ b/cyberdrop_dl/downloader/downloader.py @@ -32,16 +32,6 @@ async def wrapper(self, *args, **kwargs): while True: try: return await f(self, *args, **kwargs) - except InvalidContentTypeFailure as e: - media_item = args[0] - if not isinstance(media_item.download_task_id, Field): - await self.manager.progress_manager.file_progress.remove_file(media_item.download_task_id) - await log(f"Download Failed: {media_item.url} received Invalid Content", 40) - await log(e.message, 40) - await self.manager.progress_manager.download_stats_progress.add_failure("Invalid Content Type") - await self.manager.progress_manager.download_progress.add_failed() - break - except DownloadFailure as e: media_item = args[0] if not isinstance(media_item.download_task_id, Field): @@ -75,9 +65,21 @@ async def wrapper(self, *args, **kwargs): else: await log(f"Download Failed: {media_item.url} with error {e}", 40) await log(f"Download Retrying: {media_item.url} with attempt {media_item.current_attempt}", 20) - + + except InvalidContentTypeFailure as e: + media_item = args[0] + await self._file_lock.release_lock(media_item.file_lock_reference_name) + if not isinstance(media_item.download_task_id, Field): + await self.manager.progress_manager.file_progress.remove_file(media_item.download_task_id) + await log(f"Download Failed: {media_item.url} received Invalid Content", 40) + await log(e.message, 40) + await self.manager.progress_manager.download_stats_progress.add_failure("Invalid Content Type") + await self.manager.progress_manager.download_progress.add_failed() + break + except Exception as e: media_item = args[0] + await self._file_lock.release_lock(media_item.file_lock_reference_name) await log(f"Download Failed: {media_item.url} with error {e}", 40) if not isinstance(media_item.download_task_id, Field): await self.manager.progress_manager.file_progress.remove_file(media_item.download_task_id) @@ -309,10 +311,11 @@ async def download(self, media_item: MediaItem) -> None: partial_file = None complete_file = None - FL_Filename = media_item.filename + if isinstance(media_item.file_lock_reference_name, Field): + media_item.file_lock_reference_name = media_item.filename try: - await self._file_lock.check_lock(FL_Filename) + await self._file_lock.check_lock(media_item.file_lock_reference_name) if not isinstance(media_item.current_attempt, int): media_item.current_attempt = 1 @@ -331,7 +334,7 @@ async def download(self, media_item: MediaItem) -> None: await log(f"Skipping {media_item.url} as it has already been downloaded", 10) await self.manager.progress_manager.download_progress.add_previously_completed(False) await self.mark_completed(media_item) - await self._file_lock.release_lock(FL_Filename) + await self._file_lock.release_lock(media_item.file_lock_reference_name) return resume_point = partial_file.stat().st_size if partial_file.exists() else 0 @@ -354,11 +357,11 @@ async def download(self, media_item: MediaItem) -> None: await self.mark_completed(media_item) await self.manager.progress_manager.file_progress.mark_task_completed(media_item.download_task_id) await self.manager.progress_manager.download_progress.add_completed() - await self._file_lock.release_lock(FL_Filename) + await self._file_lock.release_lock(media_item.file_lock_reference_name) return except (aiohttp.ServerDisconnectedError, asyncio.TimeoutError, aiohttp.ServerTimeoutError) as e: - await self._file_lock.release_lock(FL_Filename) + await self._file_lock.release_lock(media_item.file_lock_reference_name) if partial_file and partial_file.is_file(): size = partial_file.stat().st_size @@ -372,7 +375,7 @@ async def download(self, media_item: MediaItem) -> None: except (aiohttp.ClientPayloadError, aiohttp.ClientOSError, aiohttp.ClientResponseError, ConnectionResetError, DownloadFailure, FileNotFoundError, PermissionError) as e: - await self._file_lock.release_lock(FL_Filename) + await self._file_lock.release_lock(media_item.file_lock_reference_name) if hasattr(e, "status"): if ((await is_4xx_client_error(e.status) and e.status != HTTPStatus.TOO_MANY_REQUESTS) diff --git a/cyberdrop_dl/utils/dataclasses/url_objects.py b/cyberdrop_dl/utils/dataclasses/url_objects.py index 99cde3223..53c48d1de 100644 --- a/cyberdrop_dl/utils/dataclasses/url_objects.py +++ b/cyberdrop_dl/utils/dataclasses/url_objects.py @@ -18,6 +18,7 @@ def __init__(self, url: "URL", referer: "URL", download_folder: Path, filename: self.ext: str = ext self.download_filename: str = field(init=False) self.original_filename: str = original_filename + self.file_lock_reference_name: str = field(init=False) self.datetime: str = field(init=False) self.filesize: int = field(init=False) self.current_attempt: int = field(init=False)