Add mime type guessing from file ext for web ui
This commit is contained in:
parent
41d7feb4df
commit
4e2cfcfac6
3 changed files with 21 additions and 1 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -1030,6 +1030,7 @@ dependencies = [
|
||||||
"gloo-console",
|
"gloo-console",
|
||||||
"http",
|
"http",
|
||||||
"js-sys",
|
"js-sys",
|
||||||
|
"mime_guess",
|
||||||
"omegaupload-common 0.1.0",
|
"omegaupload-common 0.1.0",
|
||||||
"reqwasm",
|
"reqwasm",
|
||||||
"serde",
|
"serde",
|
||||||
|
|
|
@ -17,6 +17,7 @@ console_error_panic_hook = "0.1"
|
||||||
gloo-console = "0.1"
|
gloo-console = "0.1"
|
||||||
http = "0.2"
|
http = "0.2"
|
||||||
js-sys = "0.3"
|
js-sys = "0.3"
|
||||||
|
mime_guess = "2"
|
||||||
reqwasm = "0.2"
|
reqwasm = "0.2"
|
||||||
tree_magic_mini = { version = "3", features = ["with-gpl-data"] }
|
tree_magic_mini = { version = "3", features = ["with-gpl-data"] }
|
||||||
serde = { version = "1.0", features = ["derive"] }
|
serde = { version = "1.0", features = ["derive"] }
|
||||||
|
|
|
@ -55,10 +55,11 @@ pub fn decrypt(
|
||||||
mut container: Vec<u8>,
|
mut container: Vec<u8>,
|
||||||
key: &Secret<Key>,
|
key: &Secret<Key>,
|
||||||
maybe_password: Option<SecretVec<u8>>,
|
maybe_password: Option<SecretVec<u8>>,
|
||||||
|
name_hint: Option<&str>,
|
||||||
) -> Result<(DecryptedData, MimeType), Error> {
|
) -> Result<(DecryptedData, MimeType), Error> {
|
||||||
open_in_place(&mut container, key, maybe_password)?;
|
open_in_place(&mut container, key, maybe_password)?;
|
||||||
|
|
||||||
let mime_type = tree_magic_mini::from_u8(&container);
|
let mime_type = guess_mime_type(name_hint, &container);
|
||||||
log!("[rs] Mime type:", mime_type);
|
log!("[rs] Mime type:", mime_type);
|
||||||
|
|
||||||
log!("[rs] Blob conversion started.");
|
log!("[rs] Blob conversion started.");
|
||||||
|
@ -148,6 +149,23 @@ fn handle_gzip(blob: Arc<Blob>, container: Vec<u8>) -> DecryptedData {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn guess_mime_type(name_hint: Option<&str>, data: &[u8]) -> &'static str {
|
||||||
|
if let Some(name) = name_hint {
|
||||||
|
let guesses = mime_guess::from_path(name);
|
||||||
|
if let Some(mime_type) = guesses.first_raw() {
|
||||||
|
// Found at least one, but generally speaking this crate only
|
||||||
|
// uses authoritative sources (RFCs), so generally speaking
|
||||||
|
// there's only one association, and multiple are due to legacy
|
||||||
|
// support. As a result, we can probably just get the first one.
|
||||||
|
log!("[rs] Mime type inferred from extension.");
|
||||||
|
return mime_type;
|
||||||
|
} else {
|
||||||
|
log!("[rs] No mime type found for extension, falling back to introspection.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
tree_magic_mini::from_u8(&data)
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
|
||||||
enum ContentType {
|
enum ContentType {
|
||||||
Text,
|
Text,
|
||||||
|
|
Loading…
Reference in a new issue