| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
pub mod direct; |
| 11 |
|
| 12 |
use std::path::{Path, PathBuf}; |
| 13 |
|
| 14 |
use audiofiles_core::analysis::config::AnalysisConfig; |
| 15 |
use audiofiles_core::analysis::suggest::TagSuggestion; |
| 16 |
use audiofiles_core::analysis::waveform::WaveformData; |
| 17 |
use audiofiles_core::analysis::AnalysisResult; |
| 18 |
use audiofiles_core::edit::EditOperation; |
| 19 |
use audiofiles_core::export::profile::DeviceProfileSummary; |
| 20 |
use audiofiles_core::export::{ExportConfig, ExportItem}; |
| 21 |
use audiofiles_core::forge::{ChopMethod, ConformResult, ConformTarget}; |
| 22 |
use audiofiles_core::search::SearchFilter; |
| 23 |
use audiofiles_core::collections::Collection; |
| 24 |
use audiofiles_core::vfs::{Vfs, VfsNode, VfsNodeWithAnalysis}; |
| 25 |
use audiofiles_core::{CollectionId, NodeId, VfsId}; |
| 26 |
|
| 27 |
pub use direct::DirectBackend; |
| 28 |
|
| 29 |
|
| 30 |
pub type BackendResult<T> = Result<T, BackendError>; |
| 31 |
|
| 32 |
|
| 33 |
#[derive(Debug, thiserror::Error)] |
| 34 |
pub enum BackendError { |
| 35 |
#[error("{0}")] |
| 36 |
Core(#[from] audiofiles_core::error::CoreError), |
| 37 |
|
| 38 |
#[error("{0}")] |
| 39 |
Other(String), |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
#[derive(Debug, serde::Serialize, serde::Deserialize)] |
| 47 |
pub enum BackendEvent { |
| 48 |
|
| 49 |
ImportWalkProgress { |
| 50 |
count: usize, |
| 51 |
total_bytes: u64, |
| 52 |
}, |
| 53 |
ImportWalkComplete { |
| 54 |
total: usize, |
| 55 |
total_bytes: u64, |
| 56 |
}, |
| 57 |
ImportProgress { |
| 58 |
completed: usize, |
| 59 |
total: usize, |
| 60 |
current_name: String, |
| 61 |
}, |
| 62 |
ImportFileError { |
| 63 |
path: String, |
| 64 |
error: String, |
| 65 |
}, |
| 66 |
ImportComplete { |
| 67 |
imported: Vec<(String, String)>, |
| 68 |
total_files: usize, |
| 69 |
errors: usize, |
| 70 |
duplicates: usize, |
| 71 |
folders: Vec<ImportedFolderDesc>, |
| 72 |
}, |
| 73 |
|
| 74 |
|
| 75 |
AnalysisProgress { |
| 76 |
completed: usize, |
| 77 |
total: usize, |
| 78 |
current_name: String, |
| 79 |
}, |
| 80 |
AnalysisSampleDone { |
| 81 |
result: Box<AnalysisResult>, |
| 82 |
suggestions: Vec<TagSuggestion>, |
| 83 |
}, |
| 84 |
AnalysisSampleError { |
| 85 |
hash: String, |
| 86 |
error: String, |
| 87 |
}, |
| 88 |
AnalysisBatchComplete, |
| 89 |
|
| 90 |
|
| 91 |
ExportProgress { |
| 92 |
completed: usize, |
| 93 |
total: usize, |
| 94 |
current_name: String, |
| 95 |
}, |
| 96 |
ExportComplete { |
| 97 |
total: usize, |
| 98 |
errors: Vec<(String, String)>, |
| 99 |
}, |
| 100 |
|
| 101 |
|
| 102 |
CleanupProgress { |
| 103 |
completed: usize, |
| 104 |
total: usize, |
| 105 |
current_name: String, |
| 106 |
}, |
| 107 |
CleanupComplete { |
| 108 |
removed: usize, |
| 109 |
errors: usize, |
| 110 |
}, |
| 111 |
|
| 112 |
|
| 113 |
EditStarted { |
| 114 |
hash: String, |
| 115 |
}, |
| 116 |
EditComplete { |
| 117 |
source_hash: String, |
| 118 |
result_path: std::path::PathBuf, |
| 119 |
operation: EditOperation, |
| 120 |
}, |
| 121 |
EditError { |
| 122 |
hash: String, |
| 123 |
error: String, |
| 124 |
}, |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)] |
| 129 |
pub struct ImportedFolderDesc { |
| 130 |
pub name: String, |
| 131 |
pub samples: Vec<(String, String)>, |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)] |
| 136 |
pub enum ImportStrategyDesc { |
| 137 |
Flat { vfs_id: VfsId, parent_id: Option<NodeId> }, |
| 138 |
NewVfs { vfs_name: String }, |
| 139 |
MergeIntoVfs { vfs_id: VfsId, parent_id: Option<NodeId> }, |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
pub type ExportItemDesc = ExportItem; |
| 144 |
|
| 145 |
|
| 146 |
pub type ExportConfigDesc = ExportConfig; |
| 147 |
|
| 148 |
|
| 149 |
#[derive(Debug, Clone, Default)] |
| 150 |
pub struct StorageStats { |
| 151 |
pub sample_count: u64, |
| 152 |
pub total_bytes: u64, |
| 153 |
pub db_bytes: u64, |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
pub const FORGE_AUTO_TRIM_OVERSHOOT_KEY: &str = "forge.auto_trim_overshoot"; |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
pub trait Backend: Send + Sync { |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
fn list_vfs(&self) -> BackendResult<Vec<Vfs>>; |
| 172 |
|
| 173 |
|
| 174 |
fn create_vfs(&self, name: &str) -> BackendResult<VfsId>; |
| 175 |
|
| 176 |
|
| 177 |
fn rename_vfs(&self, id: VfsId, new_name: &str) -> BackendResult<()>; |
| 178 |
|
| 179 |
|
| 180 |
fn delete_vfs(&self, id: VfsId) -> BackendResult<()>; |
| 181 |
|
| 182 |
|
| 183 |
fn list_children_enriched( |
| 184 |
&self, |
| 185 |
vfs_id: VfsId, |
| 186 |
parent_id: Option<NodeId>, |
| 187 |
) -> BackendResult<Vec<VfsNodeWithAnalysis>>; |
| 188 |
|
| 189 |
|
| 190 |
fn list_children( |
| 191 |
&self, |
| 192 |
vfs_id: VfsId, |
| 193 |
parent_id: Option<NodeId>, |
| 194 |
) -> BackendResult<Vec<VfsNode>>; |
| 195 |
|
| 196 |
|
| 197 |
fn create_directory( |
| 198 |
&self, |
| 199 |
vfs_id: VfsId, |
| 200 |
parent_id: Option<NodeId>, |
| 201 |
name: &str, |
| 202 |
) -> BackendResult<NodeId>; |
| 203 |
|
| 204 |
|
| 205 |
fn create_sample_link( |
| 206 |
&self, |
| 207 |
vfs_id: VfsId, |
| 208 |
parent_id: Option<NodeId>, |
| 209 |
name: &str, |
| 210 |
sample_hash: &str, |
| 211 |
) -> BackendResult<NodeId>; |
| 212 |
|
| 213 |
|
| 214 |
fn get_node(&self, id: NodeId) -> BackendResult<VfsNode>; |
| 215 |
|
| 216 |
|
| 217 |
fn get_breadcrumb(&self, node_id: NodeId) -> BackendResult<Vec<VfsNode>>; |
| 218 |
|
| 219 |
|
| 220 |
fn rename_node(&self, id: NodeId, new_name: &str) -> BackendResult<()>; |
| 221 |
|
| 222 |
|
| 223 |
fn move_node(&self, id: NodeId, new_parent_id: Option<NodeId>) -> BackendResult<()>; |
| 224 |
|
| 225 |
|
| 226 |
fn delete_node(&self, id: NodeId) -> BackendResult<()>; |
| 227 |
|
| 228 |
|
| 229 |
fn restore_node(&self, node: &VfsNode) -> BackendResult<()>; |
| 230 |
|
| 231 |
|
| 232 |
fn collect_subtree(&self, node_id: NodeId) -> BackendResult<Vec<VfsNode>>; |
| 233 |
|
| 234 |
|
| 235 |
fn list_all_directories(&self, vfs_id: VfsId) -> BackendResult<Vec<(NodeId, String)>>; |
| 236 |
|
| 237 |
|
| 238 |
fn find_nodes_by_hashes( |
| 239 |
&self, |
| 240 |
vfs_id: VfsId, |
| 241 |
hashes: &[&str], |
| 242 |
) -> BackendResult<Vec<VfsNodeWithAnalysis>>; |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
fn add_tag(&self, hash: &str, tag: &str) -> BackendResult<()>; |
| 248 |
|
| 249 |
|
| 250 |
fn remove_tag(&self, hash: &str, tag: &str) -> BackendResult<()>; |
| 251 |
|
| 252 |
|
| 253 |
fn get_sample_tags(&self, hash: &str) -> BackendResult<Vec<String>>; |
| 254 |
|
| 255 |
|
| 256 |
fn list_all_tags(&self) -> BackendResult<Vec<String>>; |
| 257 |
|
| 258 |
|
| 259 |
fn bulk_add_tag(&self, hashes: &[&str], tag: &str) -> BackendResult<usize>; |
| 260 |
|
| 261 |
|
| 262 |
fn bulk_remove_tag(&self, hashes: &[&str], tag: &str) -> BackendResult<usize>; |
| 263 |
|
| 264 |
|
| 265 |
fn rename_tag_globally(&self, old_tag: &str, new_tag: &str) -> BackendResult<usize>; |
| 266 |
|
| 267 |
|
| 268 |
fn count_samples_with_tag(&self, tag: &str) -> BackendResult<usize>; |
| 269 |
|
| 270 |
|
| 271 |
fn remove_tag_globally(&self, tag: &str) -> BackendResult<usize>; |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
fn search_in_folder( |
| 277 |
&self, |
| 278 |
filter: &SearchFilter, |
| 279 |
vfs_id: VfsId, |
| 280 |
parent_id: Option<NodeId>, |
| 281 |
) -> BackendResult<Vec<VfsNodeWithAnalysis>>; |
| 282 |
|
| 283 |
|
| 284 |
fn search_global(&self, filter: &SearchFilter) -> BackendResult<Vec<VfsNodeWithAnalysis>>; |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
fn list_collections(&self) -> BackendResult<Vec<Collection>>; |
| 292 |
|
| 293 |
|
| 294 |
fn create_collection(&self, name: &str, description: Option<&str>) -> BackendResult<CollectionId>; |
| 295 |
|
| 296 |
|
| 297 |
fn create_dynamic_collection(&self, name: &str, filter: &SearchFilter) -> BackendResult<CollectionId>; |
| 298 |
|
| 299 |
|
| 300 |
fn rename_collection(&self, id: CollectionId, new_name: &str) -> BackendResult<()>; |
| 301 |
|
| 302 |
|
| 303 |
fn delete_collection(&self, id: CollectionId) -> BackendResult<()>; |
| 304 |
|
| 305 |
|
| 306 |
fn add_to_collection(&self, collection_id: CollectionId, sample_hash: &str) -> BackendResult<()>; |
| 307 |
|
| 308 |
|
| 309 |
fn remove_from_collection(&self, collection_id: CollectionId, sample_hash: &str) -> BackendResult<()>; |
| 310 |
|
| 311 |
|
| 312 |
fn list_collection_members(&self, collection_id: CollectionId) -> BackendResult<Vec<String>>; |
| 313 |
|
| 314 |
|
| 315 |
fn get_sample_collections(&self, sample_hash: &str) -> BackendResult<Vec<Collection>>; |
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
fn get_analysis(&self, hash: &str) -> BackendResult<Option<AnalysisResult>>; |
| 321 |
|
| 322 |
|
| 323 |
fn save_analysis(&self, result: &AnalysisResult) -> BackendResult<()>; |
| 324 |
|
| 325 |
|
| 326 |
fn get_waveform(&self, hash: &str) -> BackendResult<Option<WaveformData>>; |
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
fn find_similar( |
| 332 |
&self, |
| 333 |
hash: &str, |
| 334 |
limit: usize, |
| 335 |
) -> BackendResult<Vec<audiofiles_core::similarity::SimilarResult>>; |
| 336 |
|
| 337 |
|
| 338 |
fn find_near_duplicates( |
| 339 |
&self, |
| 340 |
hash: &str, |
| 341 |
limit: usize, |
| 342 |
) -> BackendResult<Vec<audiofiles_core::fingerprint::DuplicateResult>>; |
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
fn import_file(&self, path: &Path) -> BackendResult<String>; |
| 348 |
|
| 349 |
|
| 350 |
fn sample_path(&self, hash: &str, ext: &str) -> BackendResult<PathBuf>; |
| 351 |
|
| 352 |
|
| 353 |
fn sample_extension(&self, hash: &str) -> BackendResult<String>; |
| 354 |
|
| 355 |
|
| 356 |
fn sample_original_name(&self, hash: &str) -> BackendResult<String>; |
| 357 |
|
| 358 |
|
| 359 |
fn remove_sample(&self, hash: &str) -> BackendResult<()>; |
| 360 |
|
| 361 |
|
| 362 |
fn remove_orphaned_samples(&self) -> BackendResult<usize>; |
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
fn cleanup_orphans_local(&self) -> BackendResult<usize>; |
| 371 |
|
| 372 |
|
| 373 |
fn sample_source_path(&self, hash: &str) -> BackendResult<Option<String>>; |
| 374 |
|
| 375 |
|
| 376 |
fn relocate_sample(&self, hash: &str, new_path: &Path) -> BackendResult<()>; |
| 377 |
|
| 378 |
|
| 379 |
fn check_vault_integrity(&self) -> BackendResult<(usize, usize)>; |
| 380 |
|
| 381 |
|
| 382 |
fn purge_missing_loose_files(&self) -> BackendResult<usize>; |
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
fn relocate_missing_loose_files( |
| 389 |
&self, |
| 390 |
search_root: &std::path::Path, |
| 391 |
) -> BackendResult<(usize, usize)>; |
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
fn collect_export_items( |
| 397 |
&self, |
| 398 |
vfs_id: VfsId, |
| 399 |
parent_id: Option<NodeId>, |
| 400 |
) -> BackendResult<Vec<ExportItem>>; |
| 401 |
|
| 402 |
|
| 403 |
fn enrich_export_with_tags(&self, items: &mut [ExportItem]) -> BackendResult<()>; |
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
fn list_device_profiles(&self) -> BackendResult<Vec<DeviceProfileSummary>>; |
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
fn device_conform_target( |
| 414 |
&self, |
| 415 |
profile_name: &str, |
| 416 |
source_rate: u32, |
| 417 |
) -> BackendResult<Option<ConformTarget>>; |
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
fn compute_chop_preview( |
| 424 |
&self, |
| 425 |
hash: &str, |
| 426 |
ext: &str, |
| 427 |
method: &ChopMethod, |
| 428 |
) -> BackendResult<Vec<f32>>; |
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
fn chop_sample( |
| 433 |
&self, |
| 434 |
vfs_id: VfsId, |
| 435 |
hash: &str, |
| 436 |
ext: &str, |
| 437 |
name: &str, |
| 438 |
parent_id: Option<NodeId>, |
| 439 |
method: &ChopMethod, |
| 440 |
) -> BackendResult<usize>; |
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
fn conform_sample( |
| 448 |
&self, |
| 449 |
vfs_id: VfsId, |
| 450 |
hash: &str, |
| 451 |
ext: &str, |
| 452 |
name: &str, |
| 453 |
parent_id: Option<NodeId>, |
| 454 |
target: &ConformTarget, |
| 455 |
) -> BackendResult<ConformResult>; |
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
fn get_config(&self, key: &str) -> BackendResult<Option<String>>; |
| 461 |
|
| 462 |
|
| 463 |
fn set_config(&self, key: &str, value: &str) -> BackendResult<()>; |
| 464 |
|
| 465 |
|
| 466 |
fn delete_config(&self, key: &str) -> BackendResult<()>; |
| 467 |
|
| 468 |
|
| 469 |
fn set_vfs_sync_files(&self, id: VfsId, enabled: bool) -> BackendResult<()>; |
| 470 |
|
| 471 |
|
| 472 |
fn get_vfs_sync_files(&self, id: VfsId) -> BackendResult<bool>; |
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
fn sync_vfs_mirror(&self, mirror_root: &Path) -> BackendResult<(usize, usize, usize)>; |
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
fn start_import( |
| 484 |
&self, |
| 485 |
source: &Path, |
| 486 |
strategy: ImportStrategyDesc, |
| 487 |
) -> BackendResult<()>; |
| 488 |
|
| 489 |
|
| 490 |
fn start_analysis( |
| 491 |
&self, |
| 492 |
samples: Vec<(String, String)>, |
| 493 |
config: AnalysisConfig, |
| 494 |
) -> BackendResult<()>; |
| 495 |
|
| 496 |
|
| 497 |
fn start_export( |
| 498 |
&self, |
| 499 |
items: Vec<ExportItemDesc>, |
| 500 |
config: ExportConfigDesc, |
| 501 |
) -> BackendResult<()>; |
| 502 |
|
| 503 |
|
| 504 |
fn cancel_import(&self) -> BackendResult<()>; |
| 505 |
|
| 506 |
|
| 507 |
fn cancel_analysis(&self) -> BackendResult<()>; |
| 508 |
|
| 509 |
|
| 510 |
fn cancel_export(&self) -> BackendResult<()>; |
| 511 |
|
| 512 |
|
| 513 |
fn start_edit(&self, hash: &str, ext: &str, operation: EditOperation) -> BackendResult<()>; |
| 514 |
|
| 515 |
|
| 516 |
fn cancel_edit(&self) -> BackendResult<()>; |
| 517 |
|
| 518 |
|
| 519 |
fn start_cleanup(&self) -> BackendResult<()>; |
| 520 |
|
| 521 |
|
| 522 |
fn cancel_cleanup(&self) -> BackendResult<()>; |
| 523 |
|
| 524 |
|
| 525 |
fn record_edit_history( |
| 526 |
&self, |
| 527 |
source_hash: &str, |
| 528 |
result_hash: &str, |
| 529 |
operation: &EditOperation, |
| 530 |
) -> BackendResult<()>; |
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
fn delete_edit_history( |
| 536 |
&self, |
| 537 |
source_hash: &str, |
| 538 |
result_hash: &str, |
| 539 |
) -> BackendResult<()>; |
| 540 |
|
| 541 |
|
| 542 |
fn storage_stats(&self) -> BackendResult<StorageStats>; |
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
fn vfs_storage_stats(&self, vfs_id: audiofiles_core::VfsId) -> BackendResult<(u64, u64)>; |
| 548 |
|
| 549 |
|
| 550 |
fn poll_events(&self) -> Vec<BackendEvent>; |
| 551 |
} |
| 552 |
|
| 553 |
#[cfg(test)] |
| 554 |
mod tests { |
| 555 |
use super::*; |
| 556 |
|
| 557 |
#[test] |
| 558 |
fn backend_error_from_core() { |
| 559 |
let core_err = audiofiles_core::error::CoreError::NodeNotFound(NodeId::from(42)); |
| 560 |
let backend_err: BackendError = core_err.into(); |
| 561 |
assert!(backend_err.to_string().contains("42")); |
| 562 |
} |
| 563 |
|
| 564 |
#[test] |
| 565 |
fn backend_error_other() { |
| 566 |
let err = BackendError::Other("test error".to_string()); |
| 567 |
assert_eq!(err.to_string(), "test error"); |
| 568 |
} |
| 569 |
|
| 570 |
#[test] |
| 571 |
fn imported_folder_desc_serializes() { |
| 572 |
let desc = ImportedFolderDesc { |
| 573 |
name: "Drums".to_string(), |
| 574 |
samples: vec![("hash1".to_string(), "wav".to_string())], |
| 575 |
}; |
| 576 |
let json = serde_json::to_string(&desc).unwrap(); |
| 577 |
assert!(json.contains("Drums")); |
| 578 |
} |
| 579 |
|
| 580 |
#[test] |
| 581 |
fn import_strategy_desc_variants() { |
| 582 |
let flat = ImportStrategyDesc::Flat { vfs_id: VfsId::from(1), parent_id: None }; |
| 583 |
let json = serde_json::to_string(&flat).unwrap(); |
| 584 |
assert!(json.contains("Flat")); |
| 585 |
|
| 586 |
let new_vfs = ImportStrategyDesc::NewVfs { vfs_name: "Test".to_string() }; |
| 587 |
let json = serde_json::to_string(&new_vfs).unwrap(); |
| 588 |
assert!(json.contains("Test")); |
| 589 |
} |
| 590 |
} |
| 591 |
|