2023-11-14 09:21:09 +00:00
|
|
|
use std::sync::Arc;
|
2023-07-29 01:46:24 +00:00
|
|
|
use std::sync::Weak;
|
2023-05-10 05:27:50 +00:00
|
|
|
|
2024-04-15 06:50:28 +00:00
|
|
|
use collab::core::collab::{DataSource, MutexCollab};
|
2023-12-27 03:42:39 +00:00
|
|
|
use collab::core::origin::CollabOrigin;
|
2024-04-24 06:38:47 +00:00
|
|
|
use collab::entity::EncodedCollab;
|
2023-12-27 03:42:39 +00:00
|
|
|
use collab::preclude::Collab;
|
2023-06-03 05:55:43 +00:00
|
|
|
use collab_document::blocks::DocumentData;
|
2023-07-05 12:57:09 +00:00
|
|
|
use collab_document::document::Document;
|
2024-03-28 09:46:31 +00:00
|
|
|
use collab_document::document_awareness::DocumentAwarenessState;
|
|
|
|
use collab_document::document_awareness::DocumentAwarenessUser;
|
2024-01-04 00:02:12 +00:00
|
|
|
use collab_document::document_data::default_document_data;
|
2023-10-10 11:05:55 +00:00
|
|
|
use collab_entity::CollabType;
|
2024-01-22 05:34:15 +00:00
|
|
|
use collab_plugins::CollabKVDB;
|
2024-03-30 08:28:24 +00:00
|
|
|
use dashmap::DashMap;
|
2024-01-20 15:16:18 +00:00
|
|
|
use flowy_storage::object_from_disk;
|
2024-03-28 09:46:31 +00:00
|
|
|
use lib_infra::util::timestamp;
|
2024-01-16 18:59:15 +00:00
|
|
|
use tokio::io::AsyncWriteExt;
|
2024-03-23 23:31:41 +00:00
|
|
|
use tracing::{error, trace};
|
2023-11-05 06:00:24 +00:00
|
|
|
use tracing::{event, instrument};
|
2023-04-13 10:53:51 +00:00
|
|
|
|
2023-12-27 03:42:39 +00:00
|
|
|
use collab_integrate::collab_builder::{AppFlowyCollabBuilder, CollabBuilderConfig};
|
2024-01-22 05:34:15 +00:00
|
|
|
use collab_integrate::CollabPersistenceConfig;
|
2024-01-11 06:42:03 +00:00
|
|
|
use flowy_document_pub::cloud::DocumentCloudService;
|
2023-12-27 03:42:39 +00:00
|
|
|
use flowy_error::{internal_error, ErrorCode, FlowyError, FlowyResult};
|
2024-01-16 18:59:15 +00:00
|
|
|
use flowy_storage::ObjectStorageService;
|
2024-01-29 21:36:27 +00:00
|
|
|
use lib_dispatch::prelude::af_spawn;
|
2023-04-28 06:08:53 +00:00
|
|
|
|
2023-08-05 07:02:05 +00:00
|
|
|
use crate::document::MutexDocument;
|
2024-03-28 09:46:31 +00:00
|
|
|
use crate::entities::UpdateDocumentAwarenessStatePB;
|
2024-01-07 03:12:05 +00:00
|
|
|
use crate::entities::{
|
|
|
|
DocumentSnapshotData, DocumentSnapshotMeta, DocumentSnapshotMetaPB, DocumentSnapshotPB,
|
|
|
|
};
|
2023-10-02 07:12:24 +00:00
|
|
|
use crate::reminder::DocumentReminderAction;
|
2023-04-13 10:53:51 +00:00
|
|
|
|
2024-01-07 03:12:05 +00:00
|
|
|
pub trait DocumentUserService: Send + Sync {
|
2023-07-29 01:46:24 +00:00
|
|
|
fn user_id(&self) -> Result<i64, FlowyError>;
|
2024-03-28 09:46:31 +00:00
|
|
|
fn device_id(&self) -> Result<String, FlowyError>;
|
2023-10-23 03:43:31 +00:00
|
|
|
fn workspace_id(&self) -> Result<String, FlowyError>;
|
2024-01-04 16:05:38 +00:00
|
|
|
fn collab_db(&self, uid: i64) -> Result<Weak<CollabKVDB>, FlowyError>;
|
2023-07-29 01:46:24 +00:00
|
|
|
}
|
|
|
|
|
2024-01-07 03:12:05 +00:00
|
|
|
pub trait DocumentSnapshotService: Send + Sync {
|
|
|
|
fn get_document_snapshot_metas(
|
|
|
|
&self,
|
|
|
|
document_id: &str,
|
|
|
|
) -> FlowyResult<Vec<DocumentSnapshotMeta>>;
|
|
|
|
fn get_document_snapshot(&self, snapshot_id: &str) -> FlowyResult<DocumentSnapshotData>;
|
|
|
|
}
|
|
|
|
|
2023-04-13 10:53:51 +00:00
|
|
|
pub struct DocumentManager {
|
2024-01-07 03:12:05 +00:00
|
|
|
pub user_service: Arc<dyn DocumentUserService>,
|
2023-05-15 14:16:05 +00:00
|
|
|
collab_builder: Arc<AppFlowyCollabBuilder>,
|
2024-03-30 08:28:24 +00:00
|
|
|
documents: Arc<DashMap<String, Arc<MutexDocument>>>,
|
|
|
|
removing_documents: Arc<DashMap<String, Arc<MutexDocument>>>,
|
2023-07-05 12:57:09 +00:00
|
|
|
cloud_service: Arc<dyn DocumentCloudService>,
|
2024-01-16 18:59:15 +00:00
|
|
|
storage_service: Weak<dyn ObjectStorageService>,
|
2024-01-07 03:12:05 +00:00
|
|
|
snapshot_service: Arc<dyn DocumentSnapshotService>,
|
2023-04-13 10:53:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl DocumentManager {
|
2023-07-05 12:57:09 +00:00
|
|
|
pub fn new(
|
2024-01-07 03:12:05 +00:00
|
|
|
user_service: Arc<dyn DocumentUserService>,
|
2023-07-05 12:57:09 +00:00
|
|
|
collab_builder: Arc<AppFlowyCollabBuilder>,
|
|
|
|
cloud_service: Arc<dyn DocumentCloudService>,
|
2024-01-16 18:59:15 +00:00
|
|
|
storage_service: Weak<dyn ObjectStorageService>,
|
2024-01-07 03:12:05 +00:00
|
|
|
snapshot_service: Arc<dyn DocumentSnapshotService>,
|
2023-07-05 12:57:09 +00:00
|
|
|
) -> Self {
|
2023-04-13 10:53:51 +00:00
|
|
|
Self {
|
2024-01-07 03:12:05 +00:00
|
|
|
user_service,
|
2023-05-15 14:16:05 +00:00
|
|
|
collab_builder,
|
2024-03-30 08:28:24 +00:00
|
|
|
documents: Arc::new(Default::default()),
|
|
|
|
removing_documents: Arc::new(Default::default()),
|
2023-07-05 12:57:09 +00:00
|
|
|
cloud_service,
|
2023-09-01 14:27:29 +00:00
|
|
|
storage_service,
|
2024-01-07 03:12:05 +00:00
|
|
|
snapshot_service,
|
2023-04-13 10:53:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-29 01:46:24 +00:00
|
|
|
pub async fn initialize(&self, _uid: i64, _workspace_id: String) -> FlowyResult<()> {
|
2024-03-30 08:28:24 +00:00
|
|
|
self.documents.clear();
|
2023-07-29 01:46:24 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-10-24 12:11:06 +00:00
|
|
|
#[instrument(
|
|
|
|
name = "document_initialize_with_new_user",
|
|
|
|
level = "debug",
|
|
|
|
skip_all,
|
|
|
|
err
|
|
|
|
)]
|
2023-07-29 01:46:24 +00:00
|
|
|
pub async fn initialize_with_new_user(&self, uid: i64, workspace_id: String) -> FlowyResult<()> {
|
|
|
|
self.initialize(uid, workspace_id).await?;
|
|
|
|
Ok(())
|
|
|
|
}
|
2023-10-02 07:12:24 +00:00
|
|
|
|
|
|
|
pub async fn handle_reminder_action(&self, action: DocumentReminderAction) {
|
|
|
|
match action {
|
|
|
|
DocumentReminderAction::Add { reminder: _ } => {},
|
|
|
|
DocumentReminderAction::Remove { reminder_id: _ } => {},
|
|
|
|
DocumentReminderAction::Update { reminder: _ } => {},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-03 12:43:46 +00:00
|
|
|
/// Create a new document.
|
|
|
|
///
|
|
|
|
/// if the document already exists, return the existing document.
|
|
|
|
/// if the data is None, will create a document with default data.
|
2024-01-04 00:02:12 +00:00
|
|
|
#[instrument(level = "info", skip(self, data))]
|
2023-08-28 05:28:24 +00:00
|
|
|
pub async fn create_document(
|
2023-06-03 12:43:46 +00:00
|
|
|
&self,
|
2023-08-17 15:46:39 +00:00
|
|
|
uid: i64,
|
2023-06-17 06:25:30 +00:00
|
|
|
doc_id: &str,
|
2023-06-03 12:43:46 +00:00
|
|
|
data: Option<DocumentData>,
|
2023-12-27 03:42:39 +00:00
|
|
|
) -> FlowyResult<()> {
|
2024-01-22 05:34:15 +00:00
|
|
|
if self.is_doc_exist(doc_id).await.unwrap_or(false) {
|
2023-12-27 03:42:39 +00:00
|
|
|
Err(FlowyError::new(
|
|
|
|
ErrorCode::RecordAlreadyExists,
|
|
|
|
format!("document {} already exists", doc_id),
|
|
|
|
))
|
2023-08-28 15:20:56 +00:00
|
|
|
} else {
|
2024-01-10 10:43:13 +00:00
|
|
|
let doc_state =
|
2024-01-15 04:53:53 +00:00
|
|
|
doc_state_from_document_data(doc_id, data.unwrap_or_else(default_document_data))
|
|
|
|
.await?
|
2024-01-10 10:43:13 +00:00
|
|
|
.doc_state
|
|
|
|
.to_vec();
|
|
|
|
let collab = self
|
2024-04-15 06:50:28 +00:00
|
|
|
.collab_for_document(uid, doc_id, DataSource::DocStateV1(doc_state), false)
|
2024-01-10 10:43:13 +00:00
|
|
|
.await?;
|
|
|
|
collab.lock().flush();
|
2023-12-27 03:42:39 +00:00
|
|
|
Ok(())
|
2023-08-28 05:28:24 +00:00
|
|
|
}
|
2023-04-17 02:12:04 +00:00
|
|
|
}
|
|
|
|
|
2024-02-24 23:49:44 +00:00
|
|
|
/// Returns Document for given object id
|
|
|
|
/// If the document does not exist in local disk, try get the doc state from the cloud.
|
|
|
|
/// If the document exists, open the document and cache it
|
2024-02-26 07:27:12 +00:00
|
|
|
#[tracing::instrument(level = "info", skip(self), err)]
|
2023-07-14 05:37:13 +00:00
|
|
|
pub async fn get_document(&self, doc_id: &str) -> FlowyResult<Arc<MutexDocument>> {
|
2024-03-30 08:28:24 +00:00
|
|
|
if let Some(doc) = self.documents.get(doc_id).map(|item| item.value().clone()) {
|
|
|
|
return Ok(doc);
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(doc) = self.restore_document_from_removing(doc_id) {
|
2023-11-14 09:21:09 +00:00
|
|
|
return Ok(doc);
|
2023-04-13 10:53:51 +00:00
|
|
|
}
|
2023-12-01 03:17:49 +00:00
|
|
|
|
2024-04-15 06:50:28 +00:00
|
|
|
let mut doc_state = DataSource::Disk;
|
2024-02-24 23:49:44 +00:00
|
|
|
// If the document does not exist in local disk, try get the doc state from the cloud. This happens
|
|
|
|
// When user_device_a create a document and user_device_b open the document.
|
2024-01-22 05:34:15 +00:00
|
|
|
if !self.is_doc_exist(doc_id).await? {
|
2024-04-15 06:50:28 +00:00
|
|
|
doc_state = DataSource::DocStateV1(
|
2024-03-23 01:18:47 +00:00
|
|
|
self
|
|
|
|
.cloud_service
|
|
|
|
.get_document_doc_state(doc_id, &self.user_service.workspace_id()?)
|
|
|
|
.await?,
|
|
|
|
);
|
2024-02-24 23:49:44 +00:00
|
|
|
|
|
|
|
// the doc_state should not be empty if remote return the doc state without error.
|
|
|
|
if doc_state.is_empty() {
|
|
|
|
return Err(FlowyError::new(
|
|
|
|
ErrorCode::RecordNotFound,
|
|
|
|
format!("document {} not found", doc_id),
|
|
|
|
));
|
|
|
|
}
|
2023-07-05 12:57:09 +00:00
|
|
|
}
|
|
|
|
|
2024-01-07 03:12:05 +00:00
|
|
|
let uid = self.user_service.user_id()?;
|
2023-11-14 09:21:09 +00:00
|
|
|
event!(tracing::Level::DEBUG, "Initialize document: {}", doc_id);
|
2023-12-29 05:02:27 +00:00
|
|
|
let collab = self
|
|
|
|
.collab_for_document(uid, doc_id, doc_state, true)
|
|
|
|
.await?;
|
2023-07-05 12:57:09 +00:00
|
|
|
|
2024-02-24 23:49:44 +00:00
|
|
|
match MutexDocument::open(doc_id, collab) {
|
|
|
|
Ok(document) => {
|
|
|
|
let document = Arc::new(document);
|
2024-03-30 08:28:24 +00:00
|
|
|
self.documents.insert(doc_id.to_string(), document.clone());
|
2024-02-24 23:49:44 +00:00
|
|
|
Ok(document)
|
|
|
|
},
|
|
|
|
Err(err) => {
|
|
|
|
if err.is_invalid_data() {
|
|
|
|
if let Some(db) = self.user_service.collab_db(uid)?.upgrade() {
|
|
|
|
db.delete_doc(uid, doc_id).await?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Err(err);
|
|
|
|
},
|
|
|
|
}
|
2023-06-17 06:25:30 +00:00
|
|
|
}
|
|
|
|
|
2023-07-14 05:37:13 +00:00
|
|
|
pub async fn get_document_data(&self, doc_id: &str) -> FlowyResult<DocumentData> {
|
2024-04-15 06:50:28 +00:00
|
|
|
let mut doc_state = DataSource::Disk;
|
2024-01-22 05:34:15 +00:00
|
|
|
if !self.is_doc_exist(doc_id).await? {
|
2024-04-15 06:50:28 +00:00
|
|
|
doc_state = DataSource::DocStateV1(
|
2024-03-30 08:28:24 +00:00
|
|
|
self
|
|
|
|
.cloud_service
|
|
|
|
.get_document_doc_state(doc_id, &self.user_service.workspace_id()?)
|
|
|
|
.await?,
|
|
|
|
);
|
2023-07-05 12:57:09 +00:00
|
|
|
}
|
2024-01-07 03:12:05 +00:00
|
|
|
let uid = self.user_service.user_id()?;
|
2023-12-27 03:42:39 +00:00
|
|
|
let collab = self
|
2024-03-30 08:28:24 +00:00
|
|
|
.collab_for_document(uid, doc_id, doc_state, false)
|
2023-12-27 03:42:39 +00:00
|
|
|
.await?;
|
2023-07-05 12:57:09 +00:00
|
|
|
Document::open(collab)?
|
|
|
|
.get_document_data()
|
|
|
|
.map_err(internal_error)
|
2023-04-13 10:53:51 +00:00
|
|
|
}
|
|
|
|
|
2024-03-30 08:28:24 +00:00
|
|
|
pub async fn open_document(&self, doc_id: &str) -> FlowyResult<()> {
|
2024-04-02 14:15:42 +00:00
|
|
|
if let Some(mutex_document) = self.restore_document_from_removing(doc_id) {
|
|
|
|
mutex_document.start_init_sync();
|
|
|
|
}
|
2024-03-30 08:28:24 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-11-20 12:54:47 +00:00
|
|
|
pub async fn close_document(&self, doc_id: &str) -> FlowyResult<()> {
|
2024-03-30 08:28:24 +00:00
|
|
|
if let Some((doc_id, document)) = self.documents.remove(doc_id) {
|
|
|
|
if let Some(doc) = document.try_lock() {
|
2024-03-28 09:46:31 +00:00
|
|
|
// clear the awareness state when close the document
|
|
|
|
doc.clean_awareness_local_state();
|
2023-11-20 12:54:47 +00:00
|
|
|
let _ = doc.flush();
|
|
|
|
}
|
2024-03-30 08:28:24 +00:00
|
|
|
let clone_doc_id = doc_id.clone();
|
|
|
|
trace!("move document to removing_documents: {}", doc_id);
|
|
|
|
self.removing_documents.insert(doc_id, document);
|
|
|
|
|
|
|
|
let weak_removing_documents = Arc::downgrade(&self.removing_documents);
|
|
|
|
af_spawn(async move {
|
|
|
|
tokio::time::sleep(std::time::Duration::from_secs(120)).await;
|
|
|
|
if let Some(removing_documents) = weak_removing_documents.upgrade() {
|
|
|
|
if removing_documents.remove(&clone_doc_id).is_some() {
|
|
|
|
trace!("drop document from removing_documents: {}", clone_doc_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2023-11-20 12:54:47 +00:00
|
|
|
}
|
|
|
|
|
2023-06-05 01:42:11 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-01-22 05:34:15 +00:00
|
|
|
pub async fn delete_document(&self, doc_id: &str) -> FlowyResult<()> {
|
2024-01-07 03:12:05 +00:00
|
|
|
let uid = self.user_service.user_id()?;
|
|
|
|
if let Some(db) = self.user_service.collab_db(uid)?.upgrade() {
|
2024-01-22 05:34:15 +00:00
|
|
|
db.delete_doc(uid, doc_id).await?;
|
2023-11-14 09:21:09 +00:00
|
|
|
// When deleting a document, we need to remove it from the cache.
|
2024-03-30 08:28:24 +00:00
|
|
|
self.documents.remove(doc_id);
|
2023-07-29 01:46:24 +00:00
|
|
|
}
|
2023-04-13 10:53:51 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
2023-07-05 12:57:09 +00:00
|
|
|
|
2024-03-28 09:46:31 +00:00
|
|
|
pub async fn set_document_awareness_local_state(
|
|
|
|
&self,
|
|
|
|
doc_id: &str,
|
|
|
|
state: UpdateDocumentAwarenessStatePB,
|
|
|
|
) -> FlowyResult<bool> {
|
|
|
|
let uid = self.user_service.user_id()?;
|
|
|
|
let device_id = self.user_service.device_id()?;
|
|
|
|
if let Ok(doc) = self.get_document(doc_id).await {
|
|
|
|
if let Some(doc) = doc.try_lock() {
|
|
|
|
let user = DocumentAwarenessUser { uid, device_id };
|
|
|
|
let selection = state.selection.map(|s| s.into());
|
|
|
|
let state = DocumentAwarenessState {
|
|
|
|
version: 1,
|
|
|
|
user,
|
|
|
|
selection,
|
|
|
|
metadata: state.metadata,
|
|
|
|
timestamp: timestamp(),
|
|
|
|
};
|
|
|
|
doc.set_awareness_local_state(state);
|
|
|
|
return Ok(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(false)
|
|
|
|
}
|
|
|
|
|
2023-07-05 12:57:09 +00:00
|
|
|
/// Return the list of snapshots of the document.
|
2024-01-07 03:12:05 +00:00
|
|
|
pub async fn get_document_snapshot_meta(
|
2023-07-05 12:57:09 +00:00
|
|
|
&self,
|
|
|
|
document_id: &str,
|
2024-01-07 03:12:05 +00:00
|
|
|
_limit: usize,
|
|
|
|
) -> FlowyResult<Vec<DocumentSnapshotMetaPB>> {
|
|
|
|
let metas = self
|
|
|
|
.snapshot_service
|
|
|
|
.get_document_snapshot_metas(document_id)?
|
2023-08-17 15:46:39 +00:00
|
|
|
.into_iter()
|
2024-01-07 03:12:05 +00:00
|
|
|
.map(|meta| DocumentSnapshotMetaPB {
|
|
|
|
snapshot_id: meta.snapshot_id,
|
|
|
|
object_id: meta.object_id,
|
|
|
|
created_at: meta.created_at,
|
2023-07-05 12:57:09 +00:00
|
|
|
})
|
2023-08-17 15:46:39 +00:00
|
|
|
.collect::<Vec<_>>();
|
2023-07-05 12:57:09 +00:00
|
|
|
|
2024-01-07 03:12:05 +00:00
|
|
|
Ok(metas)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn get_document_snapshot(&self, snapshot_id: &str) -> FlowyResult<DocumentSnapshotPB> {
|
|
|
|
let snapshot = self
|
|
|
|
.snapshot_service
|
|
|
|
.get_document_snapshot(snapshot_id)
|
|
|
|
.map(|snapshot| DocumentSnapshotPB {
|
|
|
|
object_id: snapshot.object_id,
|
|
|
|
encoded_v1: snapshot.encoded_v1,
|
|
|
|
})?;
|
|
|
|
Ok(snapshot)
|
2023-07-05 12:57:09 +00:00
|
|
|
}
|
|
|
|
|
2024-01-16 18:59:15 +00:00
|
|
|
pub async fn upload_file(
|
|
|
|
&self,
|
|
|
|
workspace_id: String,
|
|
|
|
local_file_path: &str,
|
2024-02-03 21:49:45 +00:00
|
|
|
is_async: bool,
|
2024-01-16 18:59:15 +00:00
|
|
|
) -> FlowyResult<String> {
|
2024-01-20 15:16:18 +00:00
|
|
|
let (object_identity, object_value) = object_from_disk(&workspace_id, local_file_path).await?;
|
2024-01-16 18:59:15 +00:00
|
|
|
let storage_service = self.storage_service_upgrade()?;
|
2024-01-20 15:16:18 +00:00
|
|
|
let url = storage_service.get_object_url(object_identity).await?;
|
2024-01-16 18:59:15 +00:00
|
|
|
|
|
|
|
let clone_url = url.clone();
|
2024-02-03 21:49:45 +00:00
|
|
|
|
|
|
|
match is_async {
|
|
|
|
false => storage_service.put_object(clone_url, object_value).await?,
|
|
|
|
true => {
|
|
|
|
// let the upload happen in the background
|
|
|
|
af_spawn(async move {
|
|
|
|
if let Err(e) = storage_service.put_object(clone_url, object_value).await {
|
|
|
|
error!("upload file failed: {}", e);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
}
|
2024-01-16 18:59:15 +00:00
|
|
|
Ok(url)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn download_file(&self, local_file_path: String, url: String) -> FlowyResult<()> {
|
2024-01-22 05:34:15 +00:00
|
|
|
// TODO(nathan): save file when the current target is wasm
|
|
|
|
#[cfg(not(target_arch = "wasm32"))]
|
|
|
|
{
|
|
|
|
if tokio::fs::metadata(&local_file_path).await.is_ok() {
|
2024-03-23 01:18:47 +00:00
|
|
|
tracing::warn!("file already exist in user local disk: {}", local_file_path);
|
2024-01-22 05:34:15 +00:00
|
|
|
return Ok(());
|
|
|
|
}
|
2024-01-16 18:59:15 +00:00
|
|
|
|
2024-01-22 05:34:15 +00:00
|
|
|
let storage_service = self.storage_service_upgrade()?;
|
|
|
|
let object_value = storage_service.get_object(url).await?;
|
|
|
|
// create file if not exist
|
|
|
|
let mut file = tokio::fs::OpenOptions::new()
|
|
|
|
.create(true)
|
2024-04-23 13:46:57 +00:00
|
|
|
.truncate(true)
|
2024-01-22 05:34:15 +00:00
|
|
|
.write(true)
|
|
|
|
.open(&local_file_path)
|
|
|
|
.await?;
|
2024-01-16 18:59:15 +00:00
|
|
|
|
2024-01-22 05:34:15 +00:00
|
|
|
let n = file.write(&object_value.raw).await?;
|
2024-03-23 01:18:47 +00:00
|
|
|
tracing::info!("downloaded {} bytes to file: {}", n, local_file_path);
|
2024-01-22 05:34:15 +00:00
|
|
|
}
|
2024-01-16 18:59:15 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn delete_file(&self, local_file_path: String, url: String) -> FlowyResult<()> {
|
2024-01-22 05:34:15 +00:00
|
|
|
// TODO(nathan): delete file when the current target is wasm
|
|
|
|
#[cfg(not(target_arch = "wasm32"))]
|
2024-01-16 18:59:15 +00:00
|
|
|
// delete file from local
|
|
|
|
tokio::fs::remove_file(local_file_path).await?;
|
|
|
|
|
|
|
|
// delete from cloud
|
|
|
|
let storage_service = self.storage_service_upgrade()?;
|
2024-01-29 21:36:27 +00:00
|
|
|
af_spawn(async move {
|
2024-01-16 18:59:15 +00:00
|
|
|
if let Err(e) = storage_service.delete_object(url).await {
|
|
|
|
// TODO: add WAL to log the delete operation.
|
|
|
|
// keep a list of files to be deleted, and retry later
|
|
|
|
error!("delete file failed: {}", e);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-09-06 08:00:23 +00:00
|
|
|
async fn collab_for_document(
|
2023-07-14 05:37:13 +00:00
|
|
|
&self,
|
2023-08-17 15:46:39 +00:00
|
|
|
uid: i64,
|
2023-07-14 05:37:13 +00:00
|
|
|
doc_id: &str,
|
2024-04-15 06:50:28 +00:00
|
|
|
doc_state: DataSource,
|
2023-12-27 03:42:39 +00:00
|
|
|
sync_enable: bool,
|
2023-07-14 05:37:13 +00:00
|
|
|
) -> FlowyResult<Arc<MutexCollab>> {
|
2024-01-07 03:12:05 +00:00
|
|
|
let db = self.user_service.collab_db(uid)?;
|
2024-03-23 01:18:47 +00:00
|
|
|
let collab = self.collab_builder.build_with_config(
|
|
|
|
uid,
|
|
|
|
doc_id,
|
|
|
|
CollabType::Document,
|
|
|
|
db,
|
|
|
|
doc_state,
|
|
|
|
CollabPersistenceConfig::default().snapshot_per_update(1000),
|
|
|
|
CollabBuilderConfig::default().sync_enable(sync_enable),
|
|
|
|
)?;
|
2023-07-14 05:37:13 +00:00
|
|
|
Ok(collab)
|
2023-07-05 12:57:09 +00:00
|
|
|
}
|
|
|
|
|
2024-01-22 05:34:15 +00:00
|
|
|
async fn is_doc_exist(&self, doc_id: &str) -> FlowyResult<bool> {
|
2024-01-07 03:12:05 +00:00
|
|
|
let uid = self.user_service.user_id()?;
|
|
|
|
if let Some(collab_db) = self.user_service.collab_db(uid)?.upgrade() {
|
2024-01-22 05:34:15 +00:00
|
|
|
let is_exist = collab_db.is_exist(uid, doc_id).await?;
|
|
|
|
Ok(is_exist)
|
2023-07-29 01:46:24 +00:00
|
|
|
} else {
|
|
|
|
Ok(false)
|
|
|
|
}
|
2023-07-05 12:57:09 +00:00
|
|
|
}
|
|
|
|
|
2024-01-16 18:59:15 +00:00
|
|
|
fn storage_service_upgrade(&self) -> FlowyResult<Arc<dyn ObjectStorageService>> {
|
|
|
|
let storage_service = self.storage_service.upgrade().ok_or_else(|| {
|
|
|
|
FlowyError::internal().with_context("The file storage service is already dropped")
|
|
|
|
})?;
|
|
|
|
Ok(storage_service)
|
|
|
|
}
|
|
|
|
|
2023-07-05 12:57:09 +00:00
|
|
|
/// Only expose this method for testing
|
|
|
|
#[cfg(debug_assertions)]
|
|
|
|
pub fn get_cloud_service(&self) -> &Arc<dyn DocumentCloudService> {
|
|
|
|
&self.cloud_service
|
|
|
|
}
|
2023-09-01 14:27:29 +00:00
|
|
|
/// Only expose this method for testing
|
|
|
|
#[cfg(debug_assertions)]
|
2024-01-16 18:59:15 +00:00
|
|
|
pub fn get_file_storage_service(&self) -> &Weak<dyn ObjectStorageService> {
|
2023-09-01 14:27:29 +00:00
|
|
|
&self.storage_service
|
|
|
|
}
|
2024-03-30 08:28:24 +00:00
|
|
|
|
|
|
|
fn restore_document_from_removing(&self, doc_id: &str) -> Option<Arc<MutexDocument>> {
|
|
|
|
let (doc_id, doc) = self.removing_documents.remove(doc_id)?;
|
|
|
|
trace!(
|
|
|
|
"move document {} from removing_documents to documents",
|
|
|
|
doc_id
|
|
|
|
);
|
|
|
|
self.documents.insert(doc_id, doc.clone());
|
|
|
|
Some(doc)
|
|
|
|
}
|
2023-04-13 10:53:51 +00:00
|
|
|
}
|
2023-12-27 03:42:39 +00:00
|
|
|
|
2024-01-15 04:53:53 +00:00
|
|
|
async fn doc_state_from_document_data(
|
2023-12-27 03:42:39 +00:00
|
|
|
doc_id: &str,
|
|
|
|
data: DocumentData,
|
2024-01-03 03:41:29 +00:00
|
|
|
) -> Result<EncodedCollab, FlowyError> {
|
2024-01-15 04:53:53 +00:00
|
|
|
let doc_id = doc_id.to_string();
|
|
|
|
// spawn_blocking is used to avoid blocking the tokio thread pool if the document is large.
|
|
|
|
let encoded_collab = tokio::task::spawn_blocking(move || {
|
2024-04-15 06:50:28 +00:00
|
|
|
let collab = Arc::new(MutexCollab::new(Collab::new_with_origin(
|
2024-01-15 04:53:53 +00:00
|
|
|
CollabOrigin::Empty,
|
|
|
|
doc_id,
|
|
|
|
vec![],
|
2024-03-20 06:34:50 +00:00
|
|
|
false,
|
2024-01-15 04:53:53 +00:00
|
|
|
)));
|
2024-04-03 03:26:48 +00:00
|
|
|
let document = Document::create_with_data(collab.clone(), data).map_err(internal_error)?;
|
|
|
|
let encode_collab = document.encode_collab()?;
|
|
|
|
Ok::<_, FlowyError>(encode_collab)
|
2024-01-15 04:53:53 +00:00
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
Ok(encoded_collab)
|
2023-12-27 03:42:39 +00:00
|
|
|
}
|