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;
pub mod ssr;
mod state;
mod sweeper;
mod util;
mod views;
@ -64,25 +65,7 @@ async fn main() -> io::Result<()> {
let state = cache::fetch_cache().await;
// 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();
}
}
}
}
});
let _ = sweeper::spawn_sweeper();
// Router Setup
let app = Router::new()
@ -137,9 +120,7 @@ async fn remaining(
async fn welcome() -> impl IntoResponse {
let cat_fact = views::get_cat_fact().await;
Html(ssr::render(move || {
leptos::view! {
<Welcome fact=cat_fact />
}
leptos::view! { <Welcome fact=cat_fact /> }
}))
}
@ -157,15 +138,23 @@ async fn records_links(State(state): State<AppState>) -> impl IntoResponse {
<div class="form-wrapper">
<div class="column-container">
<ul>
{records.keys().map(|key| leptos::view! {
{records
.keys()
.map(|key| {
leptos::view! {
<li class="link-wrapper">
<a href="/link/{key}">{key}</a>
<button style="margin-left: 1em;"
<button
style="margin-left: 1em;"
hx-target="closest .link-wrapper"
hx-swap="outerHTML"
hx-delete="/link/{key}">X</button>
hx-delete="/link/{key}"
>
X
</button>
</li>
})
}
})
.collect::<Vec<_>>()}
</ul>
</div>
@ -190,7 +179,7 @@ async fn link(
return Ok(Html(ssr::render({
let record = record.clone();
|| {
leptos::view! { <DownloadLinkPage id=id record=record /> }
leptos::view! { <DownloadLinkPage id=id record=record /> }
}
})));
}

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]
pub fn WelcomeView(fact: String) -> impl IntoView {
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">
<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>
<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>
<input type="submit" value="Get Link~" />
@ -72,7 +87,11 @@ pub fn HtmxPage(children: Children) -> impl IntoView {
<link href="/css/link.css" rel="stylesheet" />
<script src="/scripts/file_label.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>
<body>
@ -89,16 +108,29 @@ pub fn LinkView(id: String, record: UploadRecord) -> impl IntoView {
view! {
<div class="column-container">
<div class="link-wrapper">
<a id="link" href="/download/{id}">Download Now!</a>
<a id="link" href="/download/{id}">
Download Now!
</a>
</div>
<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
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>
<button class="return-button" onclick="clipboard()">
Copy to Clipboard
</button>
<a href="/" class="return-button">Return to home</a>
<a href="/" class="return-button">
Return to home
</a>
</div>
}
}