v
Some checks failed
smoke-mm / Install & smoke test mm --help (push) Has been cancelled

This commit is contained in:
2025-12-28 04:13:11 -08:00
parent 6acb52dfa0
commit edde074cc7
3 changed files with 186 additions and 29 deletions

View File

@@ -514,6 +514,7 @@ def _download_direct_file(
debug_logger: Optional[DebugLogger] = None,
quiet: bool = False,
suggested_filename: Optional[str] = None,
pipeline_progress: Optional[Any] = None,
) -> DownloadMediaResult:
"""Download a direct file (PDF, image, document, etc.) without yt-dlp."""
ensure_directory(output_dir)
@@ -685,7 +686,21 @@ def _download_direct_file(
raise DownloadError("Could not determine filename for URL (no Content-Disposition and no path filename)")
file_path = _unique_path(output_dir / filename)
progress_bar = ProgressBar()
# Prefer pipeline transfer bars when a Live UI is active.
use_pipeline_transfer = False
try:
if pipeline_progress is not None and hasattr(pipeline_progress, "update_transfer"):
ui = None
if hasattr(pipeline_progress, "ui_and_pipe_index"):
ui, _ = pipeline_progress.ui_and_pipe_index() # type: ignore[attr-defined]
use_pipeline_transfer = ui is not None
except Exception:
use_pipeline_transfer = False
progress_bar: Optional[ProgressBar] = None
if (not quiet) and (not use_pipeline_transfer):
progress_bar = ProgressBar()
if not quiet:
debug(f"Direct download: {filename}")
@@ -696,11 +711,41 @@ def _download_direct_file(
total_bytes = [0]
last_progress_time = [start_time]
rendered_once = [False]
transfer_started = [False]
def _maybe_begin_transfer(content_length: int) -> None:
if pipeline_progress is None:
return
if transfer_started[0]:
return
try:
total_val: Optional[int] = int(content_length) if isinstance(content_length, int) and content_length > 0 else None
except Exception:
total_val = None
try:
if hasattr(pipeline_progress, "begin_transfer"):
pipeline_progress.begin_transfer(label=str(filename or "download"), total=total_val)
transfer_started[0] = True
except Exception:
return
def progress_callback(bytes_downloaded: int, content_length: int) -> None:
downloaded_bytes[0] = bytes_downloaded
total_bytes[0] = content_length
# Update pipeline transfer bar when present.
try:
if pipeline_progress is not None and hasattr(pipeline_progress, "update_transfer"):
_maybe_begin_transfer(content_length)
total_val: Optional[int] = int(content_length) if isinstance(content_length, int) and content_length > 0 else None
pipeline_progress.update_transfer(
label=str(filename or "download"),
completed=int(bytes_downloaded) if bytes_downloaded is not None else None,
total=total_val,
)
except Exception:
pass
now = time.time()
is_final = bool(content_length > 0 and bytes_downloaded >= content_length)
if (not rendered_once[0]) or is_final:
@@ -721,12 +766,13 @@ def _download_direct_file(
except Exception:
eta_str = None
progress_bar.update(
downloaded=bytes_downloaded,
total=content_length if content_length > 0 else None,
label=str(filename or "download"),
file=sys.stderr,
)
if progress_bar is not None:
progress_bar.update(
downloaded=bytes_downloaded,
total=content_length if content_length > 0 else None,
label=str(filename or "download"),
file=sys.stderr,
)
rendered_once[0] = True
@@ -736,8 +782,26 @@ def _download_direct_file(
client.download(url, str(file_path), progress_callback=progress_callback)
elapsed = time.time() - start_time
progress_bar.finish()
avg_speed_str = progress_bar.format_bytes(downloaded_bytes[0] / elapsed if elapsed > 0 else 0) + "/s"
try:
if progress_bar is not None:
progress_bar.finish()
except Exception:
pass
try:
if pipeline_progress is not None and transfer_started[0] and hasattr(pipeline_progress, "finish_transfer"):
pipeline_progress.finish_transfer(label=str(filename or "download"))
except Exception:
pass
try:
if progress_bar is not None:
avg_speed_str = progress_bar.format_bytes(downloaded_bytes[0] / elapsed if elapsed > 0 else 0) + "/s"
else:
avg_speed_str = f"{(downloaded_bytes[0] / elapsed if elapsed > 0 else 0):.1f} B/s"
except Exception:
avg_speed_str = ""
if not quiet:
debug(f"✓ Downloaded in {elapsed:.1f}s at {avg_speed_str}")
@@ -797,7 +861,13 @@ def _download_direct_file(
except (httpx.HTTPError, httpx.RequestError) as exc:
try:
progress_bar.finish()
if progress_bar is not None:
progress_bar.finish()
except Exception:
pass
try:
if pipeline_progress is not None and transfer_started[0] and hasattr(pipeline_progress, "finish_transfer"):
pipeline_progress.finish_transfer(label=str(filename or "download"))
except Exception:
pass
log(f"Download error: {exc}", file=sys.stderr)
@@ -809,7 +879,13 @@ def _download_direct_file(
raise DownloadError(f"Failed to download {url}: {exc}") from exc
except Exception as exc:
try:
progress_bar.finish()
if progress_bar is not None:
progress_bar.finish()
except Exception:
pass
try:
if pipeline_progress is not None and transfer_started[0] and hasattr(pipeline_progress, "finish_transfer"):
pipeline_progress.finish_transfer(label=str(filename or "download"))
except Exception:
pass
log(f"Error downloading file: {exc}", file=sys.stderr)