|
|
|
@ -6,19 +6,18 @@ use actix_files::{Files, NamedFile};
|
|
|
|
|
use actix_multipart::Multipart;
|
|
|
|
|
use actix_web::{
|
|
|
|
|
error,
|
|
|
|
|
http::header::{ContentDisposition, DispositionParam, DispositionType},
|
|
|
|
|
middleware,
|
|
|
|
|
web::{self, Bytes},
|
|
|
|
|
App, Error, FromRequest, HttpRequest, HttpResponse, HttpServer,
|
|
|
|
|
http::header::{Charset, ContentDisposition, DispositionParam, DispositionType, ExtendedValue},
|
|
|
|
|
middleware, web, App, Error, HttpRequest, HttpResponse, HttpServer,
|
|
|
|
|
};
|
|
|
|
|
use async_std::{
|
|
|
|
|
channel::{self, Sender},
|
|
|
|
|
fs,
|
|
|
|
|
path::PathBuf,
|
|
|
|
|
path::{Path, PathBuf},
|
|
|
|
|
task,
|
|
|
|
|
};
|
|
|
|
|
use file_kind::FileKind;
|
|
|
|
|
use futures::TryStreamExt;
|
|
|
|
|
use mime::Mime;
|
|
|
|
|
use rand::prelude::SliceRandom;
|
|
|
|
|
use sqlx::{
|
|
|
|
|
postgres::{PgPool, PgPoolOptions, PgRow},
|
|
|
|
@ -57,20 +56,21 @@ async fn upload(
|
|
|
|
|
let mut filename = config.files_dir.clone();
|
|
|
|
|
filename.push(&file_id);
|
|
|
|
|
|
|
|
|
|
let (original_name, valid_till, kind) =
|
|
|
|
|
match multipart::parse_multipart(payload, &file_id, &filename).await {
|
|
|
|
|
Ok(data) => data,
|
|
|
|
|
Err(err) => {
|
|
|
|
|
if filename.exists().await {
|
|
|
|
|
fs::remove_file(filename).await.map_err(|_| {
|
|
|
|
|
error::ErrorInternalServerError(
|
|
|
|
|
"could not parse multipart; could not remove file",
|
|
|
|
|
)
|
|
|
|
|
})?;
|
|
|
|
|
}
|
|
|
|
|
return Err(err);
|
|
|
|
|
let parsed_multipart =
|
|
|
|
|
multipart::parse_multipart(payload, &file_id, &filename, config.max_file_size).await;
|
|
|
|
|
let (original_name, valid_till, kind) = match parsed_multipart {
|
|
|
|
|
Ok(data) => data,
|
|
|
|
|
Err(err) => {
|
|
|
|
|
if filename.exists().await {
|
|
|
|
|
fs::remove_file(filename).await.map_err(|_| {
|
|
|
|
|
error::ErrorInternalServerError(
|
|
|
|
|
"could not parse multipart; could not remove file",
|
|
|
|
|
)
|
|
|
|
|
})?;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
return Err(err);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let db_insert = sqlx::query(
|
|
|
|
|
"INSERT INTO Files (file_id, file_name, valid_till, kind) VALUES ($1, $2, $3, $4)",
|
|
|
|
@ -177,18 +177,47 @@ async fn download(
|
|
|
|
|
let response = HttpResponse::Ok().content_type("text/html").body(view_html);
|
|
|
|
|
Ok(response)
|
|
|
|
|
} else {
|
|
|
|
|
let (content_type, content_disposition) = get_content_types(&path, &file_name);
|
|
|
|
|
let file = NamedFile::open(path)
|
|
|
|
|
.map_err(|_| {
|
|
|
|
|
error::ErrorInternalServerError("this file should be here but could not be found")
|
|
|
|
|
})?
|
|
|
|
|
.set_content_disposition(ContentDisposition {
|
|
|
|
|
disposition: DispositionType::Attachment,
|
|
|
|
|
parameters: vec![DispositionParam::Filename(file_name)],
|
|
|
|
|
});
|
|
|
|
|
.set_content_type(content_type)
|
|
|
|
|
.set_content_disposition(content_disposition);
|
|
|
|
|
file.into_response(&req)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn get_content_types(path: &Path, filename: &str) -> (Mime, ContentDisposition) {
|
|
|
|
|
let std_path = std::path::Path::new(path.as_os_str());
|
|
|
|
|
let ct = 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 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(),
|
|
|
|
|
}))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let cd = ContentDisposition {
|
|
|
|
|
disposition,
|
|
|
|
|
parameters,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
(ct, cd)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn not_found() -> Result<HttpResponse, Error> {
|
|
|
|
|
Ok(HttpResponse::NotFound()
|
|
|
|
|
.content_type("text/plain")
|
|
|
|
@ -240,6 +269,7 @@ async fn setup_db() -> PgPool {
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
|
struct Config {
|
|
|
|
|
files_dir: PathBuf,
|
|
|
|
|
max_file_size: Option<u64>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[actix_web::main]
|
|
|
|
@ -250,8 +280,18 @@ async fn main() -> std::io::Result<()> {
|
|
|
|
|
env_logger::init();
|
|
|
|
|
|
|
|
|
|
let pool: PgPool = setup_db().await;
|
|
|
|
|
let max_file_size = env::var("UPLOAD_MAX_BYTES")
|
|
|
|
|
.ok()
|
|
|
|
|
.and_then(|variable| variable.parse().ok())
|
|
|
|
|
.unwrap_or(8 * 1024 * 1024);
|
|
|
|
|
let max_file_size = if max_file_size == 0 {
|
|
|
|
|
None
|
|
|
|
|
} else {
|
|
|
|
|
Some(max_file_size)
|
|
|
|
|
};
|
|
|
|
|
let config = Config {
|
|
|
|
|
files_dir: PathBuf::from(env::var("FILES_DIR").unwrap_or_else(|_| "./files".to_owned())),
|
|
|
|
|
max_file_size,
|
|
|
|
|
};
|
|
|
|
|
fs::create_dir_all(&config.files_dir)
|
|
|
|
|
.await
|
|
|
|
@ -268,10 +308,6 @@ async fn main() -> std::io::Result<()> {
|
|
|
|
|
|
|
|
|
|
let db = web::Data::new(pool);
|
|
|
|
|
let expiry_watch_sender = web::Data::new(sender);
|
|
|
|
|
let upload_max_bytes: usize = env::var("UPLOAD_MAX_BYTES")
|
|
|
|
|
.ok()
|
|
|
|
|
.and_then(|variable| variable.parse().ok())
|
|
|
|
|
.unwrap_or(8 * 1024 * 1024);
|
|
|
|
|
let bind_address = env::var("BIND_ADDRESS").unwrap_or_else(|_| "0.0.0.0:8000".to_owned());
|
|
|
|
|
|
|
|
|
|
HttpServer::new({
|
|
|
|
@ -280,7 +316,6 @@ async fn main() -> std::io::Result<()> {
|
|
|
|
|
.wrap(middleware::Logger::default())
|
|
|
|
|
.app_data(db.clone())
|
|
|
|
|
.app_data(expiry_watch_sender.clone())
|
|
|
|
|
.app_data(Bytes::configure(|cfg| cfg.limit(upload_max_bytes)))
|
|
|
|
|
.data(config.clone())
|
|
|
|
|
.service(web::resource("/").route(web::get().to(index)))
|
|
|
|
|
.service(web::resource("/upload").route(web::post().to(upload)))
|
|
|
|
|