Skip to content

Commit

Permalink
Merge pull request #429 from joebonrichie/draft-infer-extension
Browse files Browse the repository at this point in the history
boulder/draft: Use infer to get extension type to perform extraction
  • Loading branch information
ikeycode authored Feb 22, 2025
2 parents bc5bda3 + 2067209 commit 8c40556
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 34 deletions.
31 changes: 26 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ xxhash-rust = { version = "0.8.11", features = ["xxh3"] }
zstd = { version = "0.13.2", features = ["zstdmt"] }
mailparse = "0.15.0"
zbus = "5.1.1"
infer = "0.19.0"

[workspace.lints.rust]
rust_2018_idioms = { level = "warn", priority = -1 }
Expand Down
1 change: 1 addition & 0 deletions boulder/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ thread-priority.workspace = true
tokio.workspace = true
url.workspace = true
mailparse.workspace = true
infer.workspace = true

[lints]
workspace = true
70 changes: 41 additions & 29 deletions boulder/src/draft/upstream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,37 +81,49 @@ async fn fetch(url: &Url, output: &Path) -> Result<String, Error> {
}

async fn extract(archive: &Path, destination: &Path) -> Result<(), Error> {
let extension = archive
.extension()
.map(|e| e.to_string_lossy().to_string())
.unwrap_or_else(|| "tar".to_owned());

// If we can't specialise (.zip, etc) assume its a tar
let result = match extension.as_str() {
"zip" => {
Command::new("unzip")
.arg(archive)
.arg("-d")
.arg(destination)
.output()
.await?
if let Some(kind) = infer::get_from_path(archive)? {
println!("Detected type: {} ({})", kind.mime_type(), kind.extension());
// If we can't specialise (.zip, etc) assume its a tar
let result = match kind.extension() {
"zip" => {
Command::new("unzip")
.arg(archive)
.arg("-d")
.arg(destination)
.output()
.await?
}
_ => {
Command::new("tar")
.arg("xf")
.arg(archive)
.arg("-C")
.arg(destination)
.output()
.await?
}
};
if result.status.success() {
Ok(())
} else {
eprintln!("Command exited with: {}", String::from_utf8_lossy(&result.stderr));
Err(Error::Extract(result.status))
}
_ => {
Command::new("tar")
.arg("xf")
.arg(archive)
.arg("-C")
.arg(destination)
.output()
.await?
}
};

if result.status.success() {
Ok(())
} else {
eprintln!("Command exited with: {}", String::from_utf8_lossy(&result.stderr));
Err(Error::Extract(result.status))
println!("Unknown file type, attempting tar extraction");
let result = Command::new("tar")
.arg("xf")
.arg(archive)
.arg("-C")
.arg(destination)
.output()
.await?;
if result.status.success() {
Ok(())
} else {
eprintln!("Command exited with: {}", String::from_utf8_lossy(&result.stderr));
Err(Error::Extract(result.status))
}
}
}

Expand Down

0 comments on commit 8c40556

Please sign in to comment.