Skip to content

Commit 8c40556

Browse files
authored
Merge pull request #429 from joebonrichie/draft-infer-extension
boulder/draft: Use infer to get extension type to perform extraction
2 parents bc5bda3 + 2067209 commit 8c40556

File tree

4 files changed

+69
-34
lines changed

4 files changed

+69
-34
lines changed

Cargo.lock

Lines changed: 26 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ xxhash-rust = { version = "0.8.11", features = ["xxh3"] }
8181
zstd = { version = "0.13.2", features = ["zstdmt"] }
8282
mailparse = "0.15.0"
8383
zbus = "5.1.1"
84+
infer = "0.19.0"
8485

8586
[workspace.lints.rust]
8687
rust_2018_idioms = { level = "warn", priority = -1 }

boulder/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ thread-priority.workspace = true
4949
tokio.workspace = true
5050
url.workspace = true
5151
mailparse.workspace = true
52+
infer.workspace = true
5253

5354
[lints]
5455
workspace = true

boulder/src/draft/upstream.rs

Lines changed: 41 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -81,37 +81,49 @@ async fn fetch(url: &Url, output: &Path) -> Result<String, Error> {
8181
}
8282

8383
async fn extract(archive: &Path, destination: &Path) -> Result<(), Error> {
84-
let extension = archive
85-
.extension()
86-
.map(|e| e.to_string_lossy().to_string())
87-
.unwrap_or_else(|| "tar".to_owned());
88-
89-
// If we can't specialise (.zip, etc) assume its a tar
90-
let result = match extension.as_str() {
91-
"zip" => {
92-
Command::new("unzip")
93-
.arg(archive)
94-
.arg("-d")
95-
.arg(destination)
96-
.output()
97-
.await?
84+
if let Some(kind) = infer::get_from_path(archive)? {
85+
println!("Detected type: {} ({})", kind.mime_type(), kind.extension());
86+
// If we can't specialise (.zip, etc) assume its a tar
87+
let result = match kind.extension() {
88+
"zip" => {
89+
Command::new("unzip")
90+
.arg(archive)
91+
.arg("-d")
92+
.arg(destination)
93+
.output()
94+
.await?
95+
}
96+
_ => {
97+
Command::new("tar")
98+
.arg("xf")
99+
.arg(archive)
100+
.arg("-C")
101+
.arg(destination)
102+
.output()
103+
.await?
104+
}
105+
};
106+
if result.status.success() {
107+
Ok(())
108+
} else {
109+
eprintln!("Command exited with: {}", String::from_utf8_lossy(&result.stderr));
110+
Err(Error::Extract(result.status))
98111
}
99-
_ => {
100-
Command::new("tar")
101-
.arg("xf")
102-
.arg(archive)
103-
.arg("-C")
104-
.arg(destination)
105-
.output()
106-
.await?
107-
}
108-
};
109-
110-
if result.status.success() {
111-
Ok(())
112112
} else {
113-
eprintln!("Command exited with: {}", String::from_utf8_lossy(&result.stderr));
114-
Err(Error::Extract(result.status))
113+
println!("Unknown file type, attempting tar extraction");
114+
let result = Command::new("tar")
115+
.arg("xf")
116+
.arg(archive)
117+
.arg("-C")
118+
.arg(destination)
119+
.output()
120+
.await?;
121+
if result.status.success() {
122+
Ok(())
123+
} else {
124+
eprintln!("Command exited with: {}", String::from_utf8_lossy(&result.stderr));
125+
Err(Error::Extract(result.status))
126+
}
115127
}
116128
}
117129

0 commit comments

Comments
 (0)