nyazoom/src/main.rs

343 lines
10 KiB
Rust
Raw Normal View History

2023-07-25 08:14:42 -07:00
use async_zip::{tokio::write::ZipFileWriter, Compression, ZipEntryBuilder};
2023-04-07 06:59:26 -07:00
use axum::{
2024-08-28 17:53:13 -07:00
body::Body,
extract::{ConnectInfo, DefaultBodyLimit, Multipart, Request, State},
http::{HeaderMap, Response, StatusCode},
2023-07-25 08:14:42 -07:00
middleware::{self, Next},
2023-07-31 06:32:17 -07:00
response::{Html, IntoResponse, Redirect},
2023-08-02 01:39:50 -07:00
routing::{get, post},
2024-08-28 17:53:13 -07:00
Json, Router,
2023-04-07 06:59:26 -07:00
};
2024-08-28 17:53:13 -07:00
use axum_extra::TypedHeader;
2023-04-11 03:29:21 -07:00
use futures::TryStreamExt;
2023-07-25 08:14:42 -07:00
use nyazoom_headers::ForwardedFor;
2023-04-11 03:29:21 -07:00
use sanitize_filename_reader_friendly::sanitize;
2023-04-12 05:07:37 -07:00
2023-07-27 08:40:01 -07:00
use std::{io, net::SocketAddr, path::Path, time::Duration};
2023-04-11 03:29:21 -07:00
2023-07-25 08:14:42 -07:00
use tokio_util::{
compat::FuturesAsyncWriteCompatExt,
io::{ReaderStream, StreamReader},
};
2023-04-11 03:29:21 -07:00
2023-04-07 06:59:26 -07:00
use tower_http::{limit::RequestBodyLimitLayer, services::ServeDir, trace::TraceLayer};
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
2023-07-25 08:14:42 -07:00
mod cache;
mod nyazoom_headers;
2023-04-12 05:07:37 -07:00
mod state;
2023-07-25 08:14:42 -07:00
mod util;
2023-07-26 01:53:41 -07:00
mod views;
2023-04-12 04:59:43 -07:00
2023-04-12 05:07:37 -07:00
use state::{AppState, UploadRecord};
2023-04-12 04:59:43 -07:00
2023-07-31 09:16:18 -07:00
use crate::state::AsyncRemoveRecord;
use crate::views::{DownloadLinkPage, HtmxPage, LinkView, Welcome};
2023-07-26 01:53:41 -07:00
2023-04-12 05:09:33 -07:00
pub mod error {
use std::io::{Error, ErrorKind};
pub fn io_other(s: &str) -> Error {
Error::new(ErrorKind::Other, s)
}
}
2023-04-07 06:59:26 -07:00
#[tokio::main]
2023-04-08 08:12:14 -07:00
async fn main() -> io::Result<()> {
2023-04-12 04:59:43 -07:00
// Set up logging
2023-04-07 06:59:26 -07:00
tracing_subscriber::registry()
.with(
tracing_subscriber::EnvFilter::try_from_default_env()
2023-04-08 08:12:14 -07:00
.unwrap_or_else(|_| "nyazoom=debug,tower_http=debug".into()),
2023-04-07 06:59:26 -07:00
)
.with(tracing_subscriber::fmt::layer())
.init();
2023-04-12 04:59:43 -07:00
// uses create_dir_all to create both .cache and serve inside it in one go
2023-07-25 08:14:42 -07:00
util::make_dir(".cache/serve").await?;
2023-04-08 08:12:14 -07:00
2023-07-25 08:14:42 -07:00
let state = cache::fetch_cache().await;
2023-04-12 04:59:43 -07:00
2023-07-27 08:58:13 -07:00
// Spawn a repeating task that will clean files periodically
tokio::spawn({
let state = state.clone();
async move {
loop {
2023-07-31 06:32:17 -07:00
tokio::time::sleep(Duration::from_secs(15 * 60)).await;
2023-07-27 08:58:13 -07:00
tracing::info!("Cleaning Sweep!");
let mut records = state.records.lock().await;
for (key, record) in records.clone().into_iter() {
if !record.can_be_downloaded() {
2023-07-31 09:16:18 -07:00
tracing::info!("culling: {:?}", record);
records.remove_record(&key).await.unwrap();
2023-07-27 08:58:13 -07:00
}
}
}
}
});
2023-04-08 08:12:14 -07:00
// Router Setup
2023-04-12 06:40:55 -07:00
let app = Router::new()
2023-07-26 01:53:41 -07:00
.route("/", get(welcome))
2023-04-11 03:29:21 -07:00
.route("/upload", post(upload_to_zip))
2023-07-27 08:40:01 -07:00
.route("/records", get(records))
.route("/records/links", get(records_links))
2023-04-12 06:40:55 -07:00
.route("/download/:id", get(download))
2023-08-02 01:39:50 -07:00
.route("/link/:id", get(link).delete(link_delete))
2023-07-31 09:16:18 -07:00
.route("/link/:id/remaining", get(remaining))
2023-04-07 06:59:26 -07:00
.layer(DefaultBodyLimit::disable())
.layer(RequestBodyLimitLayer::new(
2023-04-08 08:12:14 -07:00
10 * 1024 * 1024 * 1024, // 10GiB
2023-04-12 04:59:43 -07:00
))
2023-04-12 06:40:55 -07:00
.with_state(state)
2023-07-27 08:40:01 -07:00
.fallback_service(ServeDir::new("dist"))
2023-04-13 11:49:13 -07:00
.layer(TraceLayer::new_for_http())
.layer(middleware::from_fn(log_source));
2023-04-07 06:59:26 -07:00
2023-04-08 08:12:14 -07:00
// Server creation
2023-04-07 20:37:20 -07:00
let addr = SocketAddr::from(([0, 0, 0, 0], 3000));
2023-04-09 01:22:44 -07:00
tracing::debug!("listening on http://{}/", addr);
2024-08-28 17:53:13 -07:00
let listener = tokio::net::TcpListener::bind(&addr).await.unwrap();
axum::serve(
listener,
app.into_make_service_with_connect_info::<SocketAddr>(),
)
.await
.unwrap();
2023-04-08 08:12:14 -07:00
Ok(())
2023-04-07 06:59:26 -07:00
}
2023-07-31 09:16:18 -07:00
async fn remaining(
State(state): State<AppState>,
axum::extract::Path(id): axum::extract::Path<String>,
) -> impl IntoResponse {
let records = state.records.lock().await;
if let Some(record) = records.get(&id) {
let downloads_remaining = record.downloads_remaining();
let plural = if downloads_remaining > 1 { "s" } else { "" };
let out = format!(
"You have {} download{} remaining!",
downloads_remaining, plural
);
Html(out)
} else {
Html("?".to_string())
}
}
2023-07-26 01:53:41 -07:00
async fn welcome() -> impl IntoResponse {
let cat_fact = views::get_cat_fact().await;
2024-08-28 17:53:13 -07:00
Html(
leptos::ssr::render_to_string(move || {
leptos::view! { <Welcome fact=cat_fact /> }
})
.to_string(),
)
2023-07-26 01:53:41 -07:00
}
2023-07-27 08:40:01 -07:00
async fn records(State(state): State<AppState>) -> impl IntoResponse {
Json(state.records.lock().await.clone())
}
2023-07-31 09:16:18 -07:00
// This function is to remain ugly until that time in which I properly hide
// this behind some kind of authentication
2023-07-27 08:40:01 -07:00
async fn records_links(State(state): State<AppState>) -> impl IntoResponse {
let records = state.records.lock().await.clone();
2024-08-28 17:53:13 -07:00
Html(
leptos::ssr::render_to_string(move || {
leptos::view! {
<HtmxPage>
<div class="form-wrapper">
<div class="column-container">
<ul>
{records.keys().map(|key| leptos::view! {
<li class="link-wrapper">
<a href="/link/{key}">{key}</a>
<button style="margin-left: 1em;"
hx-target="closest .link-wrapper"
hx-swap="outerHTML"
hx-delete="/link/{key}">X</button>
</li>
})
.collect::<Vec<_>>()}
</ul>
</div>
2023-07-31 09:16:18 -07:00
</div>
2024-08-28 17:53:13 -07:00
</HtmxPage>
}
})
.to_string(),
)
2023-07-27 08:40:01 -07:00
}
async fn link(
axum::extract::Path(id): axum::extract::Path<String>,
2023-07-31 09:16:18 -07:00
State(mut state): State<AppState>,
2023-07-27 08:40:01 -07:00
) -> Result<Html<String>, Redirect> {
2023-07-31 09:16:18 -07:00
{
let mut records = state.records.lock().await;
2023-08-02 01:39:50 -07:00
if let Some(record) = records
.get_mut(&id)
.filter(|record| record.can_be_downloaded())
{
2023-07-31 09:16:18 -07:00
if record.can_be_downloaded() {
2024-08-28 17:53:13 -07:00
return Ok(Html(
leptos::ssr::render_to_string({
let record = record.clone();
|| {
leptos::view! { <DownloadLinkPage id=id record=record /> }
}
})
.into(),
));
2023-07-31 09:16:18 -07:00
}
2023-07-27 08:40:01 -07:00
}
}
2023-07-31 09:16:18 -07:00
// TODO: This....
state.remove_record(&id).await.unwrap();
2024-08-28 17:53:13 -07:00
Err(Redirect::to("/404.html"))
2023-07-27 08:40:01 -07:00
}
2023-07-31 09:16:18 -07:00
async fn link_delete(
axum::extract::Path(id): axum::extract::Path<String>,
State(mut state): State<AppState>,
) -> Result<Html<String>, (StatusCode, String)> {
state
.remove_record(&id)
.await
.map_err(|err| (StatusCode::INTERNAL_SERVER_ERROR, err.to_string()))?;
Ok(Html("".to_string()))
}
2024-08-28 17:53:13 -07:00
async fn log_source(
2023-04-15 06:36:04 -07:00
ConnectInfo(addr): ConnectInfo<SocketAddr>,
2023-07-25 08:14:42 -07:00
forwarded_for: Option<TypedHeader<ForwardedFor>>,
2024-08-28 17:53:13 -07:00
req: Request,
next: Next,
2023-07-31 06:32:17 -07:00
) -> impl IntoResponse {
2023-07-25 08:14:42 -07:00
tracing::info!("{} : {:?}", addr, forwarded_for);
2023-04-13 11:49:13 -07:00
next.run(req).await
}
2023-04-12 04:59:43 -07:00
async fn upload_to_zip(
State(state): State<AppState>,
mut body: Multipart,
2023-07-31 06:32:17 -07:00
) -> Result<Response<String>, (StatusCode, String)> {
2023-04-12 04:59:43 -07:00
tracing::debug!("{:?}", *state.records.lock().await);
2023-07-25 08:14:42 -07:00
let cache_name = util::get_random_name(10);
2023-04-08 08:12:14 -07:00
2024-08-28 17:53:13 -07:00
let archive_path = Path::new(".cache/serve").join(format!("{}.zip", &cache_name));
2023-04-12 04:59:43 -07:00
2023-04-11 03:29:21 -07:00
tracing::debug!("Zipping: {:?}", &archive_path);
2023-04-12 04:59:43 -07:00
let mut archive = tokio::fs::File::create(&archive_path)
2023-04-08 08:12:14 -07:00
.await
.map_err(|err| (StatusCode::INTERNAL_SERVER_ERROR, err.to_string()))?;
2023-04-11 03:29:21 -07:00
let mut writer = ZipFileWriter::new(&mut archive);
2023-04-08 08:12:14 -07:00
2023-04-07 06:59:26 -07:00
while let Some(field) = body.next_field().await.unwrap() {
2023-04-11 03:29:21 -07:00
let file_name = match field.file_name() {
Some(file_name) => sanitize(file_name),
_ => continue,
2023-04-08 08:12:14 -07:00
};
2023-04-11 03:29:21 -07:00
tracing::debug!("Downloading to Zip: {file_name:?}");
2023-04-08 08:12:14 -07:00
2023-04-11 03:29:21 -07:00
let stream = field;
2024-08-28 17:53:13 -07:00
let body_with_io_error = stream.map_err(io::Error::other);
2023-04-12 04:59:43 -07:00
let mut body_reader = StreamReader::new(body_with_io_error);
2023-04-08 08:12:14 -07:00
2023-04-11 03:29:21 -07:00
let builder = ZipEntryBuilder::new(file_name, Compression::Deflate);
let mut entry_writer = writer
.write_entry_stream(builder)
.await
.unwrap()
.compat_write();
tokio::io::copy(&mut body_reader, &mut entry_writer)
.await
.map_err(|err| (StatusCode::INTERNAL_SERVER_ERROR, err.to_string()))?;
entry_writer
.into_inner()
.close()
.await
.map_err(|err| (StatusCode::INTERNAL_SERVER_ERROR, err.to_string()))?;
2023-04-08 08:12:14 -07:00
}
2023-04-09 01:22:44 -07:00
2023-04-12 04:59:43 -07:00
let mut records = state.records.lock().await;
2023-07-31 06:32:17 -07:00
let record = UploadRecord::new(archive_path);
records.insert(cache_name.clone(), record.clone());
2023-04-12 04:59:43 -07:00
2023-07-25 08:14:42 -07:00
cache::write_to_cache(&records)
2023-04-12 04:59:43 -07:00
.await
.map_err(|err| (StatusCode::INTERNAL_SERVER_ERROR, err.to_string()))?;
2023-04-11 03:29:21 -07:00
writer.close().await.unwrap();
2023-04-09 00:03:05 -07:00
2023-07-31 06:32:17 -07:00
let id = cache_name;
let response = Response::builder()
.status(200)
.header("Content-Type", "text/html")
.header("HX-Push-Url", format!("/link/{}", &id))
2024-08-28 17:53:13 -07:00
.body(
leptos::ssr::render_to_string(|| {
leptos::view! { <LinkView id record /> }
})
.into(),
)
2023-07-31 06:32:17 -07:00
.unwrap();
Ok(response)
2023-04-12 06:40:55 -07:00
}
async fn download(
axum::extract::Path(id): axum::extract::Path<String>,
2023-07-31 09:16:18 -07:00
headers: HeaderMap,
2023-08-02 01:39:50 -07:00
State(state): State<AppState>,
2023-04-12 06:40:55 -07:00
) -> Result<axum::response::Response, (StatusCode, String)> {
2023-07-31 09:16:18 -07:00
{
let mut records = state.records.lock().await;
if headers.get("hx-request").is_some() {
2023-04-12 06:40:55 -07:00
return Ok(axum::http::Response::builder()
2023-07-31 09:16:18 -07:00
.header("HX-Redirect", format!("/download/{id}"))
.status(204)
.body("".to_owned())
2023-04-12 06:40:55 -07:00
.unwrap()
.into_response());
2023-07-31 09:16:18 -07:00
}
2023-08-02 01:39:50 -07:00
if let Some(record) = records
.get_mut(&id)
.filter(|record| record.can_be_downloaded())
{
record.downloads += 1;
2023-07-31 09:16:18 -07:00
2023-08-02 01:39:50 -07:00
let file = tokio::fs::File::open(&record.file).await.unwrap();
2023-07-31 09:16:18 -07:00
2023-08-02 01:39:50 -07:00
return Ok(axum::response::Response::builder()
.header("Content-Type", "application/zip")
2024-08-28 17:53:13 -07:00
.body(Body::from_stream(ReaderStream::new(file)))
.unwrap());
2023-08-02 01:39:50 -07:00
} else {
records.remove_record(&id).await.unwrap()
2023-04-12 06:40:55 -07:00
}
}
Ok(Redirect::to("/404.html").into_response())
2023-04-08 08:12:14 -07:00
}