WIP file drop server, no downloads yet
This commit is contained in:
commit
20da86132b
12 changed files with 3294 additions and 0 deletions
82
src/main.rs
Normal file
82
src/main.rs
Normal file
|
@ -0,0 +1,82 @@
|
|||
//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;
|
||||
|
||||
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")))
|
||||
}
|
||||
|
||||
fn app_name() -> String {
|
||||
std::env::var("APP_NAME").unwrap_or_else(|_| String::from("transbeam"))
|
||||
}
|
||||
|
||||
#[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 ip = "0.0.0.0:3000";
|
||||
|
||||
let data: AppData = web::Data::new(RwLock::new(HashMap::new()));
|
||||
|
||||
HttpServer::new(move || {
|
||||
App::new()
|
||||
.app_data(data.clone())
|
||||
.wrap(Logger::default())
|
||||
.service(upload_socket)
|
||||
.service(actix_files::Files::new("/", "./static").index_file("index.html"))
|
||||
})
|
||||
.bind(ip)?
|
||||
.run()
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue