From 31a429861d8e29d557714f9c2edaa6ff905defc0 Mon Sep 17 00:00:00 2001 From: neri Date: Sun, 15 Aug 2021 23:23:03 +0200 Subject: [PATCH] always prefer inline display when not downloading --- src/download.rs | 31 ++++++++++++------------------- 1 file changed, 12 insertions(+), 19 deletions(-) diff --git a/src/download.rs b/src/download.rs index 0cb7690..040fddf 100644 --- a/src/download.rs +++ b/src/download.rs @@ -45,7 +45,7 @@ pub async fn download( path.push(&file_id); let download = delete_on_download || req.query_string().contains("dl"); - let (content_type, mut content_disposition) = get_content_types(&path, &file_name); + let content_type = get_content_type(&path); let is_text = file_kind == FileKind::Text.to_string() || content_type.type_() == mime::TEXT; let response = if is_text && !download { let content = fs::read_to_string(path).await.map_err(|file_err| { @@ -57,9 +57,14 @@ pub async fn download( let response = HttpResponse::Ok().content_type("text/html").body(view_html); Ok(response) } else { - if download { - content_disposition.disposition = DispositionType::Attachment; - } + let content_disposition = ContentDisposition { + disposition: if download { + DispositionType::Attachment + } else { + DispositionType::Inline + }, + parameters: get_disposition_params(&file_name), + }; let file = NamedFile::open(path) .map_err(|file_err| { log::error!("file could not be read {:?}", file_err); @@ -80,24 +85,12 @@ pub async fn download( response } -fn get_content_types(path: &Path, filename: &str) -> (Mime, ContentDisposition) { +fn get_content_type(path: &Path) -> Mime { let std_path = std::path::Path::new(path.as_os_str()); - let ct = tree_magic_mini::from_filepath(std_path) + tree_magic_mini::from_filepath(std_path) .unwrap_or("application/octet-stream") .parse::() - .expect("tree_magic_mini should not produce invalid mime"); - - let disposition = match ct.type_() { - mime::IMAGE | mime::TEXT | mime::AUDIO | mime::VIDEO => DispositionType::Inline, - _ => DispositionType::Attachment, - }; - - let cd = ContentDisposition { - disposition, - parameters: get_disposition_params(filename), - }; - - (ct, cd) + .expect("tree_magic_mini should not produce invalid mime") } fn get_disposition_params(filename: &str) -> Vec {