Add mime type guessing from file ext for web ui

master
Edward Shen 2022-01-16 00:49:07 -08:00
parent 41d7feb4df
commit 4e2cfcfac6
Signed by: edward
GPG Key ID: 19182661E818369F
3 changed files with 21 additions and 1 deletions

1
Cargo.lock generated
View File

@ -1030,6 +1030,7 @@ dependencies = [
"gloo-console",
"http",
"js-sys",
"mime_guess",
"omegaupload-common 0.1.0",
"reqwasm",
"serde",

View File

@ -17,6 +17,7 @@ console_error_panic_hook = "0.1"
gloo-console = "0.1"
http = "0.2"
js-sys = "0.3"
mime_guess = "2"
reqwasm = "0.2"
tree_magic_mini = { version = "3", features = ["with-gpl-data"] }
serde = { version = "1.0", features = ["derive"] }

View File

@ -55,10 +55,11 @@ pub fn decrypt(
mut container: Vec<u8>,
key: &Secret<Key>,
maybe_password: Option<SecretVec<u8>>,
name_hint: Option<&str>,
) -> Result<(DecryptedData, MimeType), Error> {
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] 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)]
enum ContentType {
Text,