From 5f1bc4f2807614dca1ac84136a5c355fde65543a Mon Sep 17 00:00:00 2001 From: cel 🌸 Date: Thu, 8 May 2025 10:18:07 +0100 Subject: feat(filamento): OPFS FileStore implementation --- filamento/src/chat.rs | 16 ++++++++++++++++ filamento/src/db.rs | 12 ++++++++++++ filamento/src/error.rs | 2 +- filamento/src/files.rs | 25 +++++++++++++++++++++++++ 4 files changed, 54 insertions(+), 1 deletion(-) (limited to 'filamento/src') diff --git a/filamento/src/chat.rs b/filamento/src/chat.rs index bb0793f..5f58866 100644 --- a/filamento/src/chat.rs +++ b/filamento/src/chat.rs @@ -1,3 +1,5 @@ +use std::fmt::{Display, Write}; + use chrono::{DateTime, Utc}; use jid::JID; use rusqlite::{ @@ -34,6 +36,20 @@ pub enum Delivery { Queued, } +impl Display for Delivery { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Delivery::Sending => f.write_str("sending"), + Delivery::Written => f.write_str("written"), + Delivery::Sent => f.write_str("sent"), + Delivery::Delivered => f.write_str("delivered"), + Delivery::Read => f.write_str("read"), + Delivery::Failed => f.write_str("failed"), + Delivery::Queued => f.write_str("queued"), + } + } +} + impl ToSql for Delivery { fn to_sql(&self) -> rusqlite::Result> { Ok(match self { diff --git a/filamento/src/db.rs b/filamento/src/db.rs index d2c24a2..e560443 100644 --- a/filamento/src/db.rs +++ b/filamento/src/db.rs @@ -54,10 +54,22 @@ impl Db { }) } + #[cfg(target_arch = "wasm32")] + pub async fn create_connect_and_migrate_memory() -> Result { + let db = Connection::open("mem.db")?; + db.execute_batch(include_str!("../migrations/1.sql"))?; + Ok(Self { + db: Arc::new(Mutex::new(db)), + }) + } + #[cfg(target_arch = "wasm32")] pub async fn create_connect_and_migrate( path: impl AsRef, ) -> Result { + // rusqlite::ffi::install_opfs_sahpool(Some(&rusqlite::ffi::OpfsSAHPoolCfg::default()), true) + // .await + // .unwrap(); let db = Connection::open(path)?; db.execute_batch(include_str!("../migrations/1.sql"))?; Ok(Self { diff --git a/filamento/src/error.rs b/filamento/src/error.rs index bf28160..02f54ee 100644 --- a/filamento/src/error.rs +++ b/filamento/src/error.rs @@ -4,7 +4,7 @@ use base64::DecodeError; use image::ImageError; use jid::JID; use lampada::error::{ActorError, ReadError, WriteError}; -use stanza::client::{iq::Query, Stanza}; +use stanza::client::{Stanza, iq::Query}; use thiserror::Error; pub use lampada::error::CommandError; diff --git a/filamento/src/files.rs b/filamento/src/files.rs index 644f883..dcc9cd2 100644 --- a/filamento/src/files.rs +++ b/filamento/src/files.rs @@ -9,6 +9,7 @@ use std::{ use tokio::io; use tokio::sync::Mutex; +#[cfg(not(target_arch = "wasm32"))] pub trait FileStore { type Err: Clone + Send + Error; @@ -27,6 +28,19 @@ pub trait FileStore { ) -> impl std::future::Future> + std::marker::Send; } +#[cfg(target_arch = "wasm32")] +pub trait FileStore { + type Err: Clone + Send + Error; + + fn is_stored(&self, name: &str) -> impl std::future::Future>; + fn store( + &self, + name: &str, + data: &[u8], + ) -> impl std::future::Future>; + fn delete(&self, name: &str) -> impl std::future::Future>; +} + #[derive(Clone, Debug)] pub struct FilesMem { files: Arc>>>, @@ -38,8 +52,19 @@ impl FilesMem { files: Arc::new(Mutex::new(HashMap::new())), } } + + pub async fn get_file(&self, name: impl AsRef) -> Option> { + let name = name.as_ref(); + self.files.lock().await.get(name).cloned() + } } +#[cfg(all(feature = "opfs", target_arch = "wasm32"))] +pub mod opfs; + +#[cfg(all(feature = "opfs", target_arch = "wasm32"))] +pub use opfs::FilesOPFS; + impl FileStore for FilesMem { type Err = Infallible; -- cgit