nyazoom improves!

This commit is contained in:
Zynh0722 2023-07-31 09:16:18 -07:00
parent 6ad91ff7e5
commit 6d939efbbf
7 changed files with 163 additions and 46 deletions

16
Cargo.lock generated
View file

@ -1241,6 +1241,20 @@ dependencies = [
"uuid",
]
[[package]]
name = "leptos_meta"
version = "0.4.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7ab2fcfe2ae8f14a58798007dd90126c60f529f265e5fa0bf816e047b60b753c"
dependencies = [
"cfg-if",
"indexmap 2.0.0",
"leptos",
"tracing",
"wasm-bindgen",
"web-sys",
]
[[package]]
name = "leptos_reactive"
version = "0.4.6"
@ -1524,6 +1538,7 @@ name = "nyazoom"
version = "0.1.0"
dependencies = [
"async-bincode",
"async-trait",
"async_zip",
"axum",
"bincode",
@ -1531,6 +1546,7 @@ dependencies = [
"futures",
"headers",
"leptos",
"leptos_meta",
"leptos_router",
"rand",
"reqwest",

View file

@ -8,6 +8,7 @@ edition = "2021"
[dependencies]
async-bincode = { version = "0.7.0", features = ["tokio"] }
async-trait = "0.1.72"
async_zip = { version = "0.0.13", features = ["deflate", "tokio", "tokio-fs", "async-compression"] }
axum = { version = "0.6.12", features = ["multipart", "http2", "headers", "macros", "original-uri"] }
bincode = "1.3.3"
@ -15,6 +16,7 @@ chrono = { version = "0.4.24", features = ["serde"] }
futures = "0.3.28"
headers = "0.3.8"
leptos = { version = "0.4.6", features = ["ssr", "nightly", "tracing", "default-tls"] }
leptos_meta = { version = "0.4.6", features = ["ssr"] }
leptos_router = { version = "0.4.6", features = ["ssr"] }
rand = { version = "0.8.5", features = ["small_rng"] }
reqwest = { version = "0.11.18", features = ["json", "native-tls", "blocking"] }

1
dist/css/link.css vendored
View file

@ -16,6 +16,7 @@ body {
padding: 1.5em;
border-radius: 1em;
border: 1px solid #25283d;
list-style: none;
}
.return-button {

1
dist/css/main.css vendored
View file

@ -25,6 +25,7 @@ body {
justify-content: center;
}
.cat-img {
width: 250px;
height: 250px;

View file

@ -6,12 +6,13 @@ use axum::{
http::{Request, Response, StatusCode},
middleware::{self, Next},
response::{Html, IntoResponse, Redirect},
routing::{get, post},
routing::{delete, get, post},
Json, Router, TypedHeader,
};
use futures::TryStreamExt;
use headers::HeaderMap;
use leptos::IntoView;
use nyazoom_headers::ForwardedFor;
@ -36,7 +37,8 @@ mod views;
use state::{AppState, UploadRecord};
use crate::views::{DownloadLinkPage, LinkView, Welcome};
use crate::state::AsyncRemoveRecord;
use crate::views::{DownloadLinkPage, HtmxPage, LinkView, Welcome};
pub mod error {
use std::io::{Error, ErrorKind};
@ -74,10 +76,8 @@ async fn main() -> io::Result<()> {
for (key, record) in records.clone().into_iter() {
if !record.can_be_downloaded() {
tracing::info!("{:?} should be culled", record);
let _ = tokio::fs::remove_file(&record.file).await;
records.remove(key.as_str());
cache::write_to_cache(&records).await.unwrap();
tracing::info!("culling: {:?}", record);
records.remove_record(&key).await.unwrap();
}
}
}
@ -92,6 +92,8 @@ async fn main() -> io::Result<()> {
.route("/records/links", get(records_links))
.route("/download/:id", get(download))
.route("/link/:id", get(link))
.route("/link/:id", delete(link_delete))
.route("/link/:id/remaining", get(remaining))
.layer(DefaultBodyLimit::disable())
.layer(RequestBodyLimitLayer::new(
10 * 1024 * 1024 * 1024, // 10GiB
@ -112,6 +114,24 @@ async fn main() -> io::Result<()> {
Ok(())
}
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())
}
}
async fn welcome() -> impl IntoResponse {
let cat_fact = views::get_cat_fact().await;
Html(leptos::ssr::render_to_string(move |cx| {
@ -123,45 +143,71 @@ async fn records(State(state): State<AppState>) -> impl IntoResponse {
Json(state.records.lock().await.clone())
}
// This function is to remain ugly until that time in which I properly hide
// this behind some kind of authentication
async fn records_links(State(state): State<AppState>) -> impl IntoResponse {
let records = state.records.lock().await.clone();
Html(leptos::ssr::render_to_string(move |cx| {
leptos::view! { cx,
<ul>
{records
.iter()
.map(|(key, _)|
leptos::view! { cx, <li><a href="/link/{key}">{key}</a></li> })
.collect::<Vec<_>>()}
</ul>
<HtmxPage>
<div class="form-wrapper">
<div class="column-container">
<ul>
{records.keys().map(|key| leptos::view! { cx,
<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>
</div>
</HtmxPage>
}
}))
}
async fn link(
axum::extract::Path(id): axum::extract::Path<String>,
State(state): State<AppState>,
State(mut state): State<AppState>,
) -> Result<Html<String>, Redirect> {
let mut records = state.records.lock().await;
{
let mut records = state.records.lock().await;
if let Some(record) = records.get_mut(&id) {
if record.can_be_downloaded() {
return Ok(Html(leptos::ssr::render_to_string({
let record = record.clone();
|cx| {
leptos::view! { cx, <DownloadLinkPage id=id record=record /> }
}
})));
} else {
let _ = tokio::fs::remove_file(&record.file).await;
records.remove(&id);
cache::write_to_cache(&records).await.unwrap();
if let Some(record) = records.get_mut(&id) {
if record.can_be_downloaded() {
return Ok(Html(leptos::ssr::render_to_string({
let record = record.clone();
|cx| {
leptos::view! { cx, <DownloadLinkPage id=id record=record /> }
}
})));
}
}
}
// TODO: This....
state.remove_record(&id).await.unwrap();
Err(Redirect::to(&format!("/404.html")))
}
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()))
}
async fn log_source<B>(
ConnectInfo(addr): ConnectInfo<SocketAddr>,
forwarded_for: Option<TypedHeader<ForwardedFor>>,
@ -245,27 +291,38 @@ async fn upload_to_zip(
async fn download(
axum::extract::Path(id): axum::extract::Path<String>,
State(state): State<AppState>,
headers: HeaderMap,
State(mut state): State<AppState>,
) -> Result<axum::response::Response, (StatusCode, String)> {
let mut records = state.records.lock().await;
if let Some(record) = records.get_mut(&id) {
if record.can_be_downloaded() {
record.downloads += 1;
let file = tokio::fs::File::open(&record.file).await.unwrap();
{
let mut records = state.records.lock().await;
tracing::info!("{headers:?}");
if headers.get("hx-request").is_some() {
return Ok(axum::http::Response::builder()
.header("Content-Type", "application/zip")
.body(StreamBody::new(ReaderStream::new(file)))
.header("HX-Redirect", format!("/download/{id}"))
.status(204)
.body("".to_owned())
.unwrap()
.into_response());
} else {
let _ = tokio::fs::remove_file(&record.file).await;
records.remove(&id);
cache::write_to_cache(&records).await.unwrap();
}
if let Some(record) = records.get_mut(&id) {
if record.can_be_downloaded() {
record.downloads += 1;
let file = tokio::fs::File::open(&record.file).await.unwrap();
return Ok(axum::response::Response::builder()
.header("Content-Type", "application/zip")
.body(StreamBody::new(ReaderStream::new(file)))
.unwrap()
.into_response());
}
}
}
// TODO: This....
state.remove_record(&id).await.unwrap();
Ok(Redirect::to("/404.html").into_response())
}

View file

@ -4,10 +4,13 @@ use std::{
sync::Arc,
};
use async_trait::async_trait;
use chrono::{DateTime, Duration, Utc};
use serde::{Deserialize, Serialize};
use tokio::sync::Mutex;
use crate::cache;
#[allow(dead_code)]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UploadRecord {
@ -30,6 +33,10 @@ impl UploadRecord {
dur_since_upload < Duration::days(3) && self.downloads < self.max_downloads
}
pub fn downloads_remaining(&self) -> u8 {
self.max_downloads - self.downloads
}
}
impl Default for UploadRecord {
@ -55,3 +62,36 @@ impl AppState {
}
}
}
#[async_trait]
pub trait AsyncRemoveRecord {
async fn remove_record(&mut self, id: &String) -> Result<(), std::io::Error>;
}
#[async_trait]
impl AsyncRemoveRecord for AppState {
async fn remove_record(&mut self, id: &String) -> Result<(), std::io::Error> {
let mut records = self.records.lock().await;
if let Some(record) = records.get_mut(id) {
tokio::fs::remove_file(&record.file).await?;
records.remove(id);
cache::write_to_cache(&records).await?;
}
Ok(())
}
}
#[async_trait]
impl AsyncRemoveRecord for HashMap<String, UploadRecord> {
async fn remove_record(&mut self, id: &String) -> Result<(), std::io::Error> {
if let Some(record) = self.get_mut(id) {
tokio::fs::remove_file(&record.file).await?;
self.remove(id);
cache::write_to_cache(&self).await?;
}
Ok(())
}
}

View file

@ -1,5 +1,5 @@
use futures::TryFutureExt;
use leptos::{component, view, Children, IntoAttribute, IntoView, Scope};
use leptos::{component, view, Children, IntoView, Scope};
use serde::Deserialize;
use crate::state::UploadRecord;
@ -91,11 +91,11 @@ pub fn LinkView(cx: Scope, id: String, record: UploadRecord) -> impl IntoView {
cx,
<div class="column-container">
<div class="link-wrapper">
<a id="link" href=format!("/download/{id}")>Download Now!</a>
<a id="link" hx-get="/download/{id}" href="/download/{id}">Download Now!</a>
</div>
<div class="link-wrapper">
You have {record.max_downloads - record.downloads} download{plural} remaining!
<div class="link-wrapper" hx-get="/link/{id}/remaining" hx-trigger="click from:#link delay:0.2s, every 10s" >
You have {record.downloads_remaining()} download{plural} remaining!
</div>
<button class="return-button" onclick="clipboard()">Copy to Clipboard</button>