|
|
|
@ -12,15 +12,16 @@ use actix_web::{
|
|
|
|
|
App, Error, FromRequest, HttpRequest, HttpResponse, HttpServer,
|
|
|
|
|
};
|
|
|
|
|
use async_std::{
|
|
|
|
|
channel::{self, Sender},
|
|
|
|
|
fs,
|
|
|
|
|
path::PathBuf,
|
|
|
|
|
sync::{channel, Sender},
|
|
|
|
|
task,
|
|
|
|
|
};
|
|
|
|
|
use file_kind::FileKind;
|
|
|
|
|
use futures::TryStreamExt;
|
|
|
|
|
use sqlx::{
|
|
|
|
|
postgres::{PgPool, PgRow},
|
|
|
|
|
Cursor, Row,
|
|
|
|
|
postgres::{PgPool, PgPoolOptions, PgRow},
|
|
|
|
|
Row,
|
|
|
|
|
};
|
|
|
|
|
use std::env;
|
|
|
|
|
|
|
|
|
@ -92,10 +93,11 @@ async fn upload(
|
|
|
|
|
kind
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
expiry_watch_sender.send(()).await;
|
|
|
|
|
expiry_watch_sender.send(()).await.unwrap();
|
|
|
|
|
|
|
|
|
|
let redirect = if kind == FileKind::BINARY && original_name.is_some() {
|
|
|
|
|
format!("/upload/{}/{}", file_id, original_name.as_ref().unwrap())
|
|
|
|
|
let encoded_name = urlencoding::encode(original_name.as_ref().unwrap());
|
|
|
|
|
format!("/upload/{}/{}", file_id, encoded_name)
|
|
|
|
|
} else {
|
|
|
|
|
format!("/upload/{}", file_id)
|
|
|
|
|
};
|
|
|
|
@ -113,7 +115,8 @@ fn get_host_url(req: &web::HttpRequest) -> String {
|
|
|
|
|
|
|
|
|
|
fn get_file_url(req: &web::HttpRequest, id: &str, name: Option<&str>) -> String {
|
|
|
|
|
if let Some(name) = name {
|
|
|
|
|
format!("{}/file/{}/{}", get_host_url(req), id, name)
|
|
|
|
|
let encoded_name = urlencoding::encode(name);
|
|
|
|
|
format!("{}/file/{}/{}", get_host_url(req), id, encoded_name)
|
|
|
|
|
} else {
|
|
|
|
|
format!("{}/file/{}", get_host_url(req), id)
|
|
|
|
|
}
|
|
|
|
@ -135,11 +138,11 @@ async fn download(
|
|
|
|
|
config: web::Data<Config>,
|
|
|
|
|
) -> Result<HttpResponse, Error> {
|
|
|
|
|
let id = req.match_info().query("id");
|
|
|
|
|
let mut cursor = sqlx::query("SELECT file_id, file_name, kind from files WHERE file_id = $1")
|
|
|
|
|
let mut rows = sqlx::query("SELECT file_id, file_name, kind from files WHERE file_id = $1")
|
|
|
|
|
.bind(id)
|
|
|
|
|
.fetch(db.as_ref());
|
|
|
|
|
let row: PgRow = cursor
|
|
|
|
|
.next()
|
|
|
|
|
let row: PgRow = rows
|
|
|
|
|
.try_next()
|
|
|
|
|
.await
|
|
|
|
|
.map_err(|_| error::ErrorInternalServerError("could not run select statement"))?
|
|
|
|
|
.ok_or_else(|| error::ErrorNotFound("file does not exist or has expired"))?;
|
|
|
|
@ -204,10 +207,10 @@ async fn setup_db() -> PgPool {
|
|
|
|
|
let conn_url = &get_db_url();
|
|
|
|
|
log::info!("Using Connection string {}", conn_url);
|
|
|
|
|
|
|
|
|
|
let pool = PgPool::builder()
|
|
|
|
|
.max_size(5)
|
|
|
|
|
let pool = PgPoolOptions::new()
|
|
|
|
|
.max_connections(5)
|
|
|
|
|
.connect_timeout(std::time::Duration::from_secs(5))
|
|
|
|
|
.build(conn_url)
|
|
|
|
|
.connect(conn_url)
|
|
|
|
|
.await
|
|
|
|
|
.expect("could not create db pool");
|
|
|
|
|
|
|
|
|
@ -224,7 +227,7 @@ struct Config {
|
|
|
|
|
files_dir: PathBuf,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[actix_rt::main]
|
|
|
|
|
#[actix_web::main]
|
|
|
|
|
async fn main() -> std::io::Result<()> {
|
|
|
|
|
if env::var("RUST_LOG").is_err() {
|
|
|
|
|
env::set_var("RUST_LOG", "info");
|
|
|
|
@ -238,7 +241,7 @@ async fn main() -> std::io::Result<()> {
|
|
|
|
|
fs::create_dir_all(&config.files_dir)
|
|
|
|
|
.await
|
|
|
|
|
.expect("could not create directory for storing files");
|
|
|
|
|
let (sender, receiver) = channel(8);
|
|
|
|
|
let (sender, receiver) = channel::bounded(8);
|
|
|
|
|
|
|
|
|
|
log::info!("omnomnom");
|
|
|
|
|
|
|
|
|
|