diff --git a/app/library/DownloadQueue.py b/app/library/DownloadQueue.py index d9fc6b1e..3af15f2d 100644 --- a/app/library/DownloadQueue.py +++ b/app/library/DownloadQueue.py @@ -2,7 +2,6 @@ import asyncio import functools import glob import logging -import os import time import uuid from datetime import UTC, datetime, timedelta @@ -10,7 +9,6 @@ from email.utils import formatdate, parsedate_to_datetime from pathlib import Path from sqlite3 import Connection -import anyio import yt_dlp from aiohttp import web @@ -399,7 +397,7 @@ class DownloadQueue(metaclass=Singleton): item.template = _preset.template yt_conf = {} - cookie_file = os.path.join(self.config.temp_path, f"c_{uuid.uuid4().hex}.txt") + cookie_file = Path(self.config.temp_path) / f"c_{uuid.uuid4().hex}.txt" LOG.info(f"Adding '{item.__repr__()}'.") @@ -441,10 +439,8 @@ class DownloadQueue(metaclass=Singleton): if item.cookies: try: - async with await anyio.open_file(cookie_file, "w") as f: - await f.write(item.cookies) - yt_conf["cookiefile"] = f.name - + cookie_file.write_text(item.cookies) + yt_conf["cookiefile"] = str(cookie_file.as_posix()) load_cookies(cookie_file) except Exception as e: msg = f"Failed to create cookie file for '{item.url}'. '{e!s}'." @@ -493,10 +489,10 @@ class DownloadQueue(metaclass=Singleton): "msg": f"TimeoutError: {self.config.extract_info_timeout}s reached Unable to extract info.", } finally: - if cookie_file and os.path.exists(cookie_file): + if cookie_file and cookie_file.exists(): try: - os.remove(cookie_file) - del yt_conf["cookiefile"] + cookie_file.unlink(missing_ok=True) + yt_conf.pop("cookiefile", None) except Exception as e: LOG.error(f"Failed to remove cookie file '{yt_conf['cookiefile']}'. {e!s}") @@ -579,18 +575,19 @@ class DownloadQueue(metaclass=Singleton): filename = f"{item.info.folder}/{item.info.filename}" try: - realFile: str = calc_download_path( - base_path=self.config.download_path, - folder=filename, - create_path=False, + rf = Path( + calc_download_path( + base_path=self.config.download_path, + folder=filename, + create_path=False, + ) ) - rf = Path(realFile) - if rf.is_file() and rf.exists(): + if rf.stem and rf.is_file() and rf.exists(): for f in rf.parent.glob(f"{glob.escape(rf.stem)}.*"): if f.is_file() and f.exists() and not f.name.startswith("."): removed_files += 1 LOG.debug(f"Removing '{itemRef}' local file '{f.name}'.") - os.remove(f) + f.unlink(missing_ok=True) else: LOG.warning(f"Failed to remove '{itemRef}' local file '{filename}'. File not found.") except Exception as e: @@ -701,22 +698,13 @@ class DownloadQueue(metaclass=Singleton): """ filePath = calc_download_path(base_path=self.config.download_path, folder=entry.info.folder) - LOG.info( - f"Downloading 'id: {id}', 'Title: {entry.info.title}', 'URL: {entry.info.url}' to 'Folder: {filePath}'." - ) + LOG.info(f"Downloading 'id: {id}', 'Title: {entry.info.title}', 'URL: {entry.info.url}' To '{filePath}'.") try: self._active[entry.info._id] = entry await entry.start() if "finished" != entry.info.status: - if entry.tmpfilename and os.path.isfile(entry.tmpfilename): - try: - os.remove(entry.tmpfilename) - entry.tmpfilename = None - except Exception: - pass - entry.info.status = "error" finally: if entry.info._id in self._active: diff --git a/app/library/Utils.py b/app/library/Utils.py index 8c7e4a55..b4ca56dc 100644 --- a/app/library/Utils.py +++ b/app/library/Utils.py @@ -89,17 +89,17 @@ def calc_download_path(base_path: str, folder: str | None = None, create_path: b if folder.startswith("/"): folder = folder[1:] - realBasePath = os.path.realpath(base_path) - download_path = os.path.realpath(os.path.join(base_path, folder)) + realBasePath = pathlib.Path(base_path).absolute() + download_path = pathlib.Path(realBasePath / folder).absolute() - if not download_path.startswith(realBasePath): + if not str(download_path).startswith(str(realBasePath)): msg = f'Folder "{folder}" must resolve inside the base download folder "{realBasePath}".' raise Exception(msg) - if not os.path.isdir(download_path) and create_path: - os.makedirs(download_path, exist_ok=True) + if not download_path.is_dir() and create_path: + download_path.mkdir(parents=True, exist_ok=True) - return download_path + return str(download_path) def extract_info(