Compare commits

..

4 Commits

Author SHA1 Message Date
neri 9f37657fa7 version 1.2.0 2022-10-07 15:52:26 +02:00
neri 171bfc98a9 use inline string formatting 2022-10-07 15:52:12 +02:00
neri d7b6d31198 determine corrext file extension for text uploads 2022-10-07 15:51:38 +02:00
neri f6628e63da add button to view text content as raw file 2022-10-07 14:59:57 +02:00
11 changed files with 2199 additions and 47 deletions

View File

@ -1,6 +1,6 @@
[package]
name = "datatrash"
version = "1.1.9"
version = "1.2.0"
authors = ["neri"]
edition = "2021"

2125
mime.types Normal file

File diff suppressed because it is too large Load Diff

View File

@ -30,18 +30,15 @@ fn get_db_url() -> String {
let auth = if let Ok(user) = env::var("DATABASE_USER") {
if let Ok(pass) = env::var("DATABASE_PASS") {
format!("{}:{}@", user, pass)
format!("{user}:{pass}@")
} else {
format!("{}@", user)
format!("{user}@")
}
} else {
String::new()
};
format!(
"postgresql://{auth}{host}/{name}",
auth = auth,
host = env::var("DATABASE_HOST").unwrap_or_else(|_| "localhost".to_string()),
name = env::var("DATABASE_NAME").unwrap_or_else(|_| "datatrash".to_string())
)
let host = env::var("DATABASE_HOST").unwrap_or_else(|_| "localhost".to_string());
let name = env::var("DATABASE_NAME").unwrap_or_else(|_| "datatrash".to_string());
format!("postgresql://{auth}{host}/{name}")
}

View File

@ -41,8 +41,7 @@ pub async fn download(
path.push(&file_id);
let mime = Mime::from_str(&content_type).unwrap_or(APPLICATION_OCTET_STREAM);
let mime = mime_relations::get_alias(&mime);
let mut response = match get_view_type(&req, mime, &path, delete).await {
let mut response = match get_view_type(&req, &mime, &path, delete).await {
ViewType::Raw => build_file_response(false, &file_name, path, mime, &req).await,
ViewType::Download => build_file_response(true, &file_name, path, mime, &req).await,
ViewType::Html => build_text_response(&path).await,
@ -146,7 +145,7 @@ async fn build_file_response(
download: bool,
file_name: &str,
path: PathBuf,
mime: &Mime,
mime: Mime,
req: &HttpRequest,
) -> Result<HttpResponse, Error> {
let content_disposition = ContentDisposition {
@ -162,7 +161,7 @@ async fn build_file_response(
log::error!("file could not be read {:?}", file_err);
error::ErrorInternalServerError("this file should be here but could not be found")
})?
.set_content_type(mime.clone())
.set_content_type(mime)
.set_content_disposition(content_disposition);
let mut response = file.into_response(req);

View File

@ -1,29 +1,26 @@
use std::{collections::HashMap, str::FromStr};
use std::{
collections::{HashMap, VecDeque},
str::FromStr,
};
use lazy_static::lazy_static;
use mime::Mime;
lazy_static! {
static ref ALIASES: HashMap<Mime, Mime> = get_mime_aliases();
static ref PARENTS: Vec<(Mime, Mime)> = get_mime_parent_relations();
static ref ALIASES: HashMap<Mime, Mime> = load_mime_aliases();
static ref PARENTS: Vec<(Mime, Mime)> = load_mime_parent_relations();
static ref EXTENSIONS: HashMap<Mime, &'static str> = load_mime_extensions();
}
fn get_mime_aliases() -> HashMap<Mime, Mime> {
fn load_mime_aliases() -> HashMap<Mime, Mime> {
tree_magic_db::aliases()
.lines()
.flat_map(|line| line.split_once(' '))
.flat_map(|(a, b)| Some((Mime::from_str(a).ok()?, Mime::from_str(b).ok()?)))
.flat_map(|(alias, mime)| Some((Mime::from_str(alias).ok()?, Mime::from_str(mime).ok()?)))
.collect()
}
pub(crate) fn get_alias(mimetype: &Mime) -> &Mime {
match ALIASES.get(mimetype) {
Some(x) => x,
None => mimetype,
}
}
fn get_mime_parent_relations() -> Vec<(Mime, Mime)> {
fn load_mime_parent_relations() -> Vec<(Mime, Mime)> {
tree_magic_db::subclasses()
.lines()
.flat_map(|line| line.split_once(' '))
@ -33,6 +30,22 @@ fn get_mime_parent_relations() -> Vec<(Mime, Mime)> {
.collect()
}
fn load_mime_extensions() -> HashMap<Mime, &'static str> {
include_str!("../mime.types")
.lines()
.filter(|line| !line.is_empty() && !line.starts_with('#'))
.map(|line| line.split_whitespace())
.flat_map(|mut elements| Some((Mime::from_str(elements.next()?).ok()?, elements.next()?)))
.flat_map(|(mime, extension)| {
Some((ALIASES.get(&mime).unwrap_or(&mime).clone(), extension))
})
.collect()
}
pub(crate) fn get_alias(mimetype: Mime) -> Mime {
ALIASES.get(&mimetype).cloned().unwrap_or(mimetype)
}
fn get_mime_parents(mimetype: &Mime) -> Vec<&Mime> {
PARENTS
.iter()
@ -46,3 +59,17 @@ pub(crate) fn matches_text(mime: &Mime) -> bool {
}
return get_mime_parents(mime).into_iter().any(matches_text);
}
pub(crate) fn get_extension(mimetype: &Mime) -> Option<&'static str> {
let mut queue = VecDeque::new();
queue.push_back(mimetype);
dbg!(&*EXTENSIONS);
while let Some(mime) = queue.pop_front() {
dbg!(mime);
match EXTENSIONS.get(mimetype).copied() {
Some(ext) => return Some(ext),
None => queue.extend(get_mime_parents(mime)),
}
}
None
}

View File

@ -1,4 +1,4 @@
use crate::config;
use crate::{config, mime_relations};
use actix_multipart::{Field, Multipart};
use actix_web::{error, http::header::DispositionParam, Error};
use futures_util::{StreamExt, TryStreamExt};
@ -47,7 +47,7 @@ pub(crate) async fn parse_multipart(
content_type = Some(if mime == APPLICATION_OCTET_STREAM {
get_content_type(file_path)
} else {
mime.clone()
mime_relations::get_alias(mime)
});
}
"text" => {
@ -72,7 +72,7 @@ pub(crate) async fn parse_multipart(
let keep_for = keep_for_seconds
.map(|k| k.parse())
.transpose()
.map_err(|e| error::ErrorBadRequest(format!("field keep_for is not a number: {}", e)))?
.map_err(|e| error::ErrorBadRequest(format!("field keep_for is not a number: {e}")))?
.map(Duration::seconds)
.unwrap_or(DEFAULT_UPLOAD_DURATION);
let valid_till = OffsetDateTime::now_utc() + keep_for;
@ -104,8 +104,7 @@ fn check_requirements(
if *keep_for > MAX_UPLOAD_DURATION {
return Err(error::ErrorBadRequest(format!(
"maximum allowed validity is {}, but you specified {}",
MAX_UPLOAD_DURATION, keep_for
"maximum allowed validity is {MAX_UPLOAD_DURATION}, but you specified {keep_for}"
)));
}
@ -135,7 +134,7 @@ fn get_field_name(field: &Field) -> Result<String, error::Error> {
async fn parse_string(name: &str, field: actix_multipart::Field) -> Result<String, error::Error> {
let data = read_content(field).await?;
String::from_utf8(data)
.map_err(|_| error::ErrorBadRequest(format!("could not parse field {} as utf-8", name)))
.map_err(|_| error::ErrorBadRequest(format!("could not parse field {name} as utf-8")))
}
async fn read_content(mut field: actix_multipart::Field) -> Result<Vec<u8>, error::Error> {
@ -171,8 +170,7 @@ async fn write_to_file(
if let Some(max_size) = max_size {
if written_bytes > max_size {
return Err(error::ErrorBadRequest(format!(
"exceeded maximum file size of {} bytes",
max_size
"exceeded maximum file size of {max_size} bytes"
)));
}
}

View File

@ -34,7 +34,7 @@ impl KeyExtractor for ForwardedPeerIpKeyExtractor {
.wait_time_from(DefaultClock::default().now())
.as_secs();
(
format!("too many requests, retry in {}s", wait_time),
format!("too many requests, retry in {wait_time}s"),
ContentType::plaintext(),
)
}

View File

@ -61,7 +61,7 @@ fn render_file_size(size: u64) -> String {
let magnitude = cmp::min((size as f64).log(1024.0) as u32, 5);
let prefix = ["", "ki", "Mi", "Gi", "Ti", "Pi"][magnitude as usize];
let value = size / (1024_u64.pow(magnitude));
format!("{}{}B", value, prefix)
format!("{value}{prefix}B")
}
fn render_duration(duration: Duration) -> String {
@ -88,8 +88,8 @@ fn render_duration(duration: Duration) -> String {
fn pluralize(number: i64, word: &str, suffix: &str) -> Option<String> {
match number {
0 => None,
1 => Some(format!("{} {}", number, word)),
_ => Some(format!("{} {}{}", number, word, suffix)),
1 => Some(format!("{number} {word}")),
_ => Some(format!("{number} {word}{suffix}")),
}
}

View File

@ -2,7 +2,7 @@ use std::io::ErrorKind;
use crate::config::Config;
use crate::multipart::UploadConfig;
use crate::{multipart, template};
use crate::{mime_relations, multipart, template};
use actix_files::NamedFile;
use actix_multipart::Multipart;
use actix_web::http::header::LOCATION;
@ -64,9 +64,12 @@ pub async fn upload(
}
};
let file_name = original_name
.clone()
.unwrap_or_else(|| format!("{}.txt", file_id));
let file_name = original_name.clone().unwrap_or_else(|| {
format!(
"{file_id}.{}",
mime_relations::get_extension(&content_type).unwrap_or("txt")
)
});
let db_insert = sqlx::query(
"INSERT INTO Files (file_id, file_name, content_type, valid_till, delete_on_download) \
VALUES ($1, $2, $3, $4, $5)",
@ -104,15 +107,15 @@ pub async fn upload(
let redirect = if let Some(original_name) = original_name.as_ref() {
let encoded_name = urlencoding::encode(original_name);
format!("/upload/{}/{}", file_id, encoded_name)
format!("/upload/{file_id}/{encoded_name}")
} else {
format!("/upload/{}", file_id)
format!("/upload/{file_id}")
};
let url = get_file_url(&req, &file_id, original_name.as_deref());
Ok(HttpResponse::SeeOther()
.insert_header((LOCATION, redirect))
.body(format!("{}\n", url)))
.body(format!("{url}\n")))
}
async fn create_unique_file(
@ -145,11 +148,12 @@ fn gen_file_id() -> String {
}
fn get_file_url(req: &HttpRequest, id: &str, name: Option<&str>) -> String {
let host = template::get_host_url(req);
if let Some(name) = name {
let encoded_name = urlencoding::encode(name);
format!("{}/{}/{}", template::get_host_url(req), id, encoded_name)
format!("{host}/{id}/{encoded_name}")
} else {
format!("{}/{}", template::get_host_url(req), id)
format!("{host}/{id}")
}
}

View File

@ -15,6 +15,7 @@
<button id="copy" data-copy="#text" class="button hidden">
text kopieren
</button>
<a class="button" href="?raw">roh anzeigen</a>
</main>
<footer>
<a

View File

@ -24,6 +24,7 @@
link kopieren
</button>
<a class="button" href="?dl">als text herunterladen</a>
<a class="button" href="?raw">roh anzeigen</a>
</main>
<footer>
<a