2022-04-27 20:15:51 -04:00
|
|
|
mod download;
|
2022-04-30 01:38:26 -04:00
|
|
|
mod store;
|
2022-04-26 23:54:29 -04:00
|
|
|
mod upload;
|
2022-04-28 05:13:14 -04:00
|
|
|
mod util;
|
2022-04-26 23:54:29 -04:00
|
|
|
mod zip;
|
|
|
|
|
2022-04-28 05:18:35 -04:00
|
|
|
use std::{fs::File, path::PathBuf};
|
2022-04-26 23:54:29 -04:00
|
|
|
|
|
|
|
use actix_web::{
|
2022-04-28 05:18:35 -04:00
|
|
|
get, middleware::Logger, web, App, HttpRequest, HttpResponse, HttpServer, Responder,
|
2022-04-26 23:54:29 -04:00
|
|
|
};
|
|
|
|
use actix_web_actors::ws;
|
2022-04-30 01:38:26 -04:00
|
|
|
use log::error;
|
|
|
|
use store::FileStore;
|
2022-04-27 20:15:51 -04:00
|
|
|
use tokio::sync::RwLock;
|
2022-04-26 23:54:29 -04:00
|
|
|
|
2022-04-28 05:18:35 -04:00
|
|
|
const APP_NAME: &str = "transbeam";
|
2022-04-27 00:44:35 -04:00
|
|
|
|
2022-04-30 01:38:26 -04:00
|
|
|
type AppData = web::Data<RwLock<FileStore>>;
|
2022-04-26 23:54:29 -04:00
|
|
|
|
2022-04-27 20:15:51 -04:00
|
|
|
#[get("/download/{file_code}")]
|
2022-04-28 05:18:35 -04:00
|
|
|
async fn handle_download(
|
|
|
|
req: HttpRequest,
|
|
|
|
path: web::Path<String>,
|
|
|
|
data: AppData,
|
|
|
|
) -> actix_web::Result<HttpResponse> {
|
2022-04-27 20:15:51 -04:00
|
|
|
let file_code = path.into_inner();
|
2022-04-28 05:13:14 -04:00
|
|
|
if !util::is_ascii_alphanumeric(&file_code) {
|
2022-04-27 20:15:51 -04:00
|
|
|
return Ok(HttpResponse::NotFound().finish());
|
|
|
|
}
|
|
|
|
let data = data.read().await;
|
2022-04-28 05:13:14 -04:00
|
|
|
let info = data.lookup_file(&file_code);
|
2022-04-27 20:15:51 -04:00
|
|
|
if let Some(info) = info {
|
2022-04-30 01:38:26 -04:00
|
|
|
let storage_path = store::storage_dir().join(file_code);
|
2022-04-29 22:36:44 -04:00
|
|
|
let file = File::open(&storage_path)?;
|
2022-04-27 20:15:51 -04:00
|
|
|
Ok(download::DownloadingFile {
|
2022-04-29 22:36:44 -04:00
|
|
|
file,
|
|
|
|
storage_path,
|
2022-04-28 05:18:35 -04:00
|
|
|
info,
|
|
|
|
}
|
|
|
|
.into_response(&req))
|
2022-04-27 20:15:51 -04:00
|
|
|
} else {
|
|
|
|
Ok(HttpResponse::NotFound().finish())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-26 23:54:29 -04:00
|
|
|
#[get("/upload")]
|
2022-04-27 20:15:51 -04:00
|
|
|
async fn handle_upload(req: HttpRequest, stream: web::Payload, data: AppData) -> impl Responder {
|
2022-04-27 00:55:36 -04:00
|
|
|
ws::start(upload::Uploader::new(data), &req, stream)
|
2022-04-26 23:54:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[actix_web::main]
|
|
|
|
async fn main() -> std::io::Result<()> {
|
|
|
|
env_logger::init();
|
|
|
|
|
2022-04-30 01:38:26 -04:00
|
|
|
let data: AppData = web::Data::new(RwLock::new(FileStore::load().await?));
|
|
|
|
start_reaper(data.clone());
|
2022-04-26 23:54:29 -04:00
|
|
|
|
2022-04-27 00:55:36 -04:00
|
|
|
let static_dir =
|
2022-04-30 01:53:21 -04:00
|
|
|
PathBuf::from(std::env::var("TRANSBEAM_STATIC_DIR").unwrap_or_else(|_| String::from("static")));
|
|
|
|
let port = std::env::var("TRANSBEAM_PORT")
|
2022-04-27 00:55:36 -04:00
|
|
|
.ok()
|
|
|
|
.and_then(|p| p.parse::<u16>().ok())
|
|
|
|
.unwrap_or(8080);
|
2022-04-27 00:53:32 -04:00
|
|
|
|
2022-04-26 23:54:29 -04:00
|
|
|
HttpServer::new(move || {
|
|
|
|
App::new()
|
|
|
|
.app_data(data.clone())
|
|
|
|
.wrap(Logger::default())
|
2022-04-27 20:15:51 -04:00
|
|
|
.service(handle_upload)
|
|
|
|
.service(handle_download)
|
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(())
|
|
|
|
}
|
2022-04-30 01:38:26 -04:00
|
|
|
|
|
|
|
fn start_reaper(data: AppData) {
|
|
|
|
std::thread::spawn(move || {
|
|
|
|
actix_web::rt::System::new().block_on(async {
|
|
|
|
loop {
|
|
|
|
actix_web::rt::time::sleep(core::time::Duration::from_secs(86400)).await;
|
|
|
|
if let Err(e) = data.write().await.remove_expired_files().await {
|
|
|
|
error!("Error reaping expired files: {}", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|