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
|
|
|
|
2024-08-28 19:02:30 -07:00
|
|
|
use std::{io, net::SocketAddr, path::Path};
|
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};
|
|
|
|
|
2023-07-25 08:14:42 -07:00
|
|
|
mod cache;
|
2024-08-28 19:12:55 -07:00
|
|
|
mod logging;
|
2023-07-25 08:14:42 -07:00
|
|
|
mod nyazoom_headers;
|
2024-08-29 13:58:39 -07:00
|
|
|
mod ssr;
|
2023-04-12 05:07:37 -07:00
|
|
|
mod state;
|
2024-08-28 18:57:36 -07:00
|
|
|
mod sweeper;
|
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
|
|
|
|
2024-08-29 13:58:39 -07:00
|
|
|
use crate::state::*;
|
|
|
|
use crate::views::*;
|
2023-07-26 01:53:41 -07:00
|
|
|
|
2023-04-07 06:59:26 -07:00
|
|
|
#[tokio::main]
|
2023-04-08 08:12:14 -07:00
|
|
|
async fn main() -> io::Result<()> {
|
2024-08-28 19:12:55 -07:00
|
|
|
logging::init_tracing();
|
2023-04-07 06:59:26 -07:00
|
|
|
|
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
|
|
|
|
2024-08-28 19:19:15 -07:00
|
|
|
sweeper::spawn(state.clone());
|
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
|
|
|
|
2024-08-28 19:10:05 -07:00
|
|
|
serve(app).await;
|
2024-08-28 19:03:47 -07:00
|
|
|
|
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 18:45:12 -07:00
|
|
|
Html(ssr::render(move || {
|
2024-08-29 13:58:39 -07:00
|
|
|
leptos::view! { <WelcomePage fact=cat_fact /> }
|
2024-08-28 18:45:12 -07:00
|
|
|
}))
|
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 18:45:12 -07:00
|
|
|
Html(ssr::render(move || {
|
|
|
|
leptos::view! {
|
|
|
|
<HtmxPage>
|
|
|
|
<div class="form-wrapper">
|
|
|
|
<div class="column-container">
|
|
|
|
<ul>
|
2024-08-28 18:55:29 -07:00
|
|
|
{records
|
|
|
|
.keys()
|
|
|
|
.map(|key| {
|
|
|
|
leptos::view! {
|
2024-08-28 18:45:12 -07:00
|
|
|
<li class="link-wrapper">
|
|
|
|
<a href="/link/{key}">{key}</a>
|
2024-08-28 18:55:29 -07:00
|
|
|
<button
|
|
|
|
style="margin-left: 1em;"
|
2024-08-28 18:45:12 -07:00
|
|
|
hx-target="closest .link-wrapper"
|
|
|
|
hx-swap="outerHTML"
|
2024-08-28 18:55:29 -07:00
|
|
|
hx-delete="/link/{key}"
|
|
|
|
>
|
|
|
|
X
|
|
|
|
</button>
|
2024-08-28 18:45:12 -07:00
|
|
|
</li>
|
2024-08-28 18:55:29 -07:00
|
|
|
}
|
|
|
|
})
|
2024-08-28 18:45:12 -07:00
|
|
|
.collect::<Vec<_>>()}
|
|
|
|
</ul>
|
2023-07-31 09:16:18 -07:00
|
|
|
</div>
|
2024-08-28 18:45:12 -07:00
|
|
|
</div>
|
|
|
|
</HtmxPage>
|
|
|
|
}
|
|
|
|
}))
|
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())
|
|
|
|
{
|
2024-08-29 13:49:13 -07:00
|
|
|
return Ok(Html(ssr::render({
|
|
|
|
let record = record.clone();
|
|
|
|
|| {
|
|
|
|
leptos::view! { <DownloadLinkPage id=id record=record /> }
|
|
|
|
}
|
|
|
|
})));
|
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 18:45:12 -07:00
|
|
|
.body(ssr::render(|| {
|
|
|
|
leptos::view! { <LinkView id record /> }
|
|
|
|
}))
|
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
|
|
|
}
|
2024-08-28 19:10:05 -07:00
|
|
|
|
|
|
|
async fn serve(app: Router) {
|
|
|
|
// // Server creation
|
|
|
|
let addr = SocketAddr::from(([0, 0, 0, 0], 3000));
|
|
|
|
let listener = tokio::net::TcpListener::bind(&addr).await.unwrap();
|
|
|
|
axum::serve(
|
|
|
|
listener,
|
|
|
|
app.into_make_service_with_connect_info::<SocketAddr>(),
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
tracing::debug!("listening on http://{}/", addr);
|
|
|
|
}
|