From 93dbab0e9711d3c19cb6661d0370ff74280eb527 Mon Sep 17 00:00:00 2001 From: Orual Date: Sat, 1 Nov 2025 20:05:18 -0400 Subject: [PATCH] cache fill server function --- crates/weaver-server/src/components/entry.rs | 50 +++++++++++++++++--- crates/weaver-server/src/fetch.rs | 4 +- crates/weaver-server/src/main.rs | 17 ++++--- 3 files changed, 54 insertions(+), 17 deletions(-) diff --git a/crates/weaver-server/src/components/entry.rs b/crates/weaver-server/src/components/entry.rs index a07b0e3..2f443ab 100644 --- a/crates/weaver-server/src/components/entry.rs +++ b/crates/weaver-server/src/components/entry.rs @@ -1,19 +1,50 @@ #![allow(non_snake_case)] -use crate::fetch; -use dioxus::prelude::*; -use jacquard::{smol_str::SmolStr, types::string::AtIdentifier, CowStr}; +#[allow(unused_imports)] +use crate::{blobcache::BlobCache, fetch}; +#[allow(unused_imports)] +use dioxus::{fullstack::extract::Extension, CapturedError}; +use dioxus::{ + fullstack::{get_server_url, reqwest}, + prelude::*, +}; +use jacquard::smol_str::ToSmolStr; +#[allow(unused_imports)] +use jacquard::{ + smol_str::SmolStr, + types::{cid::Cid, string::AtIdentifier}, +}; +#[allow(unused_imports)] +use std::sync::Arc; use weaver_api::sh_weaver::notebook::{entry, BookEntryView}; #[component] pub fn Entry(ident: AtIdentifier<'static>, book_title: SmolStr, title: SmolStr) -> Element { let entry = use_resource(use_reactive!(|(ident, book_title, title)| async move { let fetcher = use_context::(); - fetcher - .get_entry(ident, book_title, title) + let entry = fetcher + .get_entry(ident.clone(), book_title, title) .await .ok() - .flatten() + .flatten(); + if let Some(entry) = &entry { + let entry = &entry.1; + if let Some(embeds) = &entry.embeds { + if let Some(images) = &embeds.images { + for image in &images.images { + let cid = image.image.blob().cid(); + cache_blob( + ident.to_smolstr(), + cid.to_smolstr(), + image.name.as_ref().map(|n| n.to_smolstr()), + ) + .await + .ok(); + } + } + } + } + entry })); rsx! { @@ -63,3 +94,10 @@ pub fn EntryMarkdown(props: EntryMarkdownProps) -> Element { } } } + +#[put("/cache/{ident}/{cid}?name", cache: Extension>)] +pub async fn cache_blob(ident: SmolStr, cid: SmolStr, name: Option) -> Result<()> { + let ident = AtIdentifier::new_owned(ident)?; + let cid = Cid::new_owned(cid.as_bytes())?; + cache.cache(ident, cid, name).await +} diff --git a/crates/weaver-server/src/fetch.rs b/crates/weaver-server/src/fetch.rs index 0af9aec..4c3d5d8 100644 --- a/crates/weaver-server/src/fetch.rs +++ b/crates/weaver-server/src/fetch.rs @@ -1,4 +1,4 @@ -use dioxus::{CapturedError, Result}; +use dioxus::Result; use jacquard::{client::BasicClient, smol_str::SmolStr, types::ident::AtIdentifier}; use std::{ sync::{Arc, Mutex}, @@ -8,7 +8,7 @@ use weaver_api::{ com_atproto::repo::strong_ref::StrongRef, sh_weaver::notebook::{entry::Entry, BookEntryView, NotebookView}, }; -use weaver_common::view::{entry_by_title, fetch_entry_view, notebook_by_title, view_entry}; +use weaver_common::view::{entry_by_title, notebook_by_title}; #[derive(Clone)] pub struct CachedFetcher { diff --git a/crates/weaver-server/src/main.rs b/crates/weaver-server/src/main.rs index ca14d64..17808d2 100644 --- a/crates/weaver-server/src/main.rs +++ b/crates/weaver-server/src/main.rs @@ -2,9 +2,7 @@ // need dioxus use components::{Entry, Repository, RepositoryIndex}; use dioxus::{fullstack::FullstackContext, prelude::*}; -use jacquard::{ - client::BasicClient, smol_str::SmolStr, types::did::Did, types::string::AtIdentifier, CowStr, -}; +use jacquard::{client::BasicClient, smol_str::SmolStr, types::string::AtIdentifier}; use std::sync::Arc; use views::{Home, Navbar, Notebook, NotebookIndex, NotebookPage}; @@ -32,11 +30,11 @@ enum Route { Home {}, #[layout(ErrorLayout)] #[nest("/:ident")] - #[layout(components::Repository)] + #[layout(Repository)] #[route("/")] RepositoryIndex { ident: AtIdentifier<'static> }, #[nest("/:book_title")] - #[layout(views::Notebook)] + #[layout(Notebook)] #[route("/")] NotebookIndex { ident: AtIdentifier<'static>, book_title: SmolStr }, #[route("/:title")] @@ -60,6 +58,7 @@ fn main() { |mut req: Request, next: Next| async move { // Attach some extra state to the request + use crate::blobcache::BlobCache; use crate::fetch::CachedFetcher; use std::convert::Infallible; use std::sync::Arc; @@ -67,6 +66,10 @@ fn main() { .insert(Arc::new(CachedFetcher::new(Arc::new( BasicClient::unauthenticated(), )))); + req.extensions_mut() + .insert(Arc::new(BlobCache::new(Arc::new( + BasicClient::unauthenticated(), + )))); // And then return the response with `next.run() Ok::<_, Infallible>(next.run(req).await) @@ -121,7 +124,3 @@ fn ErrorLayout() -> Element { } } } - -struct Config { - did: Option>, -} -- knotmirror 0.0.1