Dedup paths on create document
This commit is contained in:
parent
10fd928459
commit
aa3c587002
4 changed files with 48 additions and 19 deletions
|
|
@ -14,7 +14,10 @@ use crate::{
|
||||||
},
|
},
|
||||||
config::user_config::User,
|
config::user_config::User,
|
||||||
errors::{SyncServerError, client_error, server_error},
|
errors::{SyncServerError, client_error, server_error},
|
||||||
utils::{normalize::normalize, sanitize_path::sanitize_path},
|
utils::{
|
||||||
|
find_first_available_path::find_first_available_path, normalize::normalize,
|
||||||
|
sanitize_path::sanitize_path,
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
|
|
@ -66,11 +69,19 @@ pub async fn create_document(
|
||||||
.map_err(server_error)?;
|
.map_err(server_error)?;
|
||||||
|
|
||||||
let sanitized_relative_path = sanitize_path(&request.relative_path);
|
let sanitized_relative_path = sanitize_path(&request.relative_path);
|
||||||
|
let deduped_path = find_first_available_path(
|
||||||
|
&vault_id,
|
||||||
|
&sanitized_relative_path,
|
||||||
|
&state.database,
|
||||||
|
&mut transaction,
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.map_err(server_error)?;
|
||||||
|
|
||||||
let new_version = StoredDocumentVersion {
|
let new_version = StoredDocumentVersion {
|
||||||
vault_update_id: last_update_id + 1,
|
vault_update_id: last_update_id + 1,
|
||||||
document_id,
|
document_id,
|
||||||
relative_path: sanitized_relative_path,
|
relative_path: deduped_path,
|
||||||
content: request.content.contents.to_vec(),
|
content: request.content.contents.to_vec(),
|
||||||
updated_date: chrono::Utc::now(),
|
updated_date: chrono::Utc::now(),
|
||||||
is_deleted: false,
|
is_deleted: false,
|
||||||
|
|
|
||||||
|
|
@ -22,8 +22,8 @@ use crate::{
|
||||||
errors::{SyncServerError, not_found_error, server_error},
|
errors::{SyncServerError, not_found_error, server_error},
|
||||||
server::requests::UpdateBinaryDocumentVersion,
|
server::requests::UpdateBinaryDocumentVersion,
|
||||||
utils::{
|
utils::{
|
||||||
dedup_paths::dedup_paths, is_binary::is_binary,
|
dedup_paths::dedup_paths, find_first_available_path::find_first_available_path,
|
||||||
is_file_type_mergable::is_file_type_mergable, normalize::normalize,
|
is_binary::is_binary, is_file_type_mergable::is_file_type_mergable, normalize::normalize,
|
||||||
sanitize_path::sanitize_path,
|
sanitize_path::sanitize_path,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
@ -215,21 +215,14 @@ async fn update_document(
|
||||||
let new_relative_path = if parent_document.relative_path == latest_version.relative_path
|
let new_relative_path = if parent_document.relative_path == latest_version.relative_path
|
||||||
&& latest_version.relative_path != sanitized_relative_path
|
&& latest_version.relative_path != sanitized_relative_path
|
||||||
{
|
{
|
||||||
let mut new_relative_path = String::default();
|
find_first_available_path(
|
||||||
for candidate in dedup_paths(&sanitized_relative_path) {
|
&vault_id,
|
||||||
if state
|
&sanitized_relative_path,
|
||||||
.database
|
&state.database,
|
||||||
.get_latest_document_by_path(&vault_id, &candidate, Some(&mut transaction))
|
&mut transaction,
|
||||||
.await
|
)
|
||||||
.map_err(server_error)?
|
.await
|
||||||
.is_none()
|
.map_err(server_error)?
|
||||||
{
|
|
||||||
new_relative_path = candidate;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
new_relative_path
|
|
||||||
} else {
|
} else {
|
||||||
latest_version.relative_path.clone()
|
latest_version.relative_path.clone()
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
pub mod dedup_paths;
|
pub mod dedup_paths;
|
||||||
|
pub mod find_first_available_path;
|
||||||
pub mod is_binary;
|
pub mod is_binary;
|
||||||
pub mod is_file_type_mergable;
|
pub mod is_file_type_mergable;
|
||||||
pub mod normalize;
|
pub mod normalize;
|
||||||
|
|
|
||||||
24
sync-server/src/utils/find_first_available_path.rs
Normal file
24
sync-server/src/utils/find_first_available_path.rs
Normal file
|
|
@ -0,0 +1,24 @@
|
||||||
|
use crate::app_state::database::models::VaultId;
|
||||||
|
use crate::{app_state::database::Transaction, utils::dedup_paths::dedup_paths};
|
||||||
|
use anyhow::Result;
|
||||||
|
|
||||||
|
pub async fn find_first_available_path(
|
||||||
|
vault_id: &VaultId,
|
||||||
|
sanitized_relative_path: &str,
|
||||||
|
database: &crate::app_state::database::Database,
|
||||||
|
transaction: &mut Transaction<'_>,
|
||||||
|
) -> Result<String> {
|
||||||
|
let mut new_relative_path = String::default();
|
||||||
|
for candidate in dedup_paths(&sanitized_relative_path) {
|
||||||
|
if database
|
||||||
|
.get_latest_document_by_path(&vault_id, &candidate, Some(transaction))
|
||||||
|
.await?
|
||||||
|
.is_none()
|
||||||
|
{
|
||||||
|
new_relative_path = candidate;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(new_relative_path)
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue