transbeam/src/main.rs

82 lines
1.9 KiB
Rust

//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;
const APP_NAME: &'static str = "transbeam";
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()));
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);
HttpServer::new(move || {
App::new()
.app_data(data.clone())
.wrap(Logger::default())
.service(upload_socket)
.service(actix_files::Files::new("/", static_dir.clone()).index_file("index.html"))
})
.bind(("127.0.0.1", port))?
.run()
.await?;
Ok(())
}