treewide: fix various UI bugs; optimize crypto dependencies & format
Signed-off-by: NotAShelf <raf@notashelf.dev> Change-Id: If8fe8b38c1d9c4fecd40ff71f88d2ae06a6a6964
This commit is contained in:
parent
764aafa88d
commit
3ccddce7fd
178 changed files with 58342 additions and 54241 deletions
|
|
@ -3,295 +3,297 @@
|
|||
use std::path::{Path, PathBuf};
|
||||
|
||||
use chrono::Utc;
|
||||
use tokio::fs;
|
||||
use tokio::io::{AsyncReadExt, AsyncSeekExt, AsyncWriteExt};
|
||||
use tokio::{
|
||||
fs,
|
||||
io::{AsyncReadExt, AsyncSeekExt, AsyncWriteExt},
|
||||
};
|
||||
use tracing::{debug, info};
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::error::{PinakesError, Result};
|
||||
|
||||
use super::{ChunkInfo, UploadSession};
|
||||
use crate::error::{PinakesError, Result};
|
||||
|
||||
/// Manager for chunked uploads.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct ChunkedUploadManager {
|
||||
temp_dir: PathBuf,
|
||||
temp_dir: PathBuf,
|
||||
}
|
||||
|
||||
impl ChunkedUploadManager {
|
||||
/// Create a new chunked upload manager.
|
||||
pub fn new(temp_dir: PathBuf) -> Self {
|
||||
Self { temp_dir }
|
||||
/// Create a new chunked upload manager.
|
||||
pub fn new(temp_dir: PathBuf) -> Self {
|
||||
Self { temp_dir }
|
||||
}
|
||||
|
||||
/// Initialize the temp directory.
|
||||
pub async fn init(&self) -> Result<()> {
|
||||
fs::create_dir_all(&self.temp_dir).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Get the temp file path for an upload session.
|
||||
pub fn temp_path(&self, session_id: Uuid) -> PathBuf {
|
||||
self.temp_dir.join(format!("{}.upload", session_id))
|
||||
}
|
||||
|
||||
/// Create the temp file for a new upload session.
|
||||
pub async fn create_temp_file(&self, session: &UploadSession) -> Result<()> {
|
||||
let path = self.temp_path(session.id);
|
||||
|
||||
// Create a sparse file of the expected size
|
||||
let file = fs::File::create(&path).await?;
|
||||
file.set_len(session.expected_size).await?;
|
||||
|
||||
debug!(
|
||||
session_id = %session.id,
|
||||
size = session.expected_size,
|
||||
"created temp file for upload"
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Write a chunk to the temp file.
|
||||
pub async fn write_chunk(
|
||||
&self,
|
||||
session: &UploadSession,
|
||||
chunk_index: u64,
|
||||
data: &[u8],
|
||||
) -> Result<ChunkInfo> {
|
||||
let path = self.temp_path(session.id);
|
||||
|
||||
if !path.exists() {
|
||||
return Err(PinakesError::UploadSessionNotFound(session.id.to_string()));
|
||||
}
|
||||
|
||||
/// Initialize the temp directory.
|
||||
pub async fn init(&self) -> Result<()> {
|
||||
fs::create_dir_all(&self.temp_dir).await?;
|
||||
Ok(())
|
||||
// Calculate offset
|
||||
let offset = chunk_index * session.chunk_size;
|
||||
|
||||
// Validate chunk
|
||||
if offset >= session.expected_size {
|
||||
return Err(PinakesError::ChunkOutOfOrder {
|
||||
expected: session.chunk_count - 1,
|
||||
actual: chunk_index,
|
||||
});
|
||||
}
|
||||
|
||||
/// Get the temp file path for an upload session.
|
||||
pub fn temp_path(&self, session_id: Uuid) -> PathBuf {
|
||||
self.temp_dir.join(format!("{}.upload", session_id))
|
||||
// Calculate expected chunk size
|
||||
let expected_size = if chunk_index == session.chunk_count - 1 {
|
||||
// Last chunk may be smaller
|
||||
session.expected_size - offset
|
||||
} else {
|
||||
session.chunk_size
|
||||
};
|
||||
|
||||
if data.len() as u64 != expected_size {
|
||||
return Err(PinakesError::InvalidData(format!(
|
||||
"chunk {} has wrong size: expected {}, got {}",
|
||||
chunk_index,
|
||||
expected_size,
|
||||
data.len()
|
||||
)));
|
||||
}
|
||||
|
||||
/// Create the temp file for a new upload session.
|
||||
pub async fn create_temp_file(&self, session: &UploadSession) -> Result<()> {
|
||||
let path = self.temp_path(session.id);
|
||||
// Write chunk to file at offset
|
||||
let mut file = fs::OpenOptions::new().write(true).open(&path).await?;
|
||||
|
||||
// Create a sparse file of the expected size
|
||||
let file = fs::File::create(&path).await?;
|
||||
file.set_len(session.expected_size).await?;
|
||||
file.seek(std::io::SeekFrom::Start(offset)).await?;
|
||||
file.write_all(data).await?;
|
||||
file.flush().await?;
|
||||
|
||||
debug!(
|
||||
session_id = %session.id,
|
||||
size = session.expected_size,
|
||||
"created temp file for upload"
|
||||
);
|
||||
// Compute chunk hash
|
||||
let hash = blake3::hash(data).to_hex().to_string();
|
||||
|
||||
Ok(())
|
||||
debug!(
|
||||
session_id = %session.id,
|
||||
chunk_index,
|
||||
offset,
|
||||
size = data.len(),
|
||||
"wrote chunk"
|
||||
);
|
||||
|
||||
Ok(ChunkInfo {
|
||||
upload_id: session.id,
|
||||
chunk_index,
|
||||
offset,
|
||||
size: data.len() as u64,
|
||||
hash,
|
||||
received_at: Utc::now(),
|
||||
})
|
||||
}
|
||||
|
||||
/// Verify and finalize the upload.
|
||||
///
|
||||
/// Checks that:
|
||||
/// 1. All chunks are received
|
||||
/// 2. File size matches expected
|
||||
/// 3. Content hash matches expected
|
||||
pub async fn finalize(
|
||||
&self,
|
||||
session: &UploadSession,
|
||||
received_chunks: &[ChunkInfo],
|
||||
) -> Result<PathBuf> {
|
||||
let path = self.temp_path(session.id);
|
||||
|
||||
// Check all chunks received
|
||||
if received_chunks.len() as u64 != session.chunk_count {
|
||||
return Err(PinakesError::InvalidData(format!(
|
||||
"missing chunks: expected {}, got {}",
|
||||
session.chunk_count,
|
||||
received_chunks.len()
|
||||
)));
|
||||
}
|
||||
|
||||
/// Write a chunk to the temp file.
|
||||
pub async fn write_chunk(
|
||||
&self,
|
||||
session: &UploadSession,
|
||||
chunk_index: u64,
|
||||
data: &[u8],
|
||||
) -> Result<ChunkInfo> {
|
||||
let path = self.temp_path(session.id);
|
||||
|
||||
if !path.exists() {
|
||||
return Err(PinakesError::UploadSessionNotFound(session.id.to_string()));
|
||||
}
|
||||
|
||||
// Calculate offset
|
||||
let offset = chunk_index * session.chunk_size;
|
||||
|
||||
// Validate chunk
|
||||
if offset >= session.expected_size {
|
||||
return Err(PinakesError::ChunkOutOfOrder {
|
||||
expected: session.chunk_count - 1,
|
||||
actual: chunk_index,
|
||||
});
|
||||
}
|
||||
|
||||
// Calculate expected chunk size
|
||||
let expected_size = if chunk_index == session.chunk_count - 1 {
|
||||
// Last chunk may be smaller
|
||||
session.expected_size - offset
|
||||
} else {
|
||||
session.chunk_size
|
||||
};
|
||||
|
||||
if data.len() as u64 != expected_size {
|
||||
return Err(PinakesError::InvalidData(format!(
|
||||
"chunk {} has wrong size: expected {}, got {}",
|
||||
chunk_index,
|
||||
expected_size,
|
||||
data.len()
|
||||
)));
|
||||
}
|
||||
|
||||
// Write chunk to file at offset
|
||||
let mut file = fs::OpenOptions::new().write(true).open(&path).await?;
|
||||
|
||||
file.seek(std::io::SeekFrom::Start(offset)).await?;
|
||||
file.write_all(data).await?;
|
||||
file.flush().await?;
|
||||
|
||||
// Compute chunk hash
|
||||
let hash = blake3::hash(data).to_hex().to_string();
|
||||
|
||||
debug!(
|
||||
session_id = %session.id,
|
||||
chunk_index,
|
||||
offset,
|
||||
size = data.len(),
|
||||
"wrote chunk"
|
||||
);
|
||||
|
||||
Ok(ChunkInfo {
|
||||
upload_id: session.id,
|
||||
chunk_index,
|
||||
offset,
|
||||
size: data.len() as u64,
|
||||
hash,
|
||||
received_at: Utc::now(),
|
||||
})
|
||||
// Verify chunk indices
|
||||
let mut indices: Vec<u64> =
|
||||
received_chunks.iter().map(|c| c.chunk_index).collect();
|
||||
indices.sort();
|
||||
for (i, idx) in indices.iter().enumerate() {
|
||||
if *idx != i as u64 {
|
||||
return Err(PinakesError::InvalidData(format!(
|
||||
"chunk {} missing or out of order",
|
||||
i
|
||||
)));
|
||||
}
|
||||
}
|
||||
|
||||
/// Verify and finalize the upload.
|
||||
///
|
||||
/// Checks that:
|
||||
/// 1. All chunks are received
|
||||
/// 2. File size matches expected
|
||||
/// 3. Content hash matches expected
|
||||
pub async fn finalize(
|
||||
&self,
|
||||
session: &UploadSession,
|
||||
received_chunks: &[ChunkInfo],
|
||||
) -> Result<PathBuf> {
|
||||
let path = self.temp_path(session.id);
|
||||
// Verify file size
|
||||
let metadata = fs::metadata(&path).await?;
|
||||
if metadata.len() != session.expected_size {
|
||||
return Err(PinakesError::InvalidData(format!(
|
||||
"file size mismatch: expected {}, got {}",
|
||||
session.expected_size,
|
||||
metadata.len()
|
||||
)));
|
||||
}
|
||||
|
||||
// Check all chunks received
|
||||
if received_chunks.len() as u64 != session.chunk_count {
|
||||
return Err(PinakesError::InvalidData(format!(
|
||||
"missing chunks: expected {}, got {}",
|
||||
session.chunk_count,
|
||||
received_chunks.len()
|
||||
)));
|
||||
}
|
||||
// Verify content hash
|
||||
let computed_hash = compute_file_hash(&path).await?;
|
||||
if computed_hash != session.expected_hash.0 {
|
||||
return Err(PinakesError::StorageIntegrity(format!(
|
||||
"hash mismatch: expected {}, computed {}",
|
||||
session.expected_hash, computed_hash
|
||||
)));
|
||||
}
|
||||
|
||||
// Verify chunk indices
|
||||
let mut indices: Vec<u64> = received_chunks.iter().map(|c| c.chunk_index).collect();
|
||||
indices.sort();
|
||||
for (i, idx) in indices.iter().enumerate() {
|
||||
if *idx != i as u64 {
|
||||
return Err(PinakesError::InvalidData(format!(
|
||||
"chunk {} missing or out of order",
|
||||
i
|
||||
)));
|
||||
info!(
|
||||
session_id = %session.id,
|
||||
hash = %session.expected_hash,
|
||||
size = session.expected_size,
|
||||
"finalized chunked upload"
|
||||
);
|
||||
|
||||
Ok(path)
|
||||
}
|
||||
|
||||
/// Cancel an upload and clean up temp file.
|
||||
pub async fn cancel(&self, session_id: Uuid) -> Result<()> {
|
||||
let path = self.temp_path(session_id);
|
||||
if path.exists() {
|
||||
fs::remove_file(&path).await?;
|
||||
debug!(session_id = %session_id, "cancelled upload, removed temp file");
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Clean up expired temp files.
|
||||
pub async fn cleanup_expired(&self, max_age_hours: u64) -> Result<u64> {
|
||||
let mut count = 0u64;
|
||||
let max_age = std::time::Duration::from_secs(max_age_hours * 3600);
|
||||
|
||||
let mut entries = fs::read_dir(&self.temp_dir).await?;
|
||||
while let Some(entry) = entries.next_entry().await? {
|
||||
let path = entry.path();
|
||||
if path.extension().map(|e| e == "upload").unwrap_or(false) {
|
||||
if let Ok(metadata) = fs::metadata(&path).await {
|
||||
if let Ok(modified) = metadata.modified() {
|
||||
let age = std::time::SystemTime::now()
|
||||
.duration_since(modified)
|
||||
.unwrap_or_default();
|
||||
if age > max_age {
|
||||
let _ = fs::remove_file(&path).await;
|
||||
count += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Verify file size
|
||||
let metadata = fs::metadata(&path).await?;
|
||||
if metadata.len() != session.expected_size {
|
||||
return Err(PinakesError::InvalidData(format!(
|
||||
"file size mismatch: expected {}, got {}",
|
||||
session.expected_size,
|
||||
metadata.len()
|
||||
)));
|
||||
}
|
||||
|
||||
// Verify content hash
|
||||
let computed_hash = compute_file_hash(&path).await?;
|
||||
if computed_hash != session.expected_hash.0 {
|
||||
return Err(PinakesError::StorageIntegrity(format!(
|
||||
"hash mismatch: expected {}, computed {}",
|
||||
session.expected_hash, computed_hash
|
||||
)));
|
||||
}
|
||||
|
||||
info!(
|
||||
session_id = %session.id,
|
||||
hash = %session.expected_hash,
|
||||
size = session.expected_size,
|
||||
"finalized chunked upload"
|
||||
);
|
||||
|
||||
Ok(path)
|
||||
}
|
||||
}
|
||||
|
||||
/// Cancel an upload and clean up temp file.
|
||||
pub async fn cancel(&self, session_id: Uuid) -> Result<()> {
|
||||
let path = self.temp_path(session_id);
|
||||
if path.exists() {
|
||||
fs::remove_file(&path).await?;
|
||||
debug!(session_id = %session_id, "cancelled upload, removed temp file");
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Clean up expired temp files.
|
||||
pub async fn cleanup_expired(&self, max_age_hours: u64) -> Result<u64> {
|
||||
let mut count = 0u64;
|
||||
let max_age = std::time::Duration::from_secs(max_age_hours * 3600);
|
||||
|
||||
let mut entries = fs::read_dir(&self.temp_dir).await?;
|
||||
while let Some(entry) = entries.next_entry().await? {
|
||||
let path = entry.path();
|
||||
if path.extension().map(|e| e == "upload").unwrap_or(false) {
|
||||
if let Ok(metadata) = fs::metadata(&path).await {
|
||||
if let Ok(modified) = metadata.modified() {
|
||||
let age = std::time::SystemTime::now()
|
||||
.duration_since(modified)
|
||||
.unwrap_or_default();
|
||||
if age > max_age {
|
||||
let _ = fs::remove_file(&path).await;
|
||||
count += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if count > 0 {
|
||||
info!(count, "cleaned up expired upload temp files");
|
||||
}
|
||||
Ok(count)
|
||||
if count > 0 {
|
||||
info!(count, "cleaned up expired upload temp files");
|
||||
}
|
||||
Ok(count)
|
||||
}
|
||||
}
|
||||
|
||||
/// Compute the BLAKE3 hash of a file.
|
||||
async fn compute_file_hash(path: &Path) -> Result<String> {
|
||||
let mut file = fs::File::open(path).await?;
|
||||
let mut hasher = blake3::Hasher::new();
|
||||
let mut buf = vec![0u8; 64 * 1024];
|
||||
let mut file = fs::File::open(path).await?;
|
||||
let mut hasher = blake3::Hasher::new();
|
||||
let mut buf = vec![0u8; 64 * 1024];
|
||||
|
||||
loop {
|
||||
let n = file.read(&mut buf).await?;
|
||||
if n == 0 {
|
||||
break;
|
||||
}
|
||||
hasher.update(&buf[..n]);
|
||||
loop {
|
||||
let n = file.read(&mut buf).await?;
|
||||
if n == 0 {
|
||||
break;
|
||||
}
|
||||
hasher.update(&buf[..n]);
|
||||
}
|
||||
|
||||
Ok(hasher.finalize().to_hex().to_string())
|
||||
Ok(hasher.finalize().to_hex().to_string())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::model::ContentHash;
|
||||
use crate::sync::UploadStatus;
|
||||
use tempfile::tempdir;
|
||||
use tempfile::tempdir;
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_chunked_upload() {
|
||||
let dir = tempdir().unwrap();
|
||||
let manager = ChunkedUploadManager::new(dir.path().to_path_buf());
|
||||
manager.init().await.unwrap();
|
||||
use super::*;
|
||||
use crate::{model::ContentHash, sync::UploadStatus};
|
||||
|
||||
// Create test data
|
||||
let data = b"Hello, World! This is test data for chunked upload.";
|
||||
let hash = blake3::hash(data).to_hex().to_string();
|
||||
let chunk_size = 20u64;
|
||||
#[tokio::test]
|
||||
async fn test_chunked_upload() {
|
||||
let dir = tempdir().unwrap();
|
||||
let manager = ChunkedUploadManager::new(dir.path().to_path_buf());
|
||||
manager.init().await.unwrap();
|
||||
|
||||
let session = UploadSession {
|
||||
id: Uuid::now_v7(),
|
||||
device_id: super::super::DeviceId::new(),
|
||||
target_path: "/test/file.txt".to_string(),
|
||||
expected_hash: ContentHash::new(hash.clone()),
|
||||
expected_size: data.len() as u64,
|
||||
chunk_size,
|
||||
chunk_count: (data.len() as u64 + chunk_size - 1) / chunk_size,
|
||||
status: UploadStatus::InProgress,
|
||||
created_at: Utc::now(),
|
||||
expires_at: Utc::now() + chrono::Duration::hours(24),
|
||||
last_activity: Utc::now(),
|
||||
};
|
||||
// Create test data
|
||||
let data = b"Hello, World! This is test data for chunked upload.";
|
||||
let hash = blake3::hash(data).to_hex().to_string();
|
||||
let chunk_size = 20u64;
|
||||
|
||||
manager.create_temp_file(&session).await.unwrap();
|
||||
let session = UploadSession {
|
||||
id: Uuid::now_v7(),
|
||||
device_id: super::super::DeviceId::new(),
|
||||
target_path: "/test/file.txt".to_string(),
|
||||
expected_hash: ContentHash::new(hash.clone()),
|
||||
expected_size: data.len() as u64,
|
||||
chunk_size,
|
||||
chunk_count: (data.len() as u64 + chunk_size - 1) / chunk_size,
|
||||
status: UploadStatus::InProgress,
|
||||
created_at: Utc::now(),
|
||||
expires_at: Utc::now() + chrono::Duration::hours(24),
|
||||
last_activity: Utc::now(),
|
||||
};
|
||||
|
||||
// Write chunks
|
||||
let mut chunks = Vec::new();
|
||||
for i in 0..session.chunk_count {
|
||||
let start = (i * chunk_size) as usize;
|
||||
let end = ((i + 1) * chunk_size).min(data.len() as u64) as usize;
|
||||
let chunk_data = &data[start..end];
|
||||
manager.create_temp_file(&session).await.unwrap();
|
||||
|
||||
let chunk = manager.write_chunk(&session, i, chunk_data).await.unwrap();
|
||||
chunks.push(chunk);
|
||||
}
|
||||
// Write chunks
|
||||
let mut chunks = Vec::new();
|
||||
for i in 0..session.chunk_count {
|
||||
let start = (i * chunk_size) as usize;
|
||||
let end = ((i + 1) * chunk_size).min(data.len() as u64) as usize;
|
||||
let chunk_data = &data[start..end];
|
||||
|
||||
// Finalize
|
||||
let final_path = manager.finalize(&session, &chunks).await.unwrap();
|
||||
assert!(final_path.exists());
|
||||
|
||||
// Verify content
|
||||
let content = fs::read(&final_path).await.unwrap();
|
||||
assert_eq!(&content[..], data);
|
||||
let chunk = manager.write_chunk(&session, i, chunk_data).await.unwrap();
|
||||
chunks.push(chunk);
|
||||
}
|
||||
|
||||
// Finalize
|
||||
let final_path = manager.finalize(&session, &chunks).await.unwrap();
|
||||
assert!(final_path.exists());
|
||||
|
||||
// Verify content
|
||||
let content = fs::read(&final_path).await.unwrap();
|
||||
assert_eq!(&content[..], data);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,144 +1,144 @@
|
|||
//! Conflict detection and resolution for sync.
|
||||
|
||||
use crate::config::ConflictResolution;
|
||||
|
||||
use super::DeviceSyncState;
|
||||
use crate::config::ConflictResolution;
|
||||
|
||||
/// Detect if there's a conflict between local and server state.
|
||||
pub fn detect_conflict(state: &DeviceSyncState) -> Option<ConflictInfo> {
|
||||
// If either side has no hash, no conflict possible
|
||||
let local_hash = state.local_hash.as_ref()?;
|
||||
let server_hash = state.server_hash.as_ref()?;
|
||||
// If either side has no hash, no conflict possible
|
||||
let local_hash = state.local_hash.as_ref()?;
|
||||
let server_hash = state.server_hash.as_ref()?;
|
||||
|
||||
// Same hash = no conflict
|
||||
if local_hash == server_hash {
|
||||
return None;
|
||||
}
|
||||
// Same hash = no conflict
|
||||
if local_hash == server_hash {
|
||||
return None;
|
||||
}
|
||||
|
||||
// Both have different hashes = conflict
|
||||
Some(ConflictInfo {
|
||||
path: state.path.clone(),
|
||||
local_hash: local_hash.clone(),
|
||||
server_hash: server_hash.clone(),
|
||||
local_mtime: state.local_mtime,
|
||||
server_mtime: state.server_mtime,
|
||||
})
|
||||
// Both have different hashes = conflict
|
||||
Some(ConflictInfo {
|
||||
path: state.path.clone(),
|
||||
local_hash: local_hash.clone(),
|
||||
server_hash: server_hash.clone(),
|
||||
local_mtime: state.local_mtime,
|
||||
server_mtime: state.server_mtime,
|
||||
})
|
||||
}
|
||||
|
||||
/// Information about a detected conflict.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct ConflictInfo {
|
||||
pub path: String,
|
||||
pub local_hash: String,
|
||||
pub server_hash: String,
|
||||
pub local_mtime: Option<i64>,
|
||||
pub server_mtime: Option<i64>,
|
||||
pub path: String,
|
||||
pub local_hash: String,
|
||||
pub server_hash: String,
|
||||
pub local_mtime: Option<i64>,
|
||||
pub server_mtime: Option<i64>,
|
||||
}
|
||||
|
||||
/// Result of resolving a conflict.
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum ConflictOutcome {
|
||||
/// Use the server version
|
||||
UseServer,
|
||||
/// Use the local version (upload it)
|
||||
UseLocal,
|
||||
/// Keep both versions (rename one)
|
||||
KeepBoth { new_local_path: String },
|
||||
/// Requires manual intervention
|
||||
Manual,
|
||||
/// Use the server version
|
||||
UseServer,
|
||||
/// Use the local version (upload it)
|
||||
UseLocal,
|
||||
/// Keep both versions (rename one)
|
||||
KeepBoth { new_local_path: String },
|
||||
/// Requires manual intervention
|
||||
Manual,
|
||||
}
|
||||
|
||||
/// Resolve a conflict based on the configured strategy.
|
||||
pub fn resolve_conflict(
|
||||
conflict: &ConflictInfo,
|
||||
resolution: ConflictResolution,
|
||||
conflict: &ConflictInfo,
|
||||
resolution: ConflictResolution,
|
||||
) -> ConflictOutcome {
|
||||
match resolution {
|
||||
ConflictResolution::ServerWins => ConflictOutcome::UseServer,
|
||||
ConflictResolution::ClientWins => ConflictOutcome::UseLocal,
|
||||
ConflictResolution::KeepBoth => {
|
||||
let new_path = generate_conflict_path(&conflict.path, &conflict.local_hash);
|
||||
ConflictOutcome::KeepBoth {
|
||||
new_local_path: new_path,
|
||||
}
|
||||
}
|
||||
ConflictResolution::Manual => ConflictOutcome::Manual,
|
||||
}
|
||||
match resolution {
|
||||
ConflictResolution::ServerWins => ConflictOutcome::UseServer,
|
||||
ConflictResolution::ClientWins => ConflictOutcome::UseLocal,
|
||||
ConflictResolution::KeepBoth => {
|
||||
let new_path =
|
||||
generate_conflict_path(&conflict.path, &conflict.local_hash);
|
||||
ConflictOutcome::KeepBoth {
|
||||
new_local_path: new_path,
|
||||
}
|
||||
},
|
||||
ConflictResolution::Manual => ConflictOutcome::Manual,
|
||||
}
|
||||
}
|
||||
|
||||
/// Generate a new path for the conflicting local file.
|
||||
/// Format: filename.conflict-<short_hash>.ext
|
||||
fn generate_conflict_path(original_path: &str, local_hash: &str) -> String {
|
||||
let short_hash = &local_hash[..8.min(local_hash.len())];
|
||||
let short_hash = &local_hash[..8.min(local_hash.len())];
|
||||
|
||||
if let Some((base, ext)) = original_path.rsplit_once('.') {
|
||||
format!("{}.conflict-{}.{}", base, short_hash, ext)
|
||||
} else {
|
||||
format!("{}.conflict-{}", original_path, short_hash)
|
||||
}
|
||||
if let Some((base, ext)) = original_path.rsplit_once('.') {
|
||||
format!("{}.conflict-{}.{}", base, short_hash, ext)
|
||||
} else {
|
||||
format!("{}.conflict-{}", original_path, short_hash)
|
||||
}
|
||||
}
|
||||
|
||||
/// Automatic conflict resolution based on modification times.
|
||||
/// Useful when ConflictResolution is set to a time-based strategy.
|
||||
pub fn resolve_by_mtime(conflict: &ConflictInfo) -> ConflictOutcome {
|
||||
match (conflict.local_mtime, conflict.server_mtime) {
|
||||
(Some(local), Some(server)) => {
|
||||
if local > server {
|
||||
ConflictOutcome::UseLocal
|
||||
} else {
|
||||
ConflictOutcome::UseServer
|
||||
}
|
||||
}
|
||||
(Some(_), None) => ConflictOutcome::UseLocal,
|
||||
(None, Some(_)) => ConflictOutcome::UseServer,
|
||||
(None, None) => ConflictOutcome::UseServer, // Default to server
|
||||
}
|
||||
match (conflict.local_mtime, conflict.server_mtime) {
|
||||
(Some(local), Some(server)) => {
|
||||
if local > server {
|
||||
ConflictOutcome::UseLocal
|
||||
} else {
|
||||
ConflictOutcome::UseServer
|
||||
}
|
||||
},
|
||||
(Some(_), None) => ConflictOutcome::UseLocal,
|
||||
(None, Some(_)) => ConflictOutcome::UseServer,
|
||||
(None, None) => ConflictOutcome::UseServer, // Default to server
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::sync::FileSyncStatus;
|
||||
use super::*;
|
||||
use crate::sync::FileSyncStatus;
|
||||
|
||||
#[test]
|
||||
fn test_generate_conflict_path() {
|
||||
assert_eq!(
|
||||
generate_conflict_path("/path/to/file.txt", "abc12345"),
|
||||
"/path/to/file.conflict-abc12345.txt"
|
||||
);
|
||||
#[test]
|
||||
fn test_generate_conflict_path() {
|
||||
assert_eq!(
|
||||
generate_conflict_path("/path/to/file.txt", "abc12345"),
|
||||
"/path/to/file.conflict-abc12345.txt"
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
generate_conflict_path("/path/to/file", "abc12345"),
|
||||
"/path/to/file.conflict-abc12345"
|
||||
);
|
||||
}
|
||||
assert_eq!(
|
||||
generate_conflict_path("/path/to/file", "abc12345"),
|
||||
"/path/to/file.conflict-abc12345"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_detect_conflict() {
|
||||
let state_no_conflict = DeviceSyncState {
|
||||
device_id: super::super::DeviceId::new(),
|
||||
path: "/test".to_string(),
|
||||
local_hash: Some("abc".to_string()),
|
||||
server_hash: Some("abc".to_string()),
|
||||
local_mtime: None,
|
||||
server_mtime: None,
|
||||
sync_status: FileSyncStatus::Synced,
|
||||
last_synced_at: None,
|
||||
conflict_info_json: None,
|
||||
};
|
||||
assert!(detect_conflict(&state_no_conflict).is_none());
|
||||
#[test]
|
||||
fn test_detect_conflict() {
|
||||
let state_no_conflict = DeviceSyncState {
|
||||
device_id: super::super::DeviceId::new(),
|
||||
path: "/test".to_string(),
|
||||
local_hash: Some("abc".to_string()),
|
||||
server_hash: Some("abc".to_string()),
|
||||
local_mtime: None,
|
||||
server_mtime: None,
|
||||
sync_status: FileSyncStatus::Synced,
|
||||
last_synced_at: None,
|
||||
conflict_info_json: None,
|
||||
};
|
||||
assert!(detect_conflict(&state_no_conflict).is_none());
|
||||
|
||||
let state_conflict = DeviceSyncState {
|
||||
device_id: super::super::DeviceId::new(),
|
||||
path: "/test".to_string(),
|
||||
local_hash: Some("abc".to_string()),
|
||||
server_hash: Some("def".to_string()),
|
||||
local_mtime: None,
|
||||
server_mtime: None,
|
||||
sync_status: FileSyncStatus::Conflict,
|
||||
last_synced_at: None,
|
||||
conflict_info_json: None,
|
||||
};
|
||||
assert!(detect_conflict(&state_conflict).is_some());
|
||||
}
|
||||
let state_conflict = DeviceSyncState {
|
||||
device_id: super::super::DeviceId::new(),
|
||||
path: "/test".to_string(),
|
||||
local_hash: Some("abc".to_string()),
|
||||
server_hash: Some("def".to_string()),
|
||||
local_mtime: None,
|
||||
server_mtime: None,
|
||||
sync_status: FileSyncStatus::Conflict,
|
||||
last_synced_at: None,
|
||||
conflict_info_json: None,
|
||||
};
|
||||
assert!(detect_conflict(&state_conflict).is_some());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,375 +6,377 @@ use chrono::{DateTime, Utc};
|
|||
use serde::{Deserialize, Serialize};
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::config::ConflictResolution;
|
||||
use crate::model::{ContentHash, MediaId};
|
||||
use crate::users::UserId;
|
||||
use crate::{
|
||||
config::ConflictResolution,
|
||||
model::{ContentHash, MediaId},
|
||||
users::UserId,
|
||||
};
|
||||
|
||||
/// Unique identifier for a sync device.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
|
||||
pub struct DeviceId(pub Uuid);
|
||||
|
||||
impl DeviceId {
|
||||
pub fn new() -> Self {
|
||||
Self(Uuid::now_v7())
|
||||
}
|
||||
pub fn new() -> Self {
|
||||
Self(Uuid::now_v7())
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for DeviceId {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for DeviceId {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "{}", self.0)
|
||||
}
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "{}", self.0)
|
||||
}
|
||||
}
|
||||
|
||||
/// Type of sync device.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
pub enum DeviceType {
|
||||
Desktop,
|
||||
Mobile,
|
||||
Tablet,
|
||||
Server,
|
||||
Other,
|
||||
Desktop,
|
||||
Mobile,
|
||||
Tablet,
|
||||
Server,
|
||||
Other,
|
||||
}
|
||||
|
||||
impl Default for DeviceType {
|
||||
fn default() -> Self {
|
||||
Self::Other
|
||||
}
|
||||
fn default() -> Self {
|
||||
Self::Other
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for DeviceType {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
Self::Desktop => write!(f, "desktop"),
|
||||
Self::Mobile => write!(f, "mobile"),
|
||||
Self::Tablet => write!(f, "tablet"),
|
||||
Self::Server => write!(f, "server"),
|
||||
Self::Other => write!(f, "other"),
|
||||
}
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
Self::Desktop => write!(f, "desktop"),
|
||||
Self::Mobile => write!(f, "mobile"),
|
||||
Self::Tablet => write!(f, "tablet"),
|
||||
Self::Server => write!(f, "server"),
|
||||
Self::Other => write!(f, "other"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::str::FromStr for DeviceType {
|
||||
type Err = String;
|
||||
type Err = String;
|
||||
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
match s.to_lowercase().as_str() {
|
||||
"desktop" => Ok(Self::Desktop),
|
||||
"mobile" => Ok(Self::Mobile),
|
||||
"tablet" => Ok(Self::Tablet),
|
||||
"server" => Ok(Self::Server),
|
||||
"other" => Ok(Self::Other),
|
||||
_ => Err(format!("unknown device type: {}", s)),
|
||||
}
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
match s.to_lowercase().as_str() {
|
||||
"desktop" => Ok(Self::Desktop),
|
||||
"mobile" => Ok(Self::Mobile),
|
||||
"tablet" => Ok(Self::Tablet),
|
||||
"server" => Ok(Self::Server),
|
||||
"other" => Ok(Self::Other),
|
||||
_ => Err(format!("unknown device type: {}", s)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A registered sync device.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct SyncDevice {
|
||||
pub id: DeviceId,
|
||||
pub user_id: UserId,
|
||||
pub name: String,
|
||||
pub device_type: DeviceType,
|
||||
pub client_version: String,
|
||||
pub os_info: Option<String>,
|
||||
pub last_sync_at: Option<DateTime<Utc>>,
|
||||
pub last_seen_at: DateTime<Utc>,
|
||||
pub sync_cursor: Option<i64>,
|
||||
pub enabled: bool,
|
||||
pub created_at: DateTime<Utc>,
|
||||
pub updated_at: DateTime<Utc>,
|
||||
pub id: DeviceId,
|
||||
pub user_id: UserId,
|
||||
pub name: String,
|
||||
pub device_type: DeviceType,
|
||||
pub client_version: String,
|
||||
pub os_info: Option<String>,
|
||||
pub last_sync_at: Option<DateTime<Utc>>,
|
||||
pub last_seen_at: DateTime<Utc>,
|
||||
pub sync_cursor: Option<i64>,
|
||||
pub enabled: bool,
|
||||
pub created_at: DateTime<Utc>,
|
||||
pub updated_at: DateTime<Utc>,
|
||||
}
|
||||
|
||||
impl SyncDevice {
|
||||
pub fn new(
|
||||
user_id: UserId,
|
||||
name: String,
|
||||
device_type: DeviceType,
|
||||
client_version: String,
|
||||
) -> Self {
|
||||
let now = Utc::now();
|
||||
Self {
|
||||
id: DeviceId::new(),
|
||||
user_id,
|
||||
name,
|
||||
device_type,
|
||||
client_version,
|
||||
os_info: None,
|
||||
last_sync_at: None,
|
||||
last_seen_at: now,
|
||||
sync_cursor: None,
|
||||
enabled: true,
|
||||
created_at: now,
|
||||
updated_at: now,
|
||||
}
|
||||
pub fn new(
|
||||
user_id: UserId,
|
||||
name: String,
|
||||
device_type: DeviceType,
|
||||
client_version: String,
|
||||
) -> Self {
|
||||
let now = Utc::now();
|
||||
Self {
|
||||
id: DeviceId::new(),
|
||||
user_id,
|
||||
name,
|
||||
device_type,
|
||||
client_version,
|
||||
os_info: None,
|
||||
last_sync_at: None,
|
||||
last_seen_at: now,
|
||||
sync_cursor: None,
|
||||
enabled: true,
|
||||
created_at: now,
|
||||
updated_at: now,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Type of change recorded in the sync log.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub enum SyncChangeType {
|
||||
Created,
|
||||
Modified,
|
||||
Deleted,
|
||||
Moved,
|
||||
MetadataUpdated,
|
||||
Created,
|
||||
Modified,
|
||||
Deleted,
|
||||
Moved,
|
||||
MetadataUpdated,
|
||||
}
|
||||
|
||||
impl fmt::Display for SyncChangeType {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
Self::Created => write!(f, "created"),
|
||||
Self::Modified => write!(f, "modified"),
|
||||
Self::Deleted => write!(f, "deleted"),
|
||||
Self::Moved => write!(f, "moved"),
|
||||
Self::MetadataUpdated => write!(f, "metadata_updated"),
|
||||
}
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
Self::Created => write!(f, "created"),
|
||||
Self::Modified => write!(f, "modified"),
|
||||
Self::Deleted => write!(f, "deleted"),
|
||||
Self::Moved => write!(f, "moved"),
|
||||
Self::MetadataUpdated => write!(f, "metadata_updated"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::str::FromStr for SyncChangeType {
|
||||
type Err = String;
|
||||
type Err = String;
|
||||
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
match s.to_lowercase().as_str() {
|
||||
"created" => Ok(Self::Created),
|
||||
"modified" => Ok(Self::Modified),
|
||||
"deleted" => Ok(Self::Deleted),
|
||||
"moved" => Ok(Self::Moved),
|
||||
"metadata_updated" => Ok(Self::MetadataUpdated),
|
||||
_ => Err(format!("unknown sync change type: {}", s)),
|
||||
}
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
match s.to_lowercase().as_str() {
|
||||
"created" => Ok(Self::Created),
|
||||
"modified" => Ok(Self::Modified),
|
||||
"deleted" => Ok(Self::Deleted),
|
||||
"moved" => Ok(Self::Moved),
|
||||
"metadata_updated" => Ok(Self::MetadataUpdated),
|
||||
_ => Err(format!("unknown sync change type: {}", s)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// An entry in the sync log tracking a change.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct SyncLogEntry {
|
||||
pub id: Uuid,
|
||||
pub sequence: i64,
|
||||
pub change_type: SyncChangeType,
|
||||
pub media_id: Option<MediaId>,
|
||||
pub path: String,
|
||||
pub content_hash: Option<ContentHash>,
|
||||
pub file_size: Option<u64>,
|
||||
pub metadata_json: Option<String>,
|
||||
pub changed_by_device: Option<DeviceId>,
|
||||
pub timestamp: DateTime<Utc>,
|
||||
pub id: Uuid,
|
||||
pub sequence: i64,
|
||||
pub change_type: SyncChangeType,
|
||||
pub media_id: Option<MediaId>,
|
||||
pub path: String,
|
||||
pub content_hash: Option<ContentHash>,
|
||||
pub file_size: Option<u64>,
|
||||
pub metadata_json: Option<String>,
|
||||
pub changed_by_device: Option<DeviceId>,
|
||||
pub timestamp: DateTime<Utc>,
|
||||
}
|
||||
|
||||
impl SyncLogEntry {
|
||||
pub fn new(
|
||||
change_type: SyncChangeType,
|
||||
path: String,
|
||||
media_id: Option<MediaId>,
|
||||
content_hash: Option<ContentHash>,
|
||||
) -> Self {
|
||||
Self {
|
||||
id: Uuid::now_v7(),
|
||||
sequence: 0, // Will be assigned by database
|
||||
change_type,
|
||||
media_id,
|
||||
path,
|
||||
content_hash,
|
||||
file_size: None,
|
||||
metadata_json: None,
|
||||
changed_by_device: None,
|
||||
timestamp: Utc::now(),
|
||||
}
|
||||
pub fn new(
|
||||
change_type: SyncChangeType,
|
||||
path: String,
|
||||
media_id: Option<MediaId>,
|
||||
content_hash: Option<ContentHash>,
|
||||
) -> Self {
|
||||
Self {
|
||||
id: Uuid::now_v7(),
|
||||
sequence: 0, // Will be assigned by database
|
||||
change_type,
|
||||
media_id,
|
||||
path,
|
||||
content_hash,
|
||||
file_size: None,
|
||||
metadata_json: None,
|
||||
changed_by_device: None,
|
||||
timestamp: Utc::now(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Sync status for a file on a device.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub enum FileSyncStatus {
|
||||
Synced,
|
||||
PendingUpload,
|
||||
PendingDownload,
|
||||
Conflict,
|
||||
Deleted,
|
||||
Synced,
|
||||
PendingUpload,
|
||||
PendingDownload,
|
||||
Conflict,
|
||||
Deleted,
|
||||
}
|
||||
|
||||
impl fmt::Display for FileSyncStatus {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
Self::Synced => write!(f, "synced"),
|
||||
Self::PendingUpload => write!(f, "pending_upload"),
|
||||
Self::PendingDownload => write!(f, "pending_download"),
|
||||
Self::Conflict => write!(f, "conflict"),
|
||||
Self::Deleted => write!(f, "deleted"),
|
||||
}
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
Self::Synced => write!(f, "synced"),
|
||||
Self::PendingUpload => write!(f, "pending_upload"),
|
||||
Self::PendingDownload => write!(f, "pending_download"),
|
||||
Self::Conflict => write!(f, "conflict"),
|
||||
Self::Deleted => write!(f, "deleted"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::str::FromStr for FileSyncStatus {
|
||||
type Err = String;
|
||||
type Err = String;
|
||||
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
match s.to_lowercase().as_str() {
|
||||
"synced" => Ok(Self::Synced),
|
||||
"pending_upload" => Ok(Self::PendingUpload),
|
||||
"pending_download" => Ok(Self::PendingDownload),
|
||||
"conflict" => Ok(Self::Conflict),
|
||||
"deleted" => Ok(Self::Deleted),
|
||||
_ => Err(format!("unknown file sync status: {}", s)),
|
||||
}
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
match s.to_lowercase().as_str() {
|
||||
"synced" => Ok(Self::Synced),
|
||||
"pending_upload" => Ok(Self::PendingUpload),
|
||||
"pending_download" => Ok(Self::PendingDownload),
|
||||
"conflict" => Ok(Self::Conflict),
|
||||
"deleted" => Ok(Self::Deleted),
|
||||
_ => Err(format!("unknown file sync status: {}", s)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Sync state for a specific file on a specific device.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct DeviceSyncState {
|
||||
pub device_id: DeviceId,
|
||||
pub path: String,
|
||||
pub local_hash: Option<String>,
|
||||
pub server_hash: Option<String>,
|
||||
pub local_mtime: Option<i64>,
|
||||
pub server_mtime: Option<i64>,
|
||||
pub sync_status: FileSyncStatus,
|
||||
pub last_synced_at: Option<DateTime<Utc>>,
|
||||
pub conflict_info_json: Option<String>,
|
||||
pub device_id: DeviceId,
|
||||
pub path: String,
|
||||
pub local_hash: Option<String>,
|
||||
pub server_hash: Option<String>,
|
||||
pub local_mtime: Option<i64>,
|
||||
pub server_mtime: Option<i64>,
|
||||
pub sync_status: FileSyncStatus,
|
||||
pub last_synced_at: Option<DateTime<Utc>>,
|
||||
pub conflict_info_json: Option<String>,
|
||||
}
|
||||
|
||||
/// A sync conflict that needs resolution.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct SyncConflict {
|
||||
pub id: Uuid,
|
||||
pub device_id: DeviceId,
|
||||
pub path: String,
|
||||
pub local_hash: String,
|
||||
pub local_mtime: i64,
|
||||
pub server_hash: String,
|
||||
pub server_mtime: i64,
|
||||
pub detected_at: DateTime<Utc>,
|
||||
pub resolved_at: Option<DateTime<Utc>>,
|
||||
pub resolution: Option<ConflictResolution>,
|
||||
pub id: Uuid,
|
||||
pub device_id: DeviceId,
|
||||
pub path: String,
|
||||
pub local_hash: String,
|
||||
pub local_mtime: i64,
|
||||
pub server_hash: String,
|
||||
pub server_mtime: i64,
|
||||
pub detected_at: DateTime<Utc>,
|
||||
pub resolved_at: Option<DateTime<Utc>>,
|
||||
pub resolution: Option<ConflictResolution>,
|
||||
}
|
||||
|
||||
impl SyncConflict {
|
||||
pub fn new(
|
||||
device_id: DeviceId,
|
||||
path: String,
|
||||
local_hash: String,
|
||||
local_mtime: i64,
|
||||
server_hash: String,
|
||||
server_mtime: i64,
|
||||
) -> Self {
|
||||
Self {
|
||||
id: Uuid::now_v7(),
|
||||
device_id,
|
||||
path,
|
||||
local_hash,
|
||||
local_mtime,
|
||||
server_hash,
|
||||
server_mtime,
|
||||
detected_at: Utc::now(),
|
||||
resolved_at: None,
|
||||
resolution: None,
|
||||
}
|
||||
pub fn new(
|
||||
device_id: DeviceId,
|
||||
path: String,
|
||||
local_hash: String,
|
||||
local_mtime: i64,
|
||||
server_hash: String,
|
||||
server_mtime: i64,
|
||||
) -> Self {
|
||||
Self {
|
||||
id: Uuid::now_v7(),
|
||||
device_id,
|
||||
path,
|
||||
local_hash,
|
||||
local_mtime,
|
||||
server_hash,
|
||||
server_mtime,
|
||||
detected_at: Utc::now(),
|
||||
resolved_at: None,
|
||||
resolution: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Status of an upload session.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub enum UploadStatus {
|
||||
Pending,
|
||||
InProgress,
|
||||
Completed,
|
||||
Failed,
|
||||
Expired,
|
||||
Cancelled,
|
||||
Pending,
|
||||
InProgress,
|
||||
Completed,
|
||||
Failed,
|
||||
Expired,
|
||||
Cancelled,
|
||||
}
|
||||
|
||||
impl fmt::Display for UploadStatus {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
Self::Pending => write!(f, "pending"),
|
||||
Self::InProgress => write!(f, "in_progress"),
|
||||
Self::Completed => write!(f, "completed"),
|
||||
Self::Failed => write!(f, "failed"),
|
||||
Self::Expired => write!(f, "expired"),
|
||||
Self::Cancelled => write!(f, "cancelled"),
|
||||
}
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
Self::Pending => write!(f, "pending"),
|
||||
Self::InProgress => write!(f, "in_progress"),
|
||||
Self::Completed => write!(f, "completed"),
|
||||
Self::Failed => write!(f, "failed"),
|
||||
Self::Expired => write!(f, "expired"),
|
||||
Self::Cancelled => write!(f, "cancelled"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::str::FromStr for UploadStatus {
|
||||
type Err = String;
|
||||
type Err = String;
|
||||
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
match s.to_lowercase().as_str() {
|
||||
"pending" => Ok(Self::Pending),
|
||||
"in_progress" => Ok(Self::InProgress),
|
||||
"completed" => Ok(Self::Completed),
|
||||
"failed" => Ok(Self::Failed),
|
||||
"expired" => Ok(Self::Expired),
|
||||
"cancelled" => Ok(Self::Cancelled),
|
||||
_ => Err(format!("unknown upload status: {}", s)),
|
||||
}
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
match s.to_lowercase().as_str() {
|
||||
"pending" => Ok(Self::Pending),
|
||||
"in_progress" => Ok(Self::InProgress),
|
||||
"completed" => Ok(Self::Completed),
|
||||
"failed" => Ok(Self::Failed),
|
||||
"expired" => Ok(Self::Expired),
|
||||
"cancelled" => Ok(Self::Cancelled),
|
||||
_ => Err(format!("unknown upload status: {}", s)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A chunked upload session.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct UploadSession {
|
||||
pub id: Uuid,
|
||||
pub device_id: DeviceId,
|
||||
pub target_path: String,
|
||||
pub expected_hash: ContentHash,
|
||||
pub expected_size: u64,
|
||||
pub chunk_size: u64,
|
||||
pub chunk_count: u64,
|
||||
pub status: UploadStatus,
|
||||
pub created_at: DateTime<Utc>,
|
||||
pub expires_at: DateTime<Utc>,
|
||||
pub last_activity: DateTime<Utc>,
|
||||
pub id: Uuid,
|
||||
pub device_id: DeviceId,
|
||||
pub target_path: String,
|
||||
pub expected_hash: ContentHash,
|
||||
pub expected_size: u64,
|
||||
pub chunk_size: u64,
|
||||
pub chunk_count: u64,
|
||||
pub status: UploadStatus,
|
||||
pub created_at: DateTime<Utc>,
|
||||
pub expires_at: DateTime<Utc>,
|
||||
pub last_activity: DateTime<Utc>,
|
||||
}
|
||||
|
||||
impl UploadSession {
|
||||
pub fn new(
|
||||
device_id: DeviceId,
|
||||
target_path: String,
|
||||
expected_hash: ContentHash,
|
||||
expected_size: u64,
|
||||
chunk_size: u64,
|
||||
timeout_hours: u64,
|
||||
) -> Self {
|
||||
let now = Utc::now();
|
||||
let chunk_count = (expected_size + chunk_size - 1) / chunk_size;
|
||||
Self {
|
||||
id: Uuid::now_v7(),
|
||||
device_id,
|
||||
target_path,
|
||||
expected_hash,
|
||||
expected_size,
|
||||
chunk_size,
|
||||
chunk_count,
|
||||
status: UploadStatus::Pending,
|
||||
created_at: now,
|
||||
expires_at: now + chrono::Duration::hours(timeout_hours as i64),
|
||||
last_activity: now,
|
||||
}
|
||||
pub fn new(
|
||||
device_id: DeviceId,
|
||||
target_path: String,
|
||||
expected_hash: ContentHash,
|
||||
expected_size: u64,
|
||||
chunk_size: u64,
|
||||
timeout_hours: u64,
|
||||
) -> Self {
|
||||
let now = Utc::now();
|
||||
let chunk_count = (expected_size + chunk_size - 1) / chunk_size;
|
||||
Self {
|
||||
id: Uuid::now_v7(),
|
||||
device_id,
|
||||
target_path,
|
||||
expected_hash,
|
||||
expected_size,
|
||||
chunk_size,
|
||||
chunk_count,
|
||||
status: UploadStatus::Pending,
|
||||
created_at: now,
|
||||
expires_at: now + chrono::Duration::hours(timeout_hours as i64),
|
||||
last_activity: now,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Information about an uploaded chunk.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct ChunkInfo {
|
||||
pub upload_id: Uuid,
|
||||
pub chunk_index: u64,
|
||||
pub offset: u64,
|
||||
pub size: u64,
|
||||
pub hash: String,
|
||||
pub received_at: DateTime<Utc>,
|
||||
pub upload_id: Uuid,
|
||||
pub chunk_index: u64,
|
||||
pub offset: u64,
|
||||
pub size: u64,
|
||||
pub hash: String,
|
||||
pub received_at: DateTime<Utc>,
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,210 +6,219 @@ use chrono::Utc;
|
|||
use serde::{Deserialize, Serialize};
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::error::Result;
|
||||
use crate::model::{ContentHash, MediaId};
|
||||
use crate::storage::DynStorageBackend;
|
||||
|
||||
use super::{DeviceId, DeviceSyncState, FileSyncStatus, SyncChangeType, SyncLogEntry};
|
||||
use super::{
|
||||
DeviceId,
|
||||
DeviceSyncState,
|
||||
FileSyncStatus,
|
||||
SyncChangeType,
|
||||
SyncLogEntry,
|
||||
};
|
||||
use crate::{
|
||||
error::Result,
|
||||
model::{ContentHash, MediaId},
|
||||
storage::DynStorageBackend,
|
||||
};
|
||||
|
||||
/// Request from client to get changes since a cursor.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct ChangesRequest {
|
||||
pub cursor: i64,
|
||||
pub limit: Option<u64>,
|
||||
pub cursor: i64,
|
||||
pub limit: Option<u64>,
|
||||
}
|
||||
|
||||
/// Response containing changes since the cursor.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct ChangesResponse {
|
||||
pub changes: Vec<SyncLogEntry>,
|
||||
pub cursor: i64,
|
||||
pub has_more: bool,
|
||||
pub changes: Vec<SyncLogEntry>,
|
||||
pub cursor: i64,
|
||||
pub has_more: bool,
|
||||
}
|
||||
|
||||
/// A change reported by the client.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct ClientChange {
|
||||
pub path: String,
|
||||
pub change_type: SyncChangeType,
|
||||
pub content_hash: Option<String>,
|
||||
pub file_size: Option<u64>,
|
||||
pub local_mtime: Option<i64>,
|
||||
pub metadata: Option<serde_json::Value>,
|
||||
pub path: String,
|
||||
pub change_type: SyncChangeType,
|
||||
pub content_hash: Option<String>,
|
||||
pub file_size: Option<u64>,
|
||||
pub local_mtime: Option<i64>,
|
||||
pub metadata: Option<serde_json::Value>,
|
||||
}
|
||||
|
||||
/// Request from client to report local changes.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct ReportChangesRequest {
|
||||
pub device_id: String,
|
||||
pub changes: Vec<ClientChange>,
|
||||
pub device_id: String,
|
||||
pub changes: Vec<ClientChange>,
|
||||
}
|
||||
|
||||
/// Result of processing a client change.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(tag = "status", rename_all = "snake_case")]
|
||||
pub enum ChangeResult {
|
||||
/// Change accepted, no action needed
|
||||
Accepted { path: String },
|
||||
/// Conflict detected, needs resolution
|
||||
Conflict {
|
||||
path: String,
|
||||
server_hash: String,
|
||||
server_mtime: i64,
|
||||
},
|
||||
/// Upload required for new/modified file
|
||||
UploadRequired {
|
||||
path: String,
|
||||
upload_url: String,
|
||||
session_id: String,
|
||||
},
|
||||
/// Error processing change
|
||||
Error { path: String, message: String },
|
||||
/// Change accepted, no action needed
|
||||
Accepted { path: String },
|
||||
/// Conflict detected, needs resolution
|
||||
Conflict {
|
||||
path: String,
|
||||
server_hash: String,
|
||||
server_mtime: i64,
|
||||
},
|
||||
/// Upload required for new/modified file
|
||||
UploadRequired {
|
||||
path: String,
|
||||
upload_url: String,
|
||||
session_id: String,
|
||||
},
|
||||
/// Error processing change
|
||||
Error { path: String, message: String },
|
||||
}
|
||||
|
||||
/// Response to a report changes request.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct ReportChangesResponse {
|
||||
pub results: Vec<ChangeResult>,
|
||||
pub server_cursor: i64,
|
||||
pub results: Vec<ChangeResult>,
|
||||
pub server_cursor: i64,
|
||||
}
|
||||
|
||||
/// Acknowledgment from client that changes have been processed.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct AckRequest {
|
||||
pub device_id: String,
|
||||
pub cursor: i64,
|
||||
pub processed_paths: Vec<String>,
|
||||
pub device_id: String,
|
||||
pub cursor: i64,
|
||||
pub processed_paths: Vec<String>,
|
||||
}
|
||||
|
||||
/// Get changes since a cursor position.
|
||||
pub async fn get_changes(
|
||||
storage: &DynStorageBackend,
|
||||
cursor: i64,
|
||||
limit: u64,
|
||||
storage: &DynStorageBackend,
|
||||
cursor: i64,
|
||||
limit: u64,
|
||||
) -> Result<ChangesResponse> {
|
||||
let limit = limit.min(1000); // Cap at 1000
|
||||
let changes = storage.get_changes_since(cursor, limit + 1).await?;
|
||||
let limit = limit.min(1000); // Cap at 1000
|
||||
let changes = storage.get_changes_since(cursor, limit + 1).await?;
|
||||
|
||||
let has_more = changes.len() > limit as usize;
|
||||
let changes: Vec<_> = changes.into_iter().take(limit as usize).collect();
|
||||
let has_more = changes.len() > limit as usize;
|
||||
let changes: Vec<_> = changes.into_iter().take(limit as usize).collect();
|
||||
|
||||
let new_cursor = changes.last().map(|c| c.sequence).unwrap_or(cursor);
|
||||
let new_cursor = changes.last().map(|c| c.sequence).unwrap_or(cursor);
|
||||
|
||||
Ok(ChangesResponse {
|
||||
changes,
|
||||
cursor: new_cursor,
|
||||
has_more,
|
||||
})
|
||||
Ok(ChangesResponse {
|
||||
changes,
|
||||
cursor: new_cursor,
|
||||
has_more,
|
||||
})
|
||||
}
|
||||
|
||||
/// Record a change in the sync log.
|
||||
pub async fn record_change(
|
||||
storage: &DynStorageBackend,
|
||||
change_type: SyncChangeType,
|
||||
path: &str,
|
||||
media_id: Option<MediaId>,
|
||||
content_hash: Option<&ContentHash>,
|
||||
file_size: Option<u64>,
|
||||
changed_by_device: Option<DeviceId>,
|
||||
storage: &DynStorageBackend,
|
||||
change_type: SyncChangeType,
|
||||
path: &str,
|
||||
media_id: Option<MediaId>,
|
||||
content_hash: Option<&ContentHash>,
|
||||
file_size: Option<u64>,
|
||||
changed_by_device: Option<DeviceId>,
|
||||
) -> Result<SyncLogEntry> {
|
||||
let entry = SyncLogEntry {
|
||||
id: Uuid::now_v7(),
|
||||
sequence: 0, // Will be assigned by database
|
||||
change_type,
|
||||
media_id,
|
||||
path: path.to_string(),
|
||||
content_hash: content_hash.cloned(),
|
||||
file_size,
|
||||
metadata_json: None,
|
||||
changed_by_device,
|
||||
timestamp: Utc::now(),
|
||||
};
|
||||
let entry = SyncLogEntry {
|
||||
id: Uuid::now_v7(),
|
||||
sequence: 0, // Will be assigned by database
|
||||
change_type,
|
||||
media_id,
|
||||
path: path.to_string(),
|
||||
content_hash: content_hash.cloned(),
|
||||
file_size,
|
||||
metadata_json: None,
|
||||
changed_by_device,
|
||||
timestamp: Utc::now(),
|
||||
};
|
||||
|
||||
storage.record_sync_change(&entry).await?;
|
||||
Ok(entry)
|
||||
storage.record_sync_change(&entry).await?;
|
||||
Ok(entry)
|
||||
}
|
||||
|
||||
/// Update device cursor after processing changes.
|
||||
pub async fn update_device_cursor(
|
||||
storage: &DynStorageBackend,
|
||||
device_id: DeviceId,
|
||||
cursor: i64,
|
||||
storage: &DynStorageBackend,
|
||||
device_id: DeviceId,
|
||||
cursor: i64,
|
||||
) -> Result<()> {
|
||||
let mut device = storage.get_device(device_id).await?;
|
||||
device.sync_cursor = Some(cursor);
|
||||
device.last_sync_at = Some(Utc::now());
|
||||
device.updated_at = Utc::now();
|
||||
storage.update_device(&device).await?;
|
||||
Ok(())
|
||||
let mut device = storage.get_device(device_id).await?;
|
||||
device.sync_cursor = Some(cursor);
|
||||
device.last_sync_at = Some(Utc::now());
|
||||
device.updated_at = Utc::now();
|
||||
storage.update_device(&device).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Mark a file as synced for a device.
|
||||
pub async fn mark_synced(
|
||||
storage: &DynStorageBackend,
|
||||
device_id: DeviceId,
|
||||
path: &str,
|
||||
hash: &str,
|
||||
mtime: Option<i64>,
|
||||
storage: &DynStorageBackend,
|
||||
device_id: DeviceId,
|
||||
path: &str,
|
||||
hash: &str,
|
||||
mtime: Option<i64>,
|
||||
) -> Result<()> {
|
||||
let state = DeviceSyncState {
|
||||
device_id,
|
||||
path: path.to_string(),
|
||||
local_hash: Some(hash.to_string()),
|
||||
server_hash: Some(hash.to_string()),
|
||||
local_mtime: mtime,
|
||||
server_mtime: mtime,
|
||||
sync_status: FileSyncStatus::Synced,
|
||||
last_synced_at: Some(Utc::now()),
|
||||
conflict_info_json: None,
|
||||
};
|
||||
let state = DeviceSyncState {
|
||||
device_id,
|
||||
path: path.to_string(),
|
||||
local_hash: Some(hash.to_string()),
|
||||
server_hash: Some(hash.to_string()),
|
||||
local_mtime: mtime,
|
||||
server_mtime: mtime,
|
||||
sync_status: FileSyncStatus::Synced,
|
||||
last_synced_at: Some(Utc::now()),
|
||||
conflict_info_json: None,
|
||||
};
|
||||
|
||||
storage.upsert_device_sync_state(&state).await?;
|
||||
Ok(())
|
||||
storage.upsert_device_sync_state(&state).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Mark a file as pending download for a device.
|
||||
pub async fn mark_pending_download(
|
||||
storage: &DynStorageBackend,
|
||||
device_id: DeviceId,
|
||||
path: &str,
|
||||
server_hash: &str,
|
||||
server_mtime: Option<i64>,
|
||||
storage: &DynStorageBackend,
|
||||
device_id: DeviceId,
|
||||
path: &str,
|
||||
server_hash: &str,
|
||||
server_mtime: Option<i64>,
|
||||
) -> Result<()> {
|
||||
// Get existing state or create new
|
||||
let state = match storage.get_device_sync_state(device_id, path).await? {
|
||||
Some(mut s) => {
|
||||
s.server_hash = Some(server_hash.to_string());
|
||||
s.server_mtime = server_mtime;
|
||||
s.sync_status = FileSyncStatus::PendingDownload;
|
||||
s
|
||||
}
|
||||
None => DeviceSyncState {
|
||||
device_id,
|
||||
path: path.to_string(),
|
||||
local_hash: None,
|
||||
server_hash: Some(server_hash.to_string()),
|
||||
local_mtime: None,
|
||||
server_mtime,
|
||||
sync_status: FileSyncStatus::PendingDownload,
|
||||
last_synced_at: None,
|
||||
conflict_info_json: None,
|
||||
},
|
||||
};
|
||||
// Get existing state or create new
|
||||
let state = match storage.get_device_sync_state(device_id, path).await? {
|
||||
Some(mut s) => {
|
||||
s.server_hash = Some(server_hash.to_string());
|
||||
s.server_mtime = server_mtime;
|
||||
s.sync_status = FileSyncStatus::PendingDownload;
|
||||
s
|
||||
},
|
||||
None => {
|
||||
DeviceSyncState {
|
||||
device_id,
|
||||
path: path.to_string(),
|
||||
local_hash: None,
|
||||
server_hash: Some(server_hash.to_string()),
|
||||
local_mtime: None,
|
||||
server_mtime,
|
||||
sync_status: FileSyncStatus::PendingDownload,
|
||||
last_synced_at: None,
|
||||
conflict_info_json: None,
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
storage.upsert_device_sync_state(&state).await?;
|
||||
Ok(())
|
||||
storage.upsert_device_sync_state(&state).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Generate a device token using UUIDs for randomness.
|
||||
pub fn generate_device_token() -> String {
|
||||
// Concatenate two UUIDs for 256 bits of randomness
|
||||
let uuid1 = uuid::Uuid::new_v4();
|
||||
let uuid2 = uuid::Uuid::new_v4();
|
||||
format!("{}{}", uuid1.simple(), uuid2.simple())
|
||||
// Concatenate two UUIDs for 256 bits of randomness
|
||||
let uuid1 = uuid::Uuid::new_v4();
|
||||
let uuid2 = uuid::Uuid::new_v4();
|
||||
format!("{}{}", uuid1.simple(), uuid2.simple())
|
||||
}
|
||||
|
||||
/// Hash a device token for storage.
|
||||
pub fn hash_device_token(token: &str) -> String {
|
||||
blake3::hash(token.as_bytes()).to_hex().to_string()
|
||||
blake3::hash(token.as_bytes()).to_hex().to_string()
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue