2021-04-04 12:30:31 +00:00
|
|
|
use actix_files::NamedFile;
|
|
|
|
use actix_web::{
|
|
|
|
error,
|
|
|
|
http::header::{Charset, ContentDisposition, DispositionParam, DispositionType, ExtendedValue},
|
|
|
|
web, Error, HttpRequest, HttpResponse,
|
|
|
|
};
|
|
|
|
use async_std::{fs, path::Path};
|
|
|
|
use futures::TryStreamExt;
|
|
|
|
use mime::Mime;
|
|
|
|
use sqlx::{
|
|
|
|
postgres::{PgPool, PgRow},
|
|
|
|
Row,
|
|
|
|
};
|
|
|
|
|
|
|
|
use crate::deleter;
|
2021-04-04 12:33:37 +00:00
|
|
|
use crate::{config::Config, file_kind::FileKind};
|
2021-04-04 12:30:31 +00:00
|
|
|
|
|
|
|
const VIEW_HTML: &str = include_str!("../template/view.html");
|
|
|
|
|
|
|
|
pub async fn download(
|
|
|
|
req: HttpRequest,
|
|
|
|
db: web::Data<PgPool>,
|
|
|
|
config: web::Data<Config>,
|
|
|
|
) -> Result<HttpResponse, Error> {
|
|
|
|
let id = req.match_info().query("id");
|
2021-04-04 12:33:37 +00:00
|
|
|
let mut rows = sqlx::query(
|
|
|
|
"SELECT file_id, file_name, kind, delete_on_download from files WHERE file_id = $1",
|
|
|
|
)
|
|
|
|
.bind(id)
|
|
|
|
.fetch(db.as_ref());
|
2021-04-04 12:30:31 +00:00
|
|
|
let row: PgRow = rows
|
|
|
|
.try_next()
|
|
|
|
.await
|
2021-04-07 22:33:22 +00:00
|
|
|
.map_err(|db_err| {
|
|
|
|
log::error!("could not run select statement {:?}", db_err);
|
|
|
|
error::ErrorInternalServerError("could not run select statement")
|
|
|
|
})?
|
2021-04-04 12:30:31 +00:00
|
|
|
.ok_or_else(|| error::ErrorNotFound("file does not exist or has expired"))?;
|
|
|
|
|
|
|
|
let file_id: String = row.get("file_id");
|
|
|
|
let file_name: String = row.get("file_name");
|
2021-04-04 12:33:37 +00:00
|
|
|
let file_kind: String = row.get("kind");
|
2021-04-04 12:30:31 +00:00
|
|
|
let delete_on_download: bool = row.get("delete_on_download");
|
|
|
|
let mut path = config.files_dir.clone();
|
|
|
|
path.push(&file_id);
|
|
|
|
|
2021-04-04 12:36:38 +00:00
|
|
|
let download = delete_on_download || req.query_string().contains("dl");
|
2021-08-15 21:23:03 +00:00
|
|
|
let content_type = get_content_type(&path);
|
2021-04-04 12:33:37 +00:00
|
|
|
let is_text = file_kind == FileKind::Text.to_string() || content_type.type_() == mime::TEXT;
|
|
|
|
let response = if is_text && !download {
|
2021-04-07 22:33:22 +00:00
|
|
|
let content = fs::read_to_string(path).await.map_err(|file_err| {
|
|
|
|
log::error!("file could not be read {:?}", file_err);
|
2021-04-04 12:30:31 +00:00
|
|
|
error::ErrorInternalServerError("this file should be here but could not be found")
|
|
|
|
})?;
|
|
|
|
let encoded = htmlescape::encode_minimal(&content);
|
|
|
|
let view_html = VIEW_HTML.replace("{text}", &encoded);
|
|
|
|
let response = HttpResponse::Ok().content_type("text/html").body(view_html);
|
|
|
|
Ok(response)
|
|
|
|
} else {
|
2021-08-15 21:23:03 +00:00
|
|
|
let content_disposition = ContentDisposition {
|
|
|
|
disposition: if download {
|
|
|
|
DispositionType::Attachment
|
|
|
|
} else {
|
|
|
|
DispositionType::Inline
|
|
|
|
},
|
|
|
|
parameters: get_disposition_params(&file_name),
|
|
|
|
};
|
2021-04-04 12:30:31 +00:00
|
|
|
let file = NamedFile::open(path)
|
2021-04-07 22:33:22 +00:00
|
|
|
.map_err(|file_err| {
|
|
|
|
log::error!("file could not be read {:?}", file_err);
|
2021-04-04 12:30:31 +00:00
|
|
|
error::ErrorInternalServerError("this file should be here but could not be found")
|
|
|
|
})?
|
|
|
|
.set_content_type(content_type)
|
|
|
|
.set_content_disposition(content_disposition);
|
|
|
|
file.into_response(&req)
|
|
|
|
};
|
|
|
|
if delete_on_download {
|
|
|
|
deleter::delete_by_id(&db, &file_id, &config.files_dir)
|
|
|
|
.await
|
2021-04-07 22:33:22 +00:00
|
|
|
.map_err(|db_err| {
|
|
|
|
log::error!("could not delete file {:?}", db_err);
|
|
|
|
error::ErrorInternalServerError("could not delete file")
|
|
|
|
})?;
|
2021-04-04 12:30:31 +00:00
|
|
|
}
|
|
|
|
response
|
|
|
|
}
|
|
|
|
|
2021-08-15 21:23:03 +00:00
|
|
|
fn get_content_type(path: &Path) -> Mime {
|
2021-04-04 12:30:31 +00:00
|
|
|
let std_path = std::path::Path::new(path.as_os_str());
|
2021-08-15 21:23:03 +00:00
|
|
|
tree_magic_mini::from_filepath(std_path)
|
2021-04-04 12:30:31 +00:00
|
|
|
.unwrap_or("application/octet-stream")
|
|
|
|
.parse::<Mime>()
|
2021-08-15 21:23:03 +00:00
|
|
|
.expect("tree_magic_mini should not produce invalid mime")
|
2021-04-04 12:30:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn get_disposition_params(filename: &str) -> Vec<DispositionParam> {
|
|
|
|
let mut parameters = vec![DispositionParam::Filename(filename.to_owned())];
|
|
|
|
if !filename.is_ascii() {
|
|
|
|
parameters.push(DispositionParam::FilenameExt(ExtendedValue {
|
|
|
|
charset: Charset::Ext(String::from("UTF-8")),
|
|
|
|
language_tag: None,
|
|
|
|
value: filename.to_owned().into_bytes(),
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
parameters
|
|
|
|
}
|