fix: five PR review bugs and folder-recovery UX for missing/renamed folders
BackgroundTasks: dismiss no longer calls clearTaggingJobs; dismissal only updates local dismissed state DuplicateFinder: entering the view clears stale groups when the stored scan scope differs from selectedFolderId; duplicateScanFolderId is recorded on each scan and cache load so scope is always tracked Tagger threshold: set_tagger_threshold now sets TAGGER_SESSION_DIRTY so the worker rebuilds its WdTagger instance and applies the new threshold on the next batch Region search global scope: fetch offset+limit+2 candidates before removing the source image so has_more is accurate across pages Region search pagination: loadMoreImages now sets loadingImages and checks galleryRequestToken before appending results, preventing duplicate pages and stale responses corrupting a new collection Folder recovery: when index_folder detects a missing path it records the error in a new folders.scan_error column (ensure_column migration) and emits done without deleting images, preserving them for recovery. A new update_folder_path command rewrites image paths in the DB before reindexing so thumbnails and embeddings are not needlessly regenerated for unchanged files. The sidebar shows an amber warning icon on affected folders and a recovery banner with Locate Folder and Remove actions
This commit is contained in:
@@ -325,6 +325,37 @@ pub async fn reindex_folder(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn update_folder_path(
|
||||
app: AppHandle,
|
||||
db: State<'_, DbState>,
|
||||
folder_id: i64,
|
||||
new_path: String,
|
||||
) -> Result<(), String> {
|
||||
let new_path_buf = PathBuf::from(&new_path);
|
||||
if !new_path_buf.is_dir() {
|
||||
return Err(format!("Path is not a valid directory: {}", new_path));
|
||||
}
|
||||
let new_name = new_path_buf
|
||||
.file_name()
|
||||
.map(|n| n.to_string_lossy().to_string())
|
||||
.unwrap_or_else(|| new_path.clone());
|
||||
{
|
||||
let conn = db.get().map_err(|e| e.to_string())?;
|
||||
// Fetch the old path before updating so image paths can be rewritten.
|
||||
let old_path = db::get_folders(&conn)
|
||||
.map_err(|e| e.to_string())?
|
||||
.into_iter()
|
||||
.find(|f| f.id == folder_id)
|
||||
.map(|f| f.path)
|
||||
.ok_or("Folder not found")?;
|
||||
db::update_folder_path(&conn, folder_id, &old_path, &new_path, &new_name)
|
||||
.map_err(|e| e.to_string())?;
|
||||
}
|
||||
indexer::index_folder(app, db.inner().clone(), folder_id, new_path_buf);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn find_similar_images(
|
||||
db: State<'_, DbState>,
|
||||
@@ -403,9 +434,10 @@ pub async fn find_similar_by_region(
|
||||
)
|
||||
.map_err(|e| e.to_string())?,
|
||||
None => {
|
||||
let mut ids = vector::search_image_ids_by_embedding(&conn, &embedding, offset + limit + 1)
|
||||
// Fetch one extra candidate to compensate for the source image that
|
||||
// will be removed, so has_more is accurate and results span multiple pages.
|
||||
let mut ids = vector::search_image_ids_by_embedding(&conn, &embedding, offset + limit + 2)
|
||||
.map_err(|e| e.to_string())?;
|
||||
// Exclude the source image from global results
|
||||
ids.retain(|&id| id != params.image_id);
|
||||
ids
|
||||
}
|
||||
|
||||
+37
-2
@@ -30,6 +30,7 @@ pub struct Folder {
|
||||
pub name: String,
|
||||
pub image_count: i64,
|
||||
pub indexed_at: Option<String>,
|
||||
pub scan_error: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
@@ -172,7 +173,8 @@ pub fn migrate(conn: &Connection) -> Result<()> {
|
||||
path TEXT NOT NULL UNIQUE,
|
||||
name TEXT NOT NULL,
|
||||
image_count INTEGER NOT NULL DEFAULT 0,
|
||||
indexed_at TEXT
|
||||
indexed_at TEXT,
|
||||
scan_error TEXT
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS images (
|
||||
@@ -304,6 +306,7 @@ pub fn migrate(conn: &Connection) -> Result<()> {
|
||||
ensure_column(conn, "images", "ai_tagger_model", "TEXT")?;
|
||||
ensure_column(conn, "images", "ai_tagged_at", "TEXT")?;
|
||||
ensure_column(conn, "images", "ai_tagger_error", "TEXT")?;
|
||||
ensure_column(conn, "folders", "scan_error", "TEXT")?;
|
||||
|
||||
vector::migrate(conn)?;
|
||||
Ok(())
|
||||
@@ -1339,7 +1342,7 @@ pub fn get_images_by_ids(conn: &Connection, image_ids: &[i64]) -> Result<Vec<Ima
|
||||
|
||||
pub fn get_folders(conn: &Connection) -> Result<Vec<Folder>> {
|
||||
let mut stmt =
|
||||
conn.prepare("SELECT id, path, name, image_count, indexed_at FROM folders ORDER BY name")?;
|
||||
conn.prepare("SELECT id, path, name, image_count, indexed_at, scan_error FROM folders ORDER BY name")?;
|
||||
let rows = stmt.query_map([], |row| {
|
||||
Ok(Folder {
|
||||
id: row.get(0)?,
|
||||
@@ -1347,11 +1350,43 @@ pub fn get_folders(conn: &Connection) -> Result<Vec<Folder>> {
|
||||
name: row.get(2)?,
|
||||
image_count: row.get(3)?,
|
||||
indexed_at: row.get(4)?,
|
||||
scan_error: row.get(5)?,
|
||||
})
|
||||
})?;
|
||||
Ok(rows.collect::<rusqlite::Result<Vec<_>>>()?)
|
||||
}
|
||||
|
||||
pub fn update_folder_path(conn: &Connection, folder_id: i64, old_path: &str, new_path: &str, new_name: &str) -> Result<()> {
|
||||
conn.execute(
|
||||
"UPDATE folders SET path = ?2, name = ?3, scan_error = NULL WHERE id = ?1",
|
||||
params![folder_id, new_path, new_name],
|
||||
)?;
|
||||
// Rewrite image paths so the indexer can match them by path and skip
|
||||
// re-generating thumbnails and embeddings for unchanged files.
|
||||
// SQLite's replace() does a literal prefix substitution on each path.
|
||||
conn.execute(
|
||||
"UPDATE images SET path = replace(path, ?1, ?2) WHERE folder_id = ?3",
|
||||
params![old_path, new_path, folder_id],
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn set_folder_scan_error(conn: &Connection, folder_id: i64, error: &str) -> Result<()> {
|
||||
conn.execute(
|
||||
"UPDATE folders SET scan_error = ?2 WHERE id = ?1",
|
||||
params![folder_id, error],
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn clear_folder_scan_error(conn: &Connection, folder_id: i64) -> Result<()> {
|
||||
conn.execute(
|
||||
"UPDATE folders SET scan_error = NULL WHERE id = ?1",
|
||||
[folder_id],
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn get_images(
|
||||
conn: &Connection,
|
||||
folder_id: Option<i64>,
|
||||
|
||||
@@ -147,11 +147,39 @@ pub struct MediaJobProgressEvent {
|
||||
|
||||
pub fn index_folder(app: AppHandle, pool: DbPool, folder_id: i64, folder_path: PathBuf) {
|
||||
std::thread::spawn(move || {
|
||||
set_folder_indexing_state(folder_id, true);
|
||||
|
||||
// If the folder path no longer exists on disk, record the error and
|
||||
// emit done. Images are intentionally kept in the DB so the user can
|
||||
// choose to relocate the folder or remove it explicitly — they should
|
||||
// not be silently destroyed.
|
||||
if !folder_path.is_dir() {
|
||||
let error_msg = format!("Folder not found: {}", folder_path.display());
|
||||
eprintln!("Indexing error for folder {}: {}", folder_id, error_msg);
|
||||
if let Ok(conn) = pool.get() {
|
||||
let _ = db::set_folder_scan_error(&conn, folder_id, &error_msg);
|
||||
}
|
||||
emit_progress(
|
||||
&app,
|
||||
&IndexProgress {
|
||||
folder_id,
|
||||
total: 0,
|
||||
indexed: 0,
|
||||
current_file: String::new(),
|
||||
done: true,
|
||||
},
|
||||
);
|
||||
set_folder_indexing_state(folder_id, false);
|
||||
return;
|
||||
}
|
||||
|
||||
let storage_profile = detect_storage_profile(&folder_path);
|
||||
set_folder_storage_profile(folder_id, RuntimeAdaptiveProfile::new(storage_profile));
|
||||
set_folder_indexing_state(folder_id, true);
|
||||
if let Err(error) = do_index(app.clone(), pool, folder_id, folder_path) {
|
||||
if let Err(error) = do_index(app.clone(), &pool, folder_id, folder_path) {
|
||||
eprintln!("Indexing error for folder {}: {}", folder_id, error);
|
||||
if let Ok(conn) = pool.get() {
|
||||
let _ = db::set_folder_scan_error(&conn, folder_id, &error.to_string());
|
||||
}
|
||||
// Always emit done so the frontend reloads and recovers from partial state.
|
||||
emit_progress(
|
||||
&app,
|
||||
@@ -246,7 +274,7 @@ pub fn start_tagging_worker(app: AppHandle, pool: DbPool, app_data_dir: PathBuf)
|
||||
});
|
||||
}
|
||||
|
||||
fn do_index(app: AppHandle, pool: DbPool, folder_id: i64, folder_path: PathBuf) -> Result<()> {
|
||||
fn do_index(app: AppHandle, pool: &DbPool, folder_id: i64, folder_path: PathBuf) -> Result<()> {
|
||||
let existing_entries = {
|
||||
let conn = pool.get()?;
|
||||
db::get_folder_media_index(&conn, folder_id)?
|
||||
@@ -344,6 +372,7 @@ fn do_index(app: AppHandle, pool: DbPool, folder_id: i64, folder_path: PathBuf)
|
||||
}
|
||||
let _ = db::backfill_embedding_jobs(&conn)?;
|
||||
db::update_folder_count(&conn, folder_id)?;
|
||||
let _ = db::clear_folder_scan_error(&conn, folder_id);
|
||||
}
|
||||
|
||||
emit_progress(
|
||||
|
||||
@@ -133,6 +133,7 @@ pub fn run() {
|
||||
commands::find_duplicates,
|
||||
commands::load_duplicate_scan_cache,
|
||||
commands::delete_images_from_disk,
|
||||
commands::update_folder_path,
|
||||
])
|
||||
.run(tauri::generate_context!())
|
||||
.expect("error while running tauri application");
|
||||
|
||||
@@ -191,6 +191,7 @@ pub fn set_tagger_threshold(app_data_dir: &Path, threshold: f32) -> Result<f32>
|
||||
std::fs::create_dir_all(parent)?;
|
||||
}
|
||||
std::fs::write(path, clamped.to_string())?;
|
||||
TAGGER_SESSION_DIRTY.store(true, Ordering::Relaxed);
|
||||
Ok(clamped)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user