feat: add basic tauri client app

This commit is contained in:
Lin Onetwo
2023-01-04 17:16:26 +08:00
parent 87451a19bb
commit 64ca6a6b35
57 changed files with 12747 additions and 54 deletions
+68
View File
@@ -0,0 +1,68 @@
use bytes::Bytes;
use futures::{
stream::{self},
StreamExt,
};
use ipc_types::blob::{GetBlob, PutBlob};
use jwst::BlobStorage;
use crate::state::AppState;
#[tauri::command]
pub async fn put_blob<'s>(
state: tauri::State<'s, AppState>,
parameters: PutBlob,
) -> Result<String, String> {
let blob_storage = &state.0.lock().await.blob_storage;
if let Ok(path) = blob_storage
.put_blob(
// TODO: ask octobase to accept blob directly or wrap/await tauri command to create a real stream, so we don't need to construct stream manually
Some(parameters.workspace_id.to_string()),
stream::iter::<Vec<Bytes>>(vec![Bytes::from(parameters.blob)]),
)
.await
{
Ok(path)
} else {
Err("Failed to create".to_string())
}
}
#[tauri::command]
pub async fn get_blob<'s>(
state: tauri::State<'s, AppState>,
parameters: GetBlob,
) -> Result<Vec<u8>, String> {
let GetBlob { workspace_id, id } = parameters;
// TODO: check user permission? Or just assume there will only be one user
let blob_storage = &state.0.lock().await.blob_storage;
if let Ok(mut file_stream) = blob_storage.get_blob(Some(workspace_id.to_string()), id.clone()).await {
// Read all of the chunks into a vector.
let mut stream_contents = Vec::new();
let mut error_message = "".to_string();
while let Some(chunk) = file_stream.next().await {
match chunk {
Ok(chunk_bytes) => stream_contents.extend_from_slice(&chunk_bytes),
Err(err) => {
error_message = format!(
"Failed to read blob file {}/{} from stream, error: {}",
workspace_id.to_string(),
id,
err
);
}
}
}
if error_message.len() > 0 {
return Err(error_message);
}
Ok(stream_contents)
} else {
Err(format!(
"Failed to read blob file {}/{} ",
workspace_id.to_string(),
id
))
}
}
@@ -0,0 +1,38 @@
use ipc_types::{document::YDocumentUpdate, workspace::CreateWorkspace};
use jwst::{DocStorage, Workspace};
use lib0::any::Any;
use crate::state::AppState;
#[tauri::command]
pub async fn create_workspace<'s>(
state: tauri::State<'s, AppState>,
parameters: CreateWorkspace,
) -> Result<bool, String> {
let workspace = Workspace::new(parameters.id.to_string());
workspace.with_trx(|mut workspace_transaction| {
// TODO: why this Any here?
workspace_transaction.set_metadata("name", Any::String(parameters.name.into_boxed_str()));
workspace_transaction.set_metadata(
"avatar",
Any::String(parameters.avatar.clone().into_boxed_str()),
);
});
if let Err(error_message) = &state
.0
.lock()
.await
.doc_storage
.write_doc(parameters.id, workspace.doc())
.await
{
Err(error_message.to_string())
} else {
Ok(true)
}
}
#[tauri::command]
pub fn update_y_document(parameters: YDocumentUpdate) -> Result<bool, String> {
Ok(true)
}