aboutsummaryrefslogtreecommitdiffstats
path: root/filamento/src/files.rs
diff options
context:
space:
mode:
authorLibravatar cel 🌸 <cel@bunny.garden>2025-05-08 10:18:07 +0100
committerLibravatar cel 🌸 <cel@bunny.garden>2025-05-08 10:18:07 +0100
commit5f1bc4f2807614dca1ac84136a5c355fde65543a (patch)
treec96e8074f112f7cf22f0182002ca40403a3720c3 /filamento/src/files.rs
parent8e6a02f16c3e542492241f585a91fa0100ea7e33 (diff)
downloadluz-5f1bc4f2807614dca1ac84136a5c355fde65543a.tar.gz
luz-5f1bc4f2807614dca1ac84136a5c355fde65543a.tar.bz2
luz-5f1bc4f2807614dca1ac84136a5c355fde65543a.zip
feat(filamento): OPFS FileStore implementation
Diffstat (limited to '')
-rw-r--r--filamento/src/files.rs25
1 files changed, 25 insertions, 0 deletions
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;