|
|
|
@ -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::<Mime>()
|
|
|
|
|
.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<DispositionParam> {
|
|
|
|
|