2022-04-26 23:54:29 -04:00
|
|
|
//mod download;
|
|
|
|
mod file;
|
|
|
|
mod upload;
|
|
|
|
mod zip;
|
|
|
|
|
|
|
|
use std::{collections::HashMap, task::Waker, sync::{mpsc::Sender, RwLock}, path::PathBuf};
|
|
|
|
|
|
|
|
use actix::Addr;
|
|
|
|
use actix_web::{
|
|
|
|
get, middleware::Logger, web, App, HttpResponse, HttpServer,
|
|
|
|
Responder, HttpRequest,
|
|
|
|
};
|
|
|
|
use actix_web_actors::ws;
|
|
|
|
use time::OffsetDateTime;
|
|
|
|
|
2022-04-27 00:44:35 -04:00
|
|
|
const APP_NAME: &'static str = "transbeam";
|
|
|
|
|
2022-04-26 23:54:29 -04:00
|
|
|
pub struct UploadedFile {
|
|
|
|
name: String,
|
|
|
|
size: usize,
|
|
|
|
modtime: OffsetDateTime,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl UploadedFile {
|
|
|
|
fn new(name: &str, size: usize, modtime: OffsetDateTime) -> Self {
|
|
|
|
Self {
|
|
|
|
name: sanitise_file_name::sanitise(name),
|
|
|
|
size,
|
|
|
|
modtime,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub struct DownloadableFile {
|
|
|
|
name: String,
|
|
|
|
size: usize,
|
|
|
|
modtime: OffsetDateTime,
|
|
|
|
uploader: Option<Addr<upload::Uploader>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
type AppData = web::Data<RwLock<HashMap<String, DownloadableFile>>>;
|
|
|
|
|
|
|
|
fn storage_dir() -> PathBuf {
|
|
|
|
PathBuf::from(std::env::var("STORAGE_DIR").unwrap_or_else(|_| String::from("storage")))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[get("/upload")]
|
|
|
|
async fn upload_socket(
|
|
|
|
req: HttpRequest,
|
|
|
|
stream: web::Payload,
|
|
|
|
data: AppData,
|
|
|
|
) -> impl Responder {
|
|
|
|
ws::start(
|
|
|
|
upload::Uploader::new(data),
|
|
|
|
&req,
|
|
|
|
stream
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[actix_web::main]
|
|
|
|
async fn main() -> std::io::Result<()> {
|
|
|
|
env_logger::init();
|
|
|
|
|
|
|
|
let data: AppData = web::Data::new(RwLock::new(HashMap::new()));
|
|
|
|
|
2022-04-27 00:53:32 -04:00
|
|
|
let static_dir = PathBuf::from(std::env::var("STATIC_DIR").unwrap_or_else(|_| String::from("static")));
|
|
|
|
let port = std::env::var("PORT").ok().and_then(|p| p.parse::<u16>().ok()).unwrap_or(8080);
|
|
|
|
|
2022-04-26 23:54:29 -04:00
|
|
|
HttpServer::new(move || {
|
|
|
|
App::new()
|
|
|
|
.app_data(data.clone())
|
|
|
|
.wrap(Logger::default())
|
|
|
|
.service(upload_socket)
|
2022-04-27 00:53:32 -04:00
|
|
|
.service(actix_files::Files::new("/", static_dir.clone()).index_file("index.html"))
|
2022-04-26 23:54:29 -04:00
|
|
|
})
|
2022-04-27 00:53:32 -04:00
|
|
|
.bind(("127.0.0.1", port))?
|
2022-04-26 23:54:29 -04:00
|
|
|
.run()
|
|
|
|
.await?;
|
|
|
|
Ok(())
|
|
|
|
}
|