Extract & add test
This commit is contained in:
parent
b194bc94d2
commit
0111afd296
2 changed files with 101 additions and 63 deletions
|
|
@ -8,7 +8,6 @@ use axum_extra::{
|
|||
use axum_jsonschema::Json;
|
||||
use chrono::{DateTime, Utc};
|
||||
use log::info;
|
||||
use regex::Regex;
|
||||
use schemars::JsonSchema;
|
||||
use serde::Deserialize;
|
||||
use sync_lib::{base64_to_bytes, is_file_type_mergable, merge};
|
||||
|
|
@ -20,12 +19,9 @@ use super::{
|
|||
responses::DocumentUpdateResponse,
|
||||
};
|
||||
use crate::{
|
||||
database::{
|
||||
self, Transaction,
|
||||
models::{DocumentId, StoredDocumentVersion, VaultId, VaultUpdateId},
|
||||
},
|
||||
database::models::{DocumentId, StoredDocumentVersion, VaultId, VaultUpdateId},
|
||||
errors::{SyncServerError, client_error, not_found_error, server_error},
|
||||
utils::sanitize_path,
|
||||
utils::{deduped_file_paths, sanitize_path},
|
||||
};
|
||||
|
||||
// This is required for aide to infer the path parameter types and names
|
||||
|
|
@ -172,13 +168,21 @@ async fn internal_update_document(
|
|||
let new_relative_path = if parent_document.relative_path == latest_version.relative_path
|
||||
&& latest_version.relative_path != sanitized_relative_path
|
||||
{
|
||||
get_deduped_file_name(
|
||||
&state.database,
|
||||
&vault_id,
|
||||
&mut transaction,
|
||||
&sanitized_relative_path,
|
||||
)
|
||||
.await?
|
||||
let mut new_relative_path = Default::default();
|
||||
for candidate in deduped_file_paths(&sanitized_relative_path) {
|
||||
if state
|
||||
.database
|
||||
.get_latest_document_by_path(&vault_id, &candidate, Some(&mut transaction))
|
||||
.await
|
||||
.map_err(server_error)?
|
||||
.is_none()
|
||||
{
|
||||
new_relative_path = candidate;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
new_relative_path
|
||||
} else {
|
||||
latest_version.relative_path.clone()
|
||||
};
|
||||
|
|
@ -212,53 +216,3 @@ async fn internal_update_document(
|
|||
DocumentUpdateResponse::FastForwardUpdate(new_version.into())
|
||||
}))
|
||||
}
|
||||
|
||||
// Only a single file can be on the same path, so we need to dedup the path
|
||||
// in case the client is trying to rename the file to an existing file's name
|
||||
// that it's unaware of.
|
||||
async fn get_deduped_file_name(
|
||||
database: &database::Database,
|
||||
vault_id: &VaultId,
|
||||
transaction: &mut Transaction<'_>,
|
||||
path: &str,
|
||||
) -> Result<String, SyncServerError> {
|
||||
let parts = path.rsplitn(2, '.').collect::<Vec<_>>();
|
||||
let mut reverse_parts = parts.into_iter().rev();
|
||||
let (stem, extension) = match (reverse_parts.next(), reverse_parts.next()) {
|
||||
(Some(stem), maybe_extension) => (
|
||||
stem,
|
||||
maybe_extension
|
||||
.map(|ext| format!(".{ext}"))
|
||||
.unwrap_or_default(),
|
||||
),
|
||||
_ => unreachable!("Path must have at least one part"),
|
||||
};
|
||||
|
||||
let regex = Regex::new(r" \((\d+)\)$").unwrap();
|
||||
let start_number = regex
|
||||
.captures(stem)
|
||||
.and_then(|caps| caps.get(1))
|
||||
.and_then(|m| m.as_str().parse::<u32>().ok())
|
||||
.unwrap_or(0);
|
||||
|
||||
let clean_stem = regex.replace(stem, "").to_string();
|
||||
|
||||
for dedup_number in start_number.. {
|
||||
let proposed_path = if dedup_number == 0 {
|
||||
format!("{clean_stem}{extension}")
|
||||
} else {
|
||||
format!("{clean_stem} ({dedup_number}){extension}")
|
||||
};
|
||||
|
||||
if database
|
||||
.get_latest_document_by_path(vault_id, &proposed_path, Some(transaction))
|
||||
.await
|
||||
.map_err(server_error)?
|
||||
.is_none()
|
||||
{
|
||||
return Ok(proposed_path.to_string());
|
||||
}
|
||||
}
|
||||
|
||||
unreachable!("Loop must always return a value");
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue