tighten export and capture handling

This commit is contained in:
2026-06-04 23:09:13 +03:00
parent a48d82ddc9
commit 4ca802ef71
14 changed files with 736 additions and 456 deletions
+51 -27
View File
@@ -1,5 +1,5 @@
use std::fs::{self, File};
use std::io::{self, Cursor};
use std::io::{self, Write};
use std::path::{Path, PathBuf};
use anyhow::{anyhow, bail, Context, Result};
@@ -118,21 +118,39 @@ where
None
};
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))?
let app_dir = dest
.parent()
.and_then(|p| p.parent())
.context("invalid dest path for temp archive")?;
let scratch_dir = app_dir.join("temp");
fs::create_dir_all(&scratch_dir).context("creating temp dir for archive")?;
let archive_path = scratch_dir.join(format!(
"ffmpeg-archive-{}.tmp",
source.name.to_lowercase()
));
if let Some(ref hash) = expected {
download_verified(
&client,
&asset.browser_download_url,
hash,
&archive_path,
on_progress,
)
.await
.with_context(|| format!("downloading {}", asset.browser_download_url))?;
} else {
download_plain(&client, &asset.browser_download_url, on_progress)
download_plain(&client, &asset.browser_download_url, &archive_path, on_progress)
.await
.with_context(|| format!("downloading {}", asset.browser_download_url))?
.with_context(|| format!("downloading {}", asset.browser_download_url))?;
};
match source.kind {
ArchiveKind::SevenZ => extract_ffmpeg_from_7z(&archive, dest)?,
ArchiveKind::Zip => extract_ffmpeg_from_zip(&archive, dest)?,
}
Ok(())
let result = match source.kind {
ArchiveKind::SevenZ => extract_ffmpeg_from_7z(&archive_path, dest),
ArchiveKind::Zip => extract_ffmpeg_from_zip(&archive_path, dest),
};
let _ = fs::remove_file(&archive_path);
result
}
/// Fetch and parse a SHA-256 checksums file from GyanD.
@@ -174,23 +192,25 @@ async fn fetch_sha256(client: &reqwest::Client, url: &str) -> Result<[u8; 32]> {
async fn download_plain<F>(
client: &reqwest::Client,
url: &str,
dest: &Path,
mut on_progress: F,
) -> Result<Vec<u8>>
) -> Result<()>
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 file = std::fs::File::create(dest).context("creating archive temp file")?;
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);
file.write_all(&chunk)?;
downloaded += chunk.len() as u64;
on_progress(downloaded, total);
}
Ok(buf)
file.flush()?;
Ok(())
}
async fn pick_asset(client: &reqwest::Client, source: &Source) -> Result<(GithubAsset, String)> {
@@ -224,33 +244,36 @@ async fn download_verified<F>(
client: &reqwest::Client,
url: &str,
expected: &[u8; 32],
dest: &Path,
on_progress: &mut F,
) -> Result<Vec<u8>>
) -> Result<()>
where
F: FnMut(u64, Option<u64>),
{
let resp = client.get(url).send().await?.error_for_status()?;
let total = resp.content_length();
let mut hasher = Sha256::new();
let mut buf: Vec<u8> = Vec::with_capacity(total.unwrap_or(0) as usize);
let mut file = std::fs::File::create(dest).context("creating archive temp file")?;
let mut downloaded = 0u64;
let mut stream = resp.bytes_stream();
while let Some(chunk) = stream.next().await {
let chunk = chunk?;
hasher.update(&chunk);
buf.extend_from_slice(&chunk);
file.write_all(&chunk)?;
downloaded += chunk.len() as u64;
on_progress(downloaded, total);
}
file.flush()?;
let got: [u8; 32] = hasher.finalize().into();
if &got != expected {
let _ = std::fs::remove_file(dest);
bail!(
"sha256 mismatch: expected {} got {}",
hex::encode(expected),
hex::encode(got)
);
}
Ok(buf)
Ok(())
}
fn is_ffmpeg_entry(name: &str) -> bool {
@@ -265,7 +288,7 @@ fn is_ffmpeg_entry(name: &str) -> bool {
.unwrap_or(false)
}
fn extract_ffmpeg_from_7z(archive: &[u8], dest: &Path) -> Result<()> {
fn extract_ffmpeg_from_7z(archive_path: &Path, dest: &Path) -> Result<()> {
let app_dir = dest
.parent()
.and_then(|p| p.parent())
@@ -274,9 +297,10 @@ fn extract_ffmpeg_from_7z(archive: &[u8], dest: &Path) -> Result<()> {
let _ = fs::remove_dir_all(&scratch);
fs::create_dir_all(&scratch).context("create 7z scratch dir")?;
let reader = Cursor::new(archive);
let extract_result = sevenz_rust2::decompress_with_extract_fn(reader, &scratch, |_, _, _| Ok(true))
.map_err(|e| anyhow!("7z extraction failed: {e}"));
let file = std::fs::File::open(archive_path).context("opening 7z archive")?;
let extract_result =
sevenz_rust2::decompress_with_extract_fn(file, &scratch, |_, _, _| Ok(true))
.map_err(|e| anyhow!("7z extraction failed: {e}"));
let found = find_ffmpeg_in(&scratch);
let _ = fs::remove_dir_all(&scratch);
@@ -305,9 +329,9 @@ fn find_ffmpeg_in(dir: &Path) -> Option<PathBuf> {
None
}
fn extract_ffmpeg_from_zip(archive: &[u8], dest: &Path) -> Result<()> {
let reader = Cursor::new(archive);
let mut zip = zip::ZipArchive::new(reader).context("open zip archive")?;
fn extract_ffmpeg_from_zip(archive_path: &Path, dest: &Path) -> Result<()> {
let file = std::fs::File::open(archive_path).context("opening zip archive")?;
let mut zip = zip::ZipArchive::new(file).context("open zip archive")?;
for i in 0..zip.len() {
let mut entry = zip.by_index(i)?;
if entry.is_dir() {