Compare commits

...

2 Commits

Author SHA1 Message Date
Zynh Ludwig 7128b9e304 sweeper module 2024-08-28 18:57:36 -07:00
Zynh Ludwig 31bfd4d126 formatting 2024-08-28 18:55:29 -07:00
6 changed files with 86 additions and 37 deletions

1
.rustfmt.toml Normal file
View File

@ -0,0 +1 @@
edition = "2021"

2
leptosfmt.toml Normal file
View File

@ -0,0 +1,2 @@
[attr_values]
class = "Tailwind" # "Tailwind" is the only attribute value formatter available for now

2
rust-analyzer.toml Normal file
View File

@ -0,0 +1,2 @@
[rustfmt]
overrideCommand = ["leptosfmt", "--stdin", "--rustfmt"]

View File

@ -32,6 +32,7 @@ mod cache;
mod nyazoom_headers; mod nyazoom_headers;
pub mod ssr; pub mod ssr;
mod state; mod state;
mod sweeper;
mod util; mod util;
mod views; mod views;
@ -64,25 +65,7 @@ async fn main() -> io::Result<()> {
let state = cache::fetch_cache().await; let state = cache::fetch_cache().await;
// Spawn a repeating task that will clean files periodically let _ = sweeper::spawn_sweeper();
tokio::spawn({
let state = state.clone();
async move {
loop {
tokio::time::sleep(Duration::from_secs(15 * 60)).await;
tracing::info!("Cleaning Sweep!");
let mut records = state.records.lock().await;
for (key, record) in records.clone().into_iter() {
if !record.can_be_downloaded() {
tracing::info!("culling: {:?}", record);
records.remove_record(&key).await.unwrap();
}
}
}
}
});
// Router Setup // Router Setup
let app = Router::new() let app = Router::new()
@ -137,9 +120,7 @@ async fn remaining(
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(ssr::render(move || { Html(ssr::render(move || {
leptos::view! { leptos::view! { <Welcome fact=cat_fact /> }
<Welcome fact=cat_fact />
}
})) }))
} }
@ -157,14 +138,22 @@ async fn records_links(State(state): State<AppState>) -> impl IntoResponse {
<div class="form-wrapper"> <div class="form-wrapper">
<div class="column-container"> <div class="column-container">
<ul> <ul>
{records.keys().map(|key| leptos::view! { {records
.keys()
.map(|key| {
leptos::view! {
<li class="link-wrapper"> <li class="link-wrapper">
<a href="/link/{key}">{key}</a> <a href="/link/{key}">{key}</a>
<button style="margin-left: 1em;" <button
style="margin-left: 1em;"
hx-target="closest .link-wrapper" hx-target="closest .link-wrapper"
hx-swap="outerHTML" hx-swap="outerHTML"
hx-delete="/link/{key}">X</button> hx-delete="/link/{key}"
>
X
</button>
</li> </li>
}
}) })
.collect::<Vec<_>>()} .collect::<Vec<_>>()}
</ul> </ul>

23
src/sweeper.rs Normal file
View File

@ -0,0 +1,23 @@
use tokio::task::JoinHandle;
pub fn spawn_sweeper() -> JoinHandle<!> {
// Spawn a repeating task that will clean files periodically
tokio::spawn({
let state = state.clone();
async move {
loop {
tokio::time::sleep(Duration::from_secs(15 * 60)).await;
tracing::info!("Cleaning Sweep!");
let mut records = state.records.lock().await;
for (key, record) in records.clone().into_iter() {
if !record.can_be_downloaded() {
tracing::info!("culling: {:?}", record);
records.remove_record(&key).await.unwrap();
}
}
}
}
})
}

View File

@ -33,11 +33,26 @@ pub fn Welcome(fact: String) -> impl IntoView {
#[component] #[component]
pub fn WelcomeView(fact: String) -> impl IntoView { pub fn WelcomeView(fact: String) -> impl IntoView {
view! { view! {
<form id="form" hx-swap="outerHTML" hx-post="/upload" hx-encoding="multipart/form-data" class="column-container"> <form
id="form"
hx-swap="outerHTML"
hx-post="/upload"
hx-encoding="multipart/form-data"
class="column-container"
>
<div class="cat-img-wrapper"> <div class="cat-img-wrapper">
<img class="cat-img" src="https://api.thecatapi.com/v1/images/search?size=small&format=src" /> <img
class="cat-img"
src="https://api.thecatapi.com/v1/images/search?size=small&format=src"
/>
</div> </div>
<input type="file" id="file" name="file" data-multiple-caption="{{count}} files selected" multiple /> <input
type="file"
id="file"
name="file"
data-multiple-caption="{{count}} files selected"
multiple
/>
<label for="file">Select Files</label> <label for="file">Select Files</label>
<input type="submit" value="Get Link~" /> <input type="submit" value="Get Link~" />
@ -72,7 +87,11 @@ pub fn HtmxPage(children: Children) -> impl IntoView {
<link href="/css/link.css" rel="stylesheet" /> <link href="/css/link.css" rel="stylesheet" />
<script src="/scripts/file_label.js" /> <script src="/scripts/file_label.js" />
<script src="/scripts/link.js" /> <script src="/scripts/link.js" />
<script src="https://unpkg.com/htmx.org@1.9.4" integrity="sha384-zUfuhFKKZCbHTY6aRR46gxiqszMk5tcHjsVFxnUo8VMus4kHGVdIYVbOYYNlKmHV" crossorigin="anonymous"></script> <script
src="https://unpkg.com/htmx.org@1.9.4"
integrity="sha384-zUfuhFKKZCbHTY6aRR46gxiqszMk5tcHjsVFxnUo8VMus4kHGVdIYVbOYYNlKmHV"
crossorigin="anonymous"
></script>
</head> </head>
<body> <body>
@ -89,16 +108,29 @@ pub fn LinkView(id: String, record: UploadRecord) -> impl IntoView {
view! { view! {
<div class="column-container"> <div class="column-container">
<div class="link-wrapper"> <div class="link-wrapper">
<a id="link" href="/download/{id}">Download Now!</a> <a id="link" href="/download/{id}">
Download Now!
</a>
</div> </div>
<div class="link-wrapper" hx-get="/link/{id}/remaining" hx-trigger="click from:#link delay:0.2s, every 10s" > <div
You have {record.downloads_remaining()} download{plural} remaining! 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> </div>
<button class="return-button" onclick="clipboard()">Copy to Clipboard</button> <button class="return-button" onclick="clipboard()">
Copy to Clipboard
</button>
<a href="/" class="return-button">
<a href="/" class="return-button">Return to home</a> Return to home
</a>
</div> </div>
} }
} }