nyazoom improves!
This commit is contained in:
parent
6ad91ff7e5
commit
6d939efbbf
7 changed files with 163 additions and 46 deletions
16
Cargo.lock
generated
16
Cargo.lock
generated
|
@ -1241,6 +1241,20 @@ dependencies = [
|
||||||
"uuid",
|
"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]]
|
[[package]]
|
||||||
name = "leptos_reactive"
|
name = "leptos_reactive"
|
||||||
version = "0.4.6"
|
version = "0.4.6"
|
||||||
|
@ -1524,6 +1538,7 @@ name = "nyazoom"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"async-bincode",
|
"async-bincode",
|
||||||
|
"async-trait",
|
||||||
"async_zip",
|
"async_zip",
|
||||||
"axum",
|
"axum",
|
||||||
"bincode",
|
"bincode",
|
||||||
|
@ -1531,6 +1546,7 @@ dependencies = [
|
||||||
"futures",
|
"futures",
|
||||||
"headers",
|
"headers",
|
||||||
"leptos",
|
"leptos",
|
||||||
|
"leptos_meta",
|
||||||
"leptos_router",
|
"leptos_router",
|
||||||
"rand",
|
"rand",
|
||||||
"reqwest",
|
"reqwest",
|
||||||
|
|
|
@ -8,6 +8,7 @@ edition = "2021"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
async-bincode = { version = "0.7.0", features = ["tokio"] }
|
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"] }
|
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"] }
|
axum = { version = "0.6.12", features = ["multipart", "http2", "headers", "macros", "original-uri"] }
|
||||||
bincode = "1.3.3"
|
bincode = "1.3.3"
|
||||||
|
@ -15,6 +16,7 @@ chrono = { version = "0.4.24", features = ["serde"] }
|
||||||
futures = "0.3.28"
|
futures = "0.3.28"
|
||||||
headers = "0.3.8"
|
headers = "0.3.8"
|
||||||
leptos = { version = "0.4.6", features = ["ssr", "nightly", "tracing", "default-tls"] }
|
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"] }
|
leptos_router = { version = "0.4.6", features = ["ssr"] }
|
||||||
rand = { version = "0.8.5", features = ["small_rng"] }
|
rand = { version = "0.8.5", features = ["small_rng"] }
|
||||||
reqwest = { version = "0.11.18", features = ["json", "native-tls", "blocking"] }
|
reqwest = { version = "0.11.18", features = ["json", "native-tls", "blocking"] }
|
||||||
|
|
1
dist/css/link.css
vendored
1
dist/css/link.css
vendored
|
@ -16,6 +16,7 @@ body {
|
||||||
padding: 1.5em;
|
padding: 1.5em;
|
||||||
border-radius: 1em;
|
border-radius: 1em;
|
||||||
border: 1px solid #25283d;
|
border: 1px solid #25283d;
|
||||||
|
list-style: none;
|
||||||
}
|
}
|
||||||
|
|
||||||
.return-button {
|
.return-button {
|
||||||
|
|
1
dist/css/main.css
vendored
1
dist/css/main.css
vendored
|
@ -25,6 +25,7 @@ body {
|
||||||
justify-content: center;
|
justify-content: center;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
.cat-img {
|
.cat-img {
|
||||||
width: 250px;
|
width: 250px;
|
||||||
height: 250px;
|
height: 250px;
|
||||||
|
|
99
src/main.rs
99
src/main.rs
|
@ -6,12 +6,13 @@ use axum::{
|
||||||
http::{Request, Response, StatusCode},
|
http::{Request, Response, StatusCode},
|
||||||
middleware::{self, Next},
|
middleware::{self, Next},
|
||||||
response::{Html, IntoResponse, Redirect},
|
response::{Html, IntoResponse, Redirect},
|
||||||
routing::{get, post},
|
routing::{delete, get, post},
|
||||||
Json, Router, TypedHeader,
|
Json, Router, TypedHeader,
|
||||||
};
|
};
|
||||||
|
|
||||||
use futures::TryStreamExt;
|
use futures::TryStreamExt;
|
||||||
|
|
||||||
|
use headers::HeaderMap;
|
||||||
use leptos::IntoView;
|
use leptos::IntoView;
|
||||||
use nyazoom_headers::ForwardedFor;
|
use nyazoom_headers::ForwardedFor;
|
||||||
|
|
||||||
|
@ -36,7 +37,8 @@ mod views;
|
||||||
|
|
||||||
use state::{AppState, UploadRecord};
|
use state::{AppState, UploadRecord};
|
||||||
|
|
||||||
use crate::views::{DownloadLinkPage, LinkView, Welcome};
|
use crate::state::AsyncRemoveRecord;
|
||||||
|
use crate::views::{DownloadLinkPage, HtmxPage, LinkView, Welcome};
|
||||||
|
|
||||||
pub mod error {
|
pub mod error {
|
||||||
use std::io::{Error, ErrorKind};
|
use std::io::{Error, ErrorKind};
|
||||||
|
@ -74,10 +76,8 @@ async fn main() -> io::Result<()> {
|
||||||
|
|
||||||
for (key, record) in records.clone().into_iter() {
|
for (key, record) in records.clone().into_iter() {
|
||||||
if !record.can_be_downloaded() {
|
if !record.can_be_downloaded() {
|
||||||
tracing::info!("{:?} should be culled", record);
|
tracing::info!("culling: {:?}", record);
|
||||||
let _ = tokio::fs::remove_file(&record.file).await;
|
records.remove_record(&key).await.unwrap();
|
||||||
records.remove(key.as_str());
|
|
||||||
cache::write_to_cache(&records).await.unwrap();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -92,6 +92,8 @@ async fn main() -> io::Result<()> {
|
||||||
.route("/records/links", get(records_links))
|
.route("/records/links", get(records_links))
|
||||||
.route("/download/:id", get(download))
|
.route("/download/:id", get(download))
|
||||||
.route("/link/:id", get(link))
|
.route("/link/:id", get(link))
|
||||||
|
.route("/link/:id", delete(link_delete))
|
||||||
|
.route("/link/:id/remaining", get(remaining))
|
||||||
.layer(DefaultBodyLimit::disable())
|
.layer(DefaultBodyLimit::disable())
|
||||||
.layer(RequestBodyLimitLayer::new(
|
.layer(RequestBodyLimitLayer::new(
|
||||||
10 * 1024 * 1024 * 1024, // 10GiB
|
10 * 1024 * 1024 * 1024, // 10GiB
|
||||||
|
@ -112,6 +114,24 @@ async fn main() -> io::Result<()> {
|
||||||
Ok(())
|
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 {
|
async fn welcome() -> impl IntoResponse {
|
||||||
let cat_fact = views::get_cat_fact().await;
|
let cat_fact = views::get_cat_fact().await;
|
||||||
Html(leptos::ssr::render_to_string(move |cx| {
|
Html(leptos::ssr::render_to_string(move |cx| {
|
||||||
|
@ -123,25 +143,39 @@ async fn records(State(state): State<AppState>) -> impl IntoResponse {
|
||||||
Json(state.records.lock().await.clone())
|
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 {
|
async fn records_links(State(state): State<AppState>) -> impl IntoResponse {
|
||||||
let records = state.records.lock().await.clone();
|
let records = state.records.lock().await.clone();
|
||||||
Html(leptos::ssr::render_to_string(move |cx| {
|
Html(leptos::ssr::render_to_string(move |cx| {
|
||||||
leptos::view! { cx,
|
leptos::view! { cx,
|
||||||
|
<HtmxPage>
|
||||||
|
<div class="form-wrapper">
|
||||||
|
<div class="column-container">
|
||||||
<ul>
|
<ul>
|
||||||
{records
|
{records.keys().map(|key| leptos::view! { cx,
|
||||||
.iter()
|
<li class="link-wrapper">
|
||||||
.map(|(key, _)|
|
<a href="/link/{key}">{key}</a>
|
||||||
leptos::view! { cx, <li><a href="/link/{key}">{key}</a></li> })
|
<button style="margin-left: 1em;"
|
||||||
|
hx-target="closest .link-wrapper"
|
||||||
|
hx-swap="outerHTML"
|
||||||
|
hx-delete="/link/{key}">X</button>
|
||||||
|
</li>
|
||||||
|
})
|
||||||
.collect::<Vec<_>>()}
|
.collect::<Vec<_>>()}
|
||||||
</ul>
|
</ul>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</HtmxPage>
|
||||||
}
|
}
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn link(
|
async fn link(
|
||||||
axum::extract::Path(id): axum::extract::Path<String>,
|
axum::extract::Path(id): axum::extract::Path<String>,
|
||||||
State(state): State<AppState>,
|
State(mut state): State<AppState>,
|
||||||
) -> Result<Html<String>, Redirect> {
|
) -> 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 let Some(record) = records.get_mut(&id) {
|
||||||
|
@ -152,16 +186,28 @@ async fn link(
|
||||||
leptos::view! { cx, <DownloadLinkPage id=id record=record /> }
|
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();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: This....
|
||||||
|
state.remove_record(&id).await.unwrap();
|
||||||
|
|
||||||
Err(Redirect::to(&format!("/404.html")))
|
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>(
|
async fn log_source<B>(
|
||||||
ConnectInfo(addr): ConnectInfo<SocketAddr>,
|
ConnectInfo(addr): ConnectInfo<SocketAddr>,
|
||||||
forwarded_for: Option<TypedHeader<ForwardedFor>>,
|
forwarded_for: Option<TypedHeader<ForwardedFor>>,
|
||||||
|
@ -245,9 +291,20 @@ async fn upload_to_zip(
|
||||||
|
|
||||||
async fn download(
|
async fn download(
|
||||||
axum::extract::Path(id): axum::extract::Path<String>,
|
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)> {
|
) -> Result<axum::response::Response, (StatusCode, String)> {
|
||||||
|
{
|
||||||
let mut records = state.records.lock().await;
|
let mut records = state.records.lock().await;
|
||||||
|
tracing::info!("{headers:?}");
|
||||||
|
if headers.get("hx-request").is_some() {
|
||||||
|
return Ok(axum::http::Response::builder()
|
||||||
|
.header("HX-Redirect", format!("/download/{id}"))
|
||||||
|
.status(204)
|
||||||
|
.body("".to_owned())
|
||||||
|
.unwrap()
|
||||||
|
.into_response());
|
||||||
|
}
|
||||||
|
|
||||||
if let Some(record) = records.get_mut(&id) {
|
if let Some(record) = records.get_mut(&id) {
|
||||||
if record.can_be_downloaded() {
|
if record.can_be_downloaded() {
|
||||||
|
@ -255,17 +312,17 @@ async fn download(
|
||||||
|
|
||||||
let file = tokio::fs::File::open(&record.file).await.unwrap();
|
let file = tokio::fs::File::open(&record.file).await.unwrap();
|
||||||
|
|
||||||
return Ok(axum::http::Response::builder()
|
return Ok(axum::response::Response::builder()
|
||||||
.header("Content-Type", "application/zip")
|
.header("Content-Type", "application/zip")
|
||||||
.body(StreamBody::new(ReaderStream::new(file)))
|
.body(StreamBody::new(ReaderStream::new(file)))
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.into_response());
|
.into_response());
|
||||||
} else {
|
}
|
||||||
let _ = tokio::fs::remove_file(&record.file).await;
|
|
||||||
records.remove(&id);
|
|
||||||
cache::write_to_cache(&records).await.unwrap();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: This....
|
||||||
|
state.remove_record(&id).await.unwrap();
|
||||||
|
|
||||||
Ok(Redirect::to("/404.html").into_response())
|
Ok(Redirect::to("/404.html").into_response())
|
||||||
}
|
}
|
||||||
|
|
40
src/state.rs
40
src/state.rs
|
@ -4,10 +4,13 @@ use std::{
|
||||||
sync::Arc,
|
sync::Arc,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use async_trait::async_trait;
|
||||||
use chrono::{DateTime, Duration, Utc};
|
use chrono::{DateTime, Duration, Utc};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use tokio::sync::Mutex;
|
use tokio::sync::Mutex;
|
||||||
|
|
||||||
|
use crate::cache;
|
||||||
|
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
pub struct UploadRecord {
|
pub struct UploadRecord {
|
||||||
|
@ -30,6 +33,10 @@ impl UploadRecord {
|
||||||
|
|
||||||
dur_since_upload < Duration::days(3) && self.downloads < self.max_downloads
|
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 {
|
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(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
use futures::TryFutureExt;
|
use futures::TryFutureExt;
|
||||||
use leptos::{component, view, Children, IntoAttribute, IntoView, Scope};
|
use leptos::{component, view, Children, IntoView, Scope};
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
|
||||||
use crate::state::UploadRecord;
|
use crate::state::UploadRecord;
|
||||||
|
@ -91,11 +91,11 @@ pub fn LinkView(cx: Scope, id: String, record: UploadRecord) -> impl IntoView {
|
||||||
cx,
|
cx,
|
||||||
<div class="column-container">
|
<div class="column-container">
|
||||||
<div class="link-wrapper">
|
<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>
|
||||||
|
|
||||||
<div class="link-wrapper">
|
<div class="link-wrapper" hx-get="/link/{id}/remaining" hx-trigger="click from:#link delay:0.2s, every 10s" >
|
||||||
You have {record.max_downloads - record.downloads} download{plural} remaining!
|
You have {record.downloads_remaining()} download{plural} remaining!
|
||||||
</div>
|
</div>
|
||||||
<button class="return-button" onclick="clipboard()">Copy to Clipboard</button>
|
<button class="return-button" onclick="clipboard()">Copy to Clipboard</button>
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue