global hotkeys wired up
This commit is contained in:
+56
-24
@@ -4,7 +4,7 @@ use std::path::{Path, PathBuf};
|
||||
|
||||
use anyhow::{anyhow, bail, Context, Result};
|
||||
use futures_util::StreamExt;
|
||||
use serde::Serialize;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use sha2::{Digest, Sha256};
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
|
||||
@@ -30,6 +30,7 @@ pub fn check(app_dir: &Path) -> FfmpegStatus {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
enum ArchiveKind {
|
||||
SevenZ,
|
||||
Zip,
|
||||
@@ -37,26 +38,39 @@ enum ArchiveKind {
|
||||
|
||||
struct Source {
|
||||
name: &'static str,
|
||||
archive_url: &'static str,
|
||||
checksum_url: &'static str,
|
||||
release_api_url: &'static str,
|
||||
asset_name_suffix: &'static str,
|
||||
kind: ArchiveKind,
|
||||
}
|
||||
|
||||
const SOURCES: &[Source] = &[
|
||||
Source {
|
||||
name: "GyanD",
|
||||
archive_url: "https://github.com/GyanD/codexffmpeg/releases/download/8.1.1/ffmpeg-8.1.1-essentials_build.7z",
|
||||
checksum_url: "https://github.com/GyanD/codexffmpeg/releases/download/8.1.1/ffmpeg-8.1.1-essentials_build.7z.sha256",
|
||||
release_api_url: "https://api.github.com/repos/GyanD/codexffmpeg/releases/tags/8.1.1",
|
||||
asset_name_suffix: "full_build.7z",
|
||||
kind: ArchiveKind::SevenZ,
|
||||
},
|
||||
Source {
|
||||
name: "BtbN",
|
||||
archive_url: "https://github.com/BtbN/FFmpeg-Builds/releases/download/latest/ffmpeg-master-latest-win64-gpl.zip",
|
||||
checksum_url: "https://github.com/BtbN/FFmpeg-Builds/releases/download/latest/ffmpeg-master-latest-win64-gpl.zip.sha256",
|
||||
release_api_url: "https://api.github.com/repos/BtbN/FFmpeg-Builds/releases/tags/latest",
|
||||
asset_name_suffix: "win64-gpl.zip",
|
||||
kind: ArchiveKind::Zip,
|
||||
},
|
||||
];
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
struct GithubRelease {
|
||||
assets: Vec<GithubAsset>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
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<()>
|
||||
where
|
||||
F: FnMut(u64, Option<u64>) + Send,
|
||||
@@ -89,13 +103,14 @@ where
|
||||
.user_agent(concat!("ByteSize/", env!("CARGO_PKG_VERSION")))
|
||||
.build()?;
|
||||
|
||||
let expected = fetch_sha256(&client, source.checksum_url)
|
||||
.await
|
||||
.with_context(|| format!("fetching checksum from {}", source.checksum_url))?;
|
||||
let asset = pick_asset(&client, source).await?;
|
||||
|
||||
let archive = download_verified(&client, source.archive_url, &expected, on_progress)
|
||||
let expected = parse_sha256_digest(&asset.digest)
|
||||
.with_context(|| format!("parsing digest for {}", asset.name))?;
|
||||
|
||||
let archive = download_verified(&client, &asset.browser_download_url, &expected, on_progress)
|
||||
.await
|
||||
.with_context(|| format!("downloading archive from {}", source.archive_url))?;
|
||||
.with_context(|| format!("downloading {}", asset.browser_download_url))?;
|
||||
|
||||
match source.kind {
|
||||
ArchiveKind::SevenZ => extract_ffmpeg_from_7z(&archive, dest)?,
|
||||
@@ -104,21 +119,38 @@ where
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn fetch_sha256(client: &reqwest::Client, url: &str) -> Result<[u8; 32]> {
|
||||
let body = client
|
||||
.get(url)
|
||||
async fn pick_asset(client: &reqwest::Client, source: &Source) -> Result<GithubAsset> {
|
||||
let resp = client
|
||||
.get(source.release_api_url)
|
||||
.header("Accept", "application/vnd.github+json")
|
||||
.send()
|
||||
.await?
|
||||
.error_for_status()?
|
||||
.text()
|
||||
.await?;
|
||||
let hex_str = body
|
||||
.split_whitespace()
|
||||
.next()
|
||||
.ok_or_else(|| anyhow!("empty checksum file"))?;
|
||||
let bytes = hex::decode(hex_str).context("checksum is not valid hex")?;
|
||||
.error_for_status()?;
|
||||
let body = resp.text().await?;
|
||||
let release: GithubRelease =
|
||||
serde_json::from_str(&body).context("parsing release metadata")?;
|
||||
release
|
||||
.assets
|
||||
.into_iter()
|
||||
.find(|a| a.name.ends_with(source.asset_name_suffix))
|
||||
.ok_or_else(|| {
|
||||
anyhow!(
|
||||
"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!("checksum length is {} bytes, expected 32", bytes.len());
|
||||
bail!("digest length is {} bytes, expected 32", bytes.len());
|
||||
}
|
||||
let mut sha = [0u8; 32];
|
||||
sha.copy_from_slice(&bytes);
|
||||
|
||||
Reference in New Issue
Block a user