Skip to content

Commit

Permalink
Removed curl dependency with reqwest (#48)
Browse files Browse the repository at this point in the history
* Removed curl dependecy with reqwest

* Fixed imports

* Added dependency check for check_latest_version

* Added deprecated flog to function with curl dependency
  • Loading branch information
VirtualPirate authored Oct 12, 2024
1 parent de0efc0 commit ba459ef
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 8 deletions.
6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,14 @@ repository = "https://github.com/nathanbabcock/ffmpeg-sidecar"
readme = "README.md"
license = "MIT"

[features]
default = ["download_ffmpeg"]
download_ffmpeg = ["reqwest"]

[lib]
crate-type = ["lib"]

[dependencies]
anyhow = "1.0.79"
reqwest = { version = "0.12.8", optional = true, features = ["blocking"]}

44 changes: 36 additions & 8 deletions src/download.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{
fs::{create_dir_all, read_dir, remove_dir_all, remove_file, rename},
io::Read,
fs::{create_dir_all, read_dir, remove_dir_all, remove_file, rename, File},
io::{copy, Read},
path::{Path, PathBuf},
process::{Command, ExitStatus, Stdio},
};
Expand Down Expand Up @@ -54,6 +54,7 @@ pub fn ffmpeg_download_url() -> anyhow::Result<&'static str> {
///
/// If FFmpeg is already installed, the method exits early without downloading
/// anything.
#[cfg(feature = "download_ffmpeg")]
pub fn auto_download() -> anyhow::Result<()> {
if ffmpeg_is_installed() {
return Ok(());
Expand Down Expand Up @@ -111,6 +112,7 @@ pub fn parse_linux_version(version: &str) -> Option<String> {
}

/// Invoke cURL on the command line to download a file, returning it as a string.
#[deprecated]
pub fn curl(url: &str) -> anyhow::Result<String> {
let mut child = Command::new("curl")
.create_no_window()
Expand All @@ -127,6 +129,7 @@ pub fn curl(url: &str) -> anyhow::Result<String> {
}

/// Invoke cURL on the command line to download a file, writing to a file.
#[deprecated]
pub fn curl_to_file(url: &str, destination: &str) -> anyhow::Result<ExitStatus> {
Command::new("curl")
.create_no_window()
Expand All @@ -138,13 +141,26 @@ pub fn curl_to_file(url: &str, destination: &str) -> anyhow::Result<ExitStatus>

/// Makes an HTTP request to obtain the latest version available online,
/// automatically choosing the correct URL for the current platform.
#[cfg(feature = "download_ffmpeg")]
pub fn check_latest_version() -> anyhow::Result<String> {
// Mac M1 doesn't have a manifest URL, so match the version provided in `ffmpeg_download_url`
if cfg!(all(target_os = "macos", target_arch = "aarch64")) {
return Ok("7.0".to_string());
}

let string = curl(ffmpeg_manifest_url()?)?;
let manifest_url = ffmpeg_manifest_url()?;
let response = reqwest::blocking::get(manifest_url)
.context("Failed to make a request for the latest version")?;

if !response.status().is_success() {
anyhow::bail!(
"Failed to get the latest version, status: {}",
response.status()
);
}

let string = response.text().context("Failed to read response text")?;

if cfg!(target_os = "windows") {
Ok(string)
} else if cfg!(target_os = "macos") {
Expand All @@ -158,21 +174,33 @@ pub fn check_latest_version() -> anyhow::Result<String> {

/// Invoke `curl` to download an archive (ZIP on windows, TAR on linux and mac)
/// from the latest published release online.
#[cfg(feature = "download_ffmpeg")]
pub fn download_ffmpeg_package(url: &str, download_dir: &Path) -> anyhow::Result<PathBuf> {
let filename = Path::new(url)
.file_name()
.context("Failed to get filename")?;

let archive_path = download_dir.join(filename);

let archive_filename = archive_path.to_str().context("invalid download path")?;

let exit_status = curl_to_file(url, archive_filename)?;
let response =
reqwest::blocking::get(url).context("Failed to make a request to download ffmpeg")?;

if !exit_status.success() {
anyhow::bail!("Failed to download ffmpeg");
if !response.status().is_success() {
anyhow::bail!("Failed to download ffmpeg, status: {}", response.status());
}

let mut file =
File::create(&archive_path).context("Failed to create file for ffmpeg download")?;

copy(
&mut response
.bytes()
.context("Failed to read response bytes")?
.as_ref(),
&mut file,
)
.context("Failed to write ffmpeg download to file")?;

Ok(archive_path)
}

Expand Down

0 comments on commit ba459ef

Please sign in to comment.