|
|
|
@ -17,7 +17,7 @@ use time::OffsetDateTime;
|
|
|
|
|
use tokio::fs;
|
|
|
|
|
use url::Url;
|
|
|
|
|
|
|
|
|
|
use crate::{config::Config, deleter, mime_relations};
|
|
|
|
|
use crate::{config::Config, deleter, mime_relations, template};
|
|
|
|
|
|
|
|
|
|
const TEXT_VIEW_HTML: &str = include_str!("../template/text-view.html");
|
|
|
|
|
const URL_VIEW_HTML: &str = include_str!("../template/url-view.html");
|
|
|
|
@ -43,7 +43,7 @@ pub async fn download(
|
|
|
|
|
let mut response = match get_view_type(&req, &mime, &path, delete).await {
|
|
|
|
|
ViewType::Raw => build_file_response(false, &file_name, path, mime, &req),
|
|
|
|
|
ViewType::Download => build_file_response(true, &file_name, path, mime, &req),
|
|
|
|
|
ViewType::Html => build_html_response(&path).await,
|
|
|
|
|
ViewType::Html => build_html_response(&path, &config, &req).await,
|
|
|
|
|
}?;
|
|
|
|
|
|
|
|
|
|
insert_cache_headers(&mut response, valid_till);
|
|
|
|
@ -121,20 +121,26 @@ async fn get_file_size(file_path: &Path) -> u64 {
|
|
|
|
|
.unwrap_or(0)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn build_html_response(path: &Path) -> Result<HttpResponse, Error> {
|
|
|
|
|
async fn build_html_response(
|
|
|
|
|
path: &Path,
|
|
|
|
|
config: &Config,
|
|
|
|
|
req: &HttpRequest,
|
|
|
|
|
) -> Result<HttpResponse, Error> {
|
|
|
|
|
let content = fs::read_to_string(path).await.map_err(|file_err| {
|
|
|
|
|
log::error!("file could not be read {:?}", file_err);
|
|
|
|
|
error::ErrorInternalServerError("this file should be here but could not be found")
|
|
|
|
|
})?;
|
|
|
|
|
let encoded = htmlescape::encode_minimal(&content);
|
|
|
|
|
let html = if !content.trim().contains(['\n', '\r']) && Url::from_str(content.trim()).is_ok() {
|
|
|
|
|
let attribute_encoded = htmlescape::encode_attribute(&content);
|
|
|
|
|
URL_VIEW_HTML
|
|
|
|
|
.replace("{link_content}", &encoded)
|
|
|
|
|
.replace("{link_attribute}", &attribute_encoded)
|
|
|
|
|
} else {
|
|
|
|
|
TEXT_VIEW_HTML.replace("{text}", &encoded)
|
|
|
|
|
};
|
|
|
|
|
let mut html =
|
|
|
|
|
if !content.trim().contains(['\n', '\r']) && Url::from_str(content.trim()).is_ok() {
|
|
|
|
|
let attribute_encoded = htmlescape::encode_attribute(&content);
|
|
|
|
|
URL_VIEW_HTML
|
|
|
|
|
.replace("{link_content}", &encoded)
|
|
|
|
|
.replace("{link_attribute}", &attribute_encoded)
|
|
|
|
|
} else {
|
|
|
|
|
TEXT_VIEW_HTML.replace("{text}", &encoded)
|
|
|
|
|
};
|
|
|
|
|
html = template::insert_abuse_template(html, Some(req), config);
|
|
|
|
|
Ok(HttpResponse::Ok()
|
|
|
|
|
.content_type(TEXT_HTML.to_string())
|
|
|
|
|
.body(html))
|
|
|
|
|