feat(downloads): estimate a transcode's size so the progress bar moves

A transcode is produced as it is sent — chunked, with no Content-Length —
and the worker reported progress 0.0 for its whole duration: an empty bar
reading "0%" while the byte count climbed for an hour. That is the case
every film whose audio must be re-encoded lands in.

The backend already fetches the item to decide the audio policy, and that
item carries what a prediction needs: the source's size (an `original`
download copies the picture, so the output is the source give or take the
audio track) and its runtime (a preset re-encodes at fixed rates, so the
size is rate × runtime — from a preset table the URL builder now shares, so
the two cannot drift). The prediction is made where the URL is resolved and
persisted as the row's file_size. The worker uses it only when the response
has no length; the server's figure always wins; an estimated bar is capped
at 99% so a low prediction never shows a finished download still running;
and the Completed event now carries the bytes actually written so the
frontend stops persisting the row's file_size as the final size.

The row renders three honest states: exact "42%", estimated "~42%" with
"X / ~Y", or — with no total at all — an indeterminate band and the bytes
so far, never "0%". The single-video button joins the series/season buttons
on the enqueue path so all three resolve, and predict, in one place.

DR-290, UT-252, UT-253, UT-254.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-09-21 11:18:29 +02:00
co-authored by Claude Opus 5
parent e271874b1d
commit b3228cb4f4
15 changed files with 716 additions and 105 deletions
+99 -16
View File
@@ -517,6 +517,34 @@ pub(crate) async fn requeue_mistyped_video_downloads(
Ok(n)
}
/// What a resolver hands back for one queued row: the URL to fetch and, for a
/// video, the size predicted for it (see `download::estimate`).
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct ResolvedDownloadUrl {
pub url: String,
pub expected_bytes: Option<u64>,
}
impl From<String> for ResolvedDownloadUrl {
/// An audio stream URL: served static, so the response states its own
/// length and nothing needs predicting.
fn from(url: String) -> Self {
Self {
url,
expected_bytes: None,
}
}
}
impl From<crate::repository::ResolvedVideoDownload> for ResolvedDownloadUrl {
fn from(r: crate::repository::ResolvedVideoDownload) -> Self {
Self {
url: r.url,
expected_bytes: r.expected_bytes,
}
}
}
/// Core of [`resume_queued_downloads`], factored out for testing: select every
/// `pending`/`stream_url IS NULL` row, resolve each via `resolve` (returning
/// `None` leaves the row pending), and heal the row so the pump can start it.
@@ -534,7 +562,7 @@ pub(crate) async fn resolve_pending_download_urls<F, Fut>(
) -> Result<ResumeQueuedResult, String>
where
F: Fn(String, String, String) -> Fut,
Fut: std::future::Future<Output = Option<String>>,
Fut: std::future::Future<Output = Option<ResolvedDownloadUrl>>,
{
if only_ids.is_some_and(|ids| ids.is_empty()) {
return Ok(ResumeQueuedResult {
@@ -600,8 +628,8 @@ where
let mut failed = 0usize;
for (download_id, item_id, media_type, quality) in rows {
let stream_url = match resolve(item_id.clone(), media_type, quality).await {
Some(url) => url,
let target = match resolve(item_id.clone(), media_type, quality).await {
Some(target) => target,
None => {
failed += 1;
continue;
@@ -609,13 +637,21 @@ where
};
// Heal the row so the pump can start it. Guard on stream_url IS NULL so a
// concurrent resolver doesn't clobber an already-started row.
// concurrent resolver doesn't clobber an already-started row. The
// predicted size, when there is one, gives the worker a progress total
// for a response that carries none (DR-290).
let expected = target
.expected_bytes
.and_then(|n| i64::try_from(n).ok())
.map_or(QueryParam::Null, QueryParam::Int64);
let update = Query::with_params(
"UPDATE downloads SET stream_url = ?, target_dir = ?
"UPDATE downloads SET stream_url = ?, target_dir = ?,
file_size = COALESCE(?, file_size)
WHERE id = ? AND status = 'pending' AND stream_url IS NULL",
vec![
QueryParam::String(stream_url),
QueryParam::String(target.url),
QueryParam::String(target_dir.to_string()),
expected,
QueryParam::Int64(download_id),
],
);
@@ -705,17 +741,18 @@ pub async fn resume_queued_downloads(
async move {
if media_type == "video" {
Some(
crate::repository::resolve_video_download_url(
crate::repository::resolve_video_download(
repo.as_ref(),
&item_id,
&quality,
None,
)
.await,
.await
.into(),
)
} else {
match repo.get_audio_stream_url(&item_id).await {
Ok(url) => Some(url),
Ok(url) => Some(url.into()),
Err(e) => {
warn!(
"[Catalog] Failed to resolve audio URL for {}: {:?}",
@@ -807,6 +844,7 @@ mod tests {
target_dir TEXT,
media_type TEXT,
quality_preset TEXT,
file_size INTEGER,
progress REAL DEFAULT 0,
bytes_downloaded INTEGER DEFAULT 0,
started_at TEXT,
@@ -890,7 +928,7 @@ mod tests {
&db,
"/data/downloads",
None,
|item_id, _mt, _q| async move { Some(format!("http://resolved/{item_id}")) },
|item_id, _mt, _q| async move { Some(format!("http://resolved/{item_id}").into()) },
)
.await
.unwrap();
@@ -932,7 +970,7 @@ mod tests {
&db,
"/data",
Some(&[mine]),
|item_id, _mt, _q| async move { Some(format!("http://resolved/{item_id}")) },
|item_id, _mt, _q| async move { Some(format!("http://resolved/{item_id}").into()) },
)
.await
.unwrap();
@@ -962,7 +1000,7 @@ mod tests {
let out =
resolve_pending_download_urls(&db, "/data", Some(&[]), |item_id, _mt, _q| async move {
Some(format!("http://resolved/{item_id}"))
Some(format!("http://resolved/{item_id}").into())
})
.await
.unwrap();
@@ -1015,7 +1053,7 @@ mod tests {
let seen = Arc::clone(&seen_c);
async move {
seen.lock_safe().push((item_id.clone(), media_type));
Some(format!("http://resolved/{item_id}"))
Some(format!("http://resolved/{item_id}").into())
}
})
.await
@@ -1048,7 +1086,7 @@ mod tests {
let seen = Arc::clone(&seen_c);
async move {
*seen.lock_safe() = media_type;
Some("http://x".to_string())
Some("http://x".to_string().into())
}
})
.await
@@ -1072,7 +1110,7 @@ mod tests {
let seen = Arc::clone(&seen_c);
async move {
*seen.lock_safe() = media_type;
Some("http://x".to_string())
Some("http://x".to_string().into())
}
})
.await
@@ -1123,6 +1161,51 @@ mod tests {
assert_eq!(status, "completed", "a correct video download is untouched");
}
/// A transcode answers with no `Content-Length`, so the worker's only
/// chance at a progress total is the size predicted at resolve time. That
/// prediction has to reach the row, and only where there is one — an
/// audio row's `None` must not null out a size the row already holds.
///
/// TRACES: UR-071 | DR-290 | UT-253
#[tokio::test]
async fn resolving_persists_the_predicted_size_without_erasing_a_known_one() {
let db = test_db();
insert_download(&db, "film", "pending", None, Some("video")).await;
insert_download(&db, "track", "pending", None, Some("audio")).await;
db.execute(Query::with_params(
"UPDATE downloads SET file_size = 777 WHERE item_id = ?",
vec![QueryParam::String("track".to_string())],
))
.await
.unwrap();
resolve_pending_download_urls(&db, "/data", None, |item_id, media_type, _q| async move {
Some(ResolvedDownloadUrl {
url: format!("http://resolved/{item_id}"),
expected_bytes: (media_type == "video").then_some(1_500_000_000),
})
})
.await
.unwrap();
let size = |item: &'static str| {
let db = Arc::clone(&db);
async move {
db.query_one(
Query::with_params(
"SELECT file_size FROM downloads WHERE item_id = ?",
vec![QueryParam::String(item.to_string())],
),
|row| row.get::<_, Option<i64>>(0),
)
.await
.unwrap()
}
};
assert_eq!(size("film").await, Some(1_500_000_000));
assert_eq!(size("track").await, Some(777));
}
#[tokio::test]
async fn video_rows_use_media_type_in_resolver() {
let db = test_db();
@@ -1134,7 +1217,7 @@ mod tests {
None,
|item_id, media_type, _q| async move {
assert_eq!(media_type, "video");
Some(format!("http://transcode/{item_id}"))
Some(format!("http://transcode/{item_id}").into())
},
)
.await