current state before improvement pass

This commit is contained in:
2026-05-27 18:56:36 +03:00
parent 5eb27ab779
commit f0775b8ff0
23 changed files with 3563 additions and 1853 deletions
+91 -28
View File
@@ -46,7 +46,7 @@ struct Source {
const SOURCES: &[Source] = &[
Source {
name: "GyanD",
release_api_url: "https://api.github.com/repos/GyanD/codexffmpeg/releases/tags/8.1.1",
release_api_url: "https://api.github.com/repos/GyanD/codexffmpeg/releases/tags/latest",
asset_name_suffix: "full_build.7z",
kind: ArchiveKind::SevenZ,
},
@@ -60,6 +60,7 @@ const SOURCES: &[Source] = &[
#[derive(Debug, Deserialize)]
struct GithubRelease {
tag_name: String,
assets: Vec<GithubAsset>,
}
@@ -67,8 +68,6 @@ struct GithubRelease {
struct GithubAsset {
name: String,
browser_download_url: String,
#[serde(default)]
digest: Option<String>,
}
pub async fn download<F>(app_dir: &Path, mut on_progress: F) -> Result<()>
@@ -103,14 +102,31 @@ where
.user_agent(concat!("ByteSize/", env!("CARGO_PKG_VERSION")))
.build()?;
let asset = pick_asset(&client, source).await?;
let (asset, version) = pick_asset(&client, source).await?;
let expected = parse_sha256_digest(&asset.digest)
.with_context(|| format!("parsing digest for {}", asset.name))?;
// GyanD publishes a .sha256 file alongside each release.
// BtbN does not, so we skip verification for BtbN.
let expected = if source.name == "GyanD" {
let checksum_url = format!(
"https://github.com/GyanD/codexffmpeg/releases/download/{}/{}.sha256",
version,
asset.name
);
Some(fetch_sha256(&client, &checksum_url).await?)
} else {
tracing::warn!("BtbN build: skipping SHA-256 verification (no checksum file)");
None
};
let archive = download_verified(&client, &asset.browser_download_url, &expected, on_progress)
.await
.with_context(|| format!("downloading {}", asset.browser_download_url))?;
let archive = if let Some(ref hash) = expected {
download_verified(&client, &asset.browser_download_url, hash, on_progress)
.await
.with_context(|| format!("downloading {}", asset.browser_download_url))?
} else {
download_plain(&client, &asset.browser_download_url, on_progress)
.await
.with_context(|| format!("downloading {}", asset.browser_download_url))?
};
match source.kind {
ArchiveKind::SevenZ => extract_ffmpeg_from_7z(&archive, dest)?,
@@ -119,7 +135,65 @@ where
Ok(())
}
async fn pick_asset(client: &reqwest::Client, source: &Source) -> Result<GithubAsset> {
/// Fetch and parse a SHA-256 checksums file from GyanD.
/// The file contains a line like:
/// abc123def456... ffmpeg-8.1.1-full_build.7z
async fn fetch_sha256(client: &reqwest::Client, url: &str) -> Result<[u8; 32]> {
let resp = client.get(url).send().await?.error_for_status()?;
let text = resp.text().await?;
// Each line is: "<hex> <filename>" - find the line with our filename
let hash_str = text
.lines()
.find_map(|line| {
let line = line.trim();
if line.is_empty() || line.starts_with('#') {
return None;
}
// SHA-256 hex is 64 chars, followed by spaces and the filename
if line.len() < 64 {
return None;
}
let (hash, _file) = line.split_at(64);
let remainder = _file.trim_start();
// Must be followed by two spaces and a filename
if !remainder.starts_with(" ") {
return None;
}
Some(hash)
})
.ok_or_else(|| anyhow!("SHA-256 for this build not found in checksums file"))?;
let bytes = hex::decode(hash_str).context("checksum is not valid hex")?;
if bytes.len() != 32 {
bail!("checksum length is {} bytes, expected 32", bytes.len());
}
let mut sha = [0u8; 32];
sha.copy_from_slice(&bytes);
Ok(sha)
}
async fn download_plain<F>(
client: &reqwest::Client,
url: &str,
mut on_progress: F,
) -> Result<Vec<u8>>
where
F: FnMut(u64, Option<u64>),
{
let resp = client.get(url).send().await?.error_for_status()?;
let total = resp.content_length();
let mut buf: Vec<u8> = Vec::with_capacity(total.unwrap_or(0) as usize);
let mut downloaded = 0u64;
let mut stream = resp.bytes_stream();
while let Some(chunk) = stream.next().await {
let chunk = chunk?;
buf.extend_from_slice(&chunk);
downloaded += chunk.len() as u64;
on_progress(downloaded, total);
}
Ok(buf)
}
async fn pick_asset(client: &reqwest::Client, source: &Source) -> Result<(GithubAsset, String)> {
let resp = client
.get(source.release_api_url)
.header("Accept", "application/vnd.github+json")
@@ -129,7 +203,11 @@ async fn pick_asset(client: &reqwest::Client, source: &Source) -> Result<GithubA
let body = resp.text().await?;
let release: GithubRelease =
serde_json::from_str(&body).context("parsing release metadata")?;
release
let version = release
.tag_name
.trim_start_matches('v')
.to_string();
let asset = release
.assets
.into_iter()
.find(|a| a.name.ends_with(source.asset_name_suffix))
@@ -138,23 +216,8 @@ async fn pick_asset(client: &reqwest::Client, source: &Source) -> Result<GithubA
"no asset matching '*{}' in release",
source.asset_name_suffix
)
})
}
fn parse_sha256_digest(digest: &Option<String>) -> Result<[u8; 32]> {
let raw = digest
.as_deref()
.ok_or_else(|| anyhow!("release asset has no digest"))?;
let hex_str = raw
.strip_prefix("sha256:")
.ok_or_else(|| anyhow!("digest is not sha256: '{raw}'"))?;
let bytes = hex::decode(hex_str).context("digest is not valid hex")?;
if bytes.len() != 32 {
bail!("digest length is {} bytes, expected 32", bytes.len());
}
let mut sha = [0u8; 32];
sha.copy_from_slice(&bytes);
Ok(sha)
})?;
Ok((asset, version))
}
async fn download_verified<F>(