initial commit
Signed-off-by: NotAShelf <raf@notashelf.dev> Change-Id: I4a6b498153eccd5407510dd541b7f4816a6a6964
This commit is contained in:
commit
6a73d11c4b
124 changed files with 34856 additions and 0 deletions
201
crates/pinakes-core/src/integrity.rs
Normal file
201
crates/pinakes-core/src/integrity.rs
Normal file
|
|
@ -0,0 +1,201 @@
|
|||
use std::path::{Path, PathBuf};
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use tracing::{info, warn};
|
||||
|
||||
use crate::error::Result;
|
||||
use crate::hash::compute_file_hash;
|
||||
use crate::model::{ContentHash, MediaId};
|
||||
use crate::storage::DynStorageBackend;
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct OrphanReport {
|
||||
/// Media items whose files no longer exist on disk.
|
||||
pub orphaned_ids: Vec<MediaId>,
|
||||
/// Files on disk that are not tracked in the database.
|
||||
pub untracked_paths: Vec<PathBuf>,
|
||||
/// Files that appear to have moved (same hash, different path).
|
||||
pub moved_files: Vec<(MediaId, PathBuf, PathBuf)>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub enum OrphanAction {
|
||||
Delete,
|
||||
Ignore,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct VerificationReport {
|
||||
pub verified: usize,
|
||||
pub mismatched: Vec<(MediaId, String, String)>,
|
||||
pub missing: Vec<MediaId>,
|
||||
pub errors: Vec<(MediaId, String)>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub enum IntegrityStatus {
|
||||
Unverified,
|
||||
Verified,
|
||||
Mismatch,
|
||||
Missing,
|
||||
}
|
||||
|
||||
impl std::fmt::Display for IntegrityStatus {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
Self::Unverified => write!(f, "unverified"),
|
||||
Self::Verified => write!(f, "verified"),
|
||||
Self::Mismatch => write!(f, "mismatch"),
|
||||
Self::Missing => write!(f, "missing"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::str::FromStr for IntegrityStatus {
|
||||
type Err = String;
|
||||
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
|
||||
match s {
|
||||
"unverified" => Ok(Self::Unverified),
|
||||
"verified" => Ok(Self::Verified),
|
||||
"mismatch" => Ok(Self::Mismatch),
|
||||
"missing" => Ok(Self::Missing),
|
||||
_ => Err(format!("unknown integrity status: {s}")),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Detect orphaned media items (files that no longer exist on disk).
|
||||
pub async fn detect_orphans(storage: &DynStorageBackend) -> Result<OrphanReport> {
|
||||
let media_paths = storage.list_media_paths().await?;
|
||||
let mut orphaned_ids = Vec::new();
|
||||
let moved_files = Vec::new();
|
||||
|
||||
for (id, path, _hash) in &media_paths {
|
||||
if !path.exists() {
|
||||
orphaned_ids.push(*id);
|
||||
}
|
||||
}
|
||||
|
||||
info!(
|
||||
orphaned = orphaned_ids.len(),
|
||||
total = media_paths.len(),
|
||||
"orphan detection complete"
|
||||
);
|
||||
|
||||
Ok(OrphanReport {
|
||||
orphaned_ids,
|
||||
untracked_paths: Vec::new(),
|
||||
moved_files,
|
||||
})
|
||||
}
|
||||
|
||||
/// Resolve orphaned media items by deleting them from the database.
|
||||
pub async fn resolve_orphans(
|
||||
storage: &DynStorageBackend,
|
||||
action: OrphanAction,
|
||||
ids: &[MediaId],
|
||||
) -> Result<u64> {
|
||||
match action {
|
||||
OrphanAction::Delete => {
|
||||
let count = storage.batch_delete_media(ids).await?;
|
||||
info!(count, "resolved orphans by deletion");
|
||||
Ok(count)
|
||||
}
|
||||
OrphanAction::Ignore => {
|
||||
info!(count = ids.len(), "orphans ignored");
|
||||
Ok(0)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Verify integrity of media files by recomputing hashes and comparing.
|
||||
pub async fn verify_integrity(
|
||||
storage: &DynStorageBackend,
|
||||
media_ids: Option<&[MediaId]>,
|
||||
) -> Result<VerificationReport> {
|
||||
let all_paths = storage.list_media_paths().await?;
|
||||
|
||||
let paths_to_check: Vec<(MediaId, PathBuf, ContentHash)> = if let Some(ids) = media_ids {
|
||||
let id_set: std::collections::HashSet<MediaId> = ids.iter().copied().collect();
|
||||
all_paths
|
||||
.into_iter()
|
||||
.filter(|(id, _, _)| id_set.contains(id))
|
||||
.collect()
|
||||
} else {
|
||||
all_paths
|
||||
};
|
||||
|
||||
let mut report = VerificationReport {
|
||||
verified: 0,
|
||||
mismatched: Vec::new(),
|
||||
missing: Vec::new(),
|
||||
errors: Vec::new(),
|
||||
};
|
||||
|
||||
for (id, path, expected_hash) in paths_to_check {
|
||||
if !path.exists() {
|
||||
report.missing.push(id);
|
||||
continue;
|
||||
}
|
||||
|
||||
match compute_file_hash(&path).await {
|
||||
Ok(actual_hash) => {
|
||||
if actual_hash.0 == expected_hash.0 {
|
||||
report.verified += 1;
|
||||
} else {
|
||||
report
|
||||
.mismatched
|
||||
.push((id, expected_hash.0.clone(), actual_hash.0));
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
report.errors.push((id, e.to_string()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
info!(
|
||||
verified = report.verified,
|
||||
mismatched = report.mismatched.len(),
|
||||
missing = report.missing.len(),
|
||||
errors = report.errors.len(),
|
||||
"integrity verification complete"
|
||||
);
|
||||
|
||||
Ok(report)
|
||||
}
|
||||
|
||||
/// Clean up orphaned thumbnail files that don't correspond to any media item.
|
||||
pub async fn cleanup_orphaned_thumbnails(
|
||||
storage: &DynStorageBackend,
|
||||
thumbnail_dir: &Path,
|
||||
) -> Result<usize> {
|
||||
let media_paths = storage.list_media_paths().await?;
|
||||
let known_ids: std::collections::HashSet<String> = media_paths
|
||||
.iter()
|
||||
.map(|(id, _, _)| id.0.to_string())
|
||||
.collect();
|
||||
|
||||
let mut removed = 0;
|
||||
|
||||
if thumbnail_dir.exists() {
|
||||
let entries = std::fs::read_dir(thumbnail_dir)?;
|
||||
for entry in entries.flatten() {
|
||||
let path = entry.path();
|
||||
if let Some(stem) = path.file_stem().and_then(|s| s.to_str()) {
|
||||
if !known_ids.contains(stem) {
|
||||
if let Err(e) = std::fs::remove_file(&path) {
|
||||
warn!(path = %path.display(), error = %e, "failed to remove orphaned thumbnail");
|
||||
} else {
|
||||
removed += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
info!(removed, "orphaned thumbnail cleanup complete");
|
||||
Ok(removed)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue