diff options
author | 2025-05-08 10:18:07 +0100 | |
---|---|---|
committer | 2025-05-08 10:18:07 +0100 | |
commit | 5f1bc4f2807614dca1ac84136a5c355fde65543a (patch) | |
tree | c96e8074f112f7cf22f0182002ca40403a3720c3 /filamento/src | |
parent | 8e6a02f16c3e542492241f585a91fa0100ea7e33 (diff) | |
download | luz-5f1bc4f2807614dca1ac84136a5c355fde65543a.tar.gz luz-5f1bc4f2807614dca1ac84136a5c355fde65543a.tar.bz2 luz-5f1bc4f2807614dca1ac84136a5c355fde65543a.zip |
feat(filamento): OPFS FileStore implementation
Diffstat (limited to 'filamento/src')
-rw-r--r-- | filamento/src/chat.rs | 16 | ||||
-rw-r--r-- | filamento/src/db.rs | 12 | ||||
-rw-r--r-- | filamento/src/error.rs | 2 | ||||
-rw-r--r-- | filamento/src/files.rs | 25 |
4 files changed, 54 insertions, 1 deletions
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<rusqlite::types::ToSqlOutput<'_>> { 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 @@ -55,9 +55,21 @@ impl Db { } #[cfg(target_arch = "wasm32")] + pub async fn create_connect_and_migrate_memory() -> Result<Self, DatabaseOpenError> { + 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<Path>, ) -> Result<Self, DatabaseOpenError> { + // 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<Output = Result<(), Self::Err>> + 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<Output = Result<bool, Self::Err>>; + fn store( + &self, + name: &str, + data: &[u8], + ) -> impl std::future::Future<Output = Result<(), Self::Err>>; + fn delete(&self, name: &str) -> impl std::future::Future<Output = Result<(), Self::Err>>; +} + #[derive(Clone, Debug)] pub struct FilesMem { files: Arc<Mutex<HashMap<String, Vec<u8>>>>, @@ -38,8 +52,19 @@ impl FilesMem { files: Arc::new(Mutex::new(HashMap::new())), } } + + pub async fn get_file(&self, name: impl AsRef<str>) -> Option<Vec<u8>> { + 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; |