feat: multi-library foundation (schema + libraries module)

Adds a `libraries` registry table and threads library_id through
per-instance metadata tables (image_exif, photo_insights,
entity_photo_links, video_preview_clips). File-path columns renamed to
rel_path to make the relative-to-root semantics explicit. Adds
content_hash + size_bytes on image_exif to support future hash-keyed
thumbnail/HLS dedup. Tags and favorites stay library-agnostic so they
share across libraries by rel_path.

Behavior is unchanged: a single primary library (id=1) is seeded from
BASE_PATH on first boot; all handlers and DAOs route through it as a
transitional shim until the API gains a library query param.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
Cameron
2026-04-17 15:28:30 -04:00
parent 8bc948b297
commit 00da97fe86
17 changed files with 750 additions and 108 deletions

View File

@@ -3,8 +3,10 @@ use crate::database::{
CalendarEventDao, DailySummaryDao, ExifDao, InsightDao, KnowledgeDao, LocationHistoryDao,
SearchHistoryDao, SqliteCalendarEventDao, SqliteDailySummaryDao, SqliteExifDao,
SqliteInsightDao, SqliteKnowledgeDao, SqliteLocationHistoryDao, SqliteSearchHistoryDao,
connect,
};
use crate::database::{PreviewDao, SqlitePreviewDao};
use crate::libraries::{self, Library};
use crate::tags::{SqliteTagDao, TagDao};
use crate::video::actors::{
PlaylistGenerator, PreviewClipGenerator, StreamActor, VideoPlaylistManager,
@@ -17,6 +19,11 @@ pub struct AppState {
pub stream_manager: Arc<Addr<StreamActor>>,
pub playlist_manager: Arc<Addr<VideoPlaylistManager>>,
pub preview_clip_generator: Arc<Addr<PreviewClipGenerator>>,
/// All configured media libraries. Ordered by `id` ascending; the first
/// entry is the primary library.
pub libraries: Vec<Library>,
/// Legacy shim equal to `libraries[0].root_path`. Phase 2 transitional —
/// new code should go through `primary_library()`.
pub base_path: String,
pub thumbnail_path: String,
pub video_path: String,
@@ -28,10 +35,26 @@ pub struct AppState {
pub insight_generator: InsightGenerator,
}
impl AppState {
pub fn primary_library(&self) -> &Library {
self.libraries
.first()
.expect("AppState constructed without any libraries")
}
pub fn library_by_id(&self, id: i32) -> Option<&Library> {
self.libraries.iter().find(|l| l.id == id)
}
pub fn library_by_name(&self, name: &str) -> Option<&Library> {
self.libraries.iter().find(|l| l.name == name)
}
}
impl AppState {
pub fn new(
stream_manager: Arc<Addr<StreamActor>>,
base_path: String,
libraries_vec: Vec<Library>,
thumbnail_path: String,
video_path: String,
gif_path: String,
@@ -42,6 +65,11 @@ impl AppState {
insight_generator: InsightGenerator,
preview_dao: Arc<Mutex<Box<dyn PreviewDao>>>,
) -> Self {
assert!(
!libraries_vec.is_empty(),
"AppState::new requires at least one library"
);
let base_path = libraries_vec[0].root_path.clone();
let playlist_generator = PlaylistGenerator::new();
let video_playlist_manager =
VideoPlaylistManager::new(video_path.clone(), playlist_generator.start());
@@ -53,6 +81,7 @@ impl AppState {
stream_manager,
playlist_manager: Arc::new(video_playlist_manager.start()),
preview_clip_generator: Arc::new(preview_clip_generator.start()),
libraries: libraries_vec,
base_path,
thumbnail_path,
video_path,
@@ -122,8 +151,16 @@ impl Default for AppState {
let knowledge_dao: Arc<Mutex<Box<dyn KnowledgeDao>>> =
Arc::new(Mutex::new(Box::new(SqliteKnowledgeDao::new())));
// Load base path
// Load base path and ensure the primary library row reflects it.
let base_path = env::var("BASE_PATH").expect("BASE_PATH was not set in the env");
let mut seed_conn = connect();
libraries::seed_or_patch_from_env(&mut seed_conn, &base_path);
let libraries_vec = libraries::load_all(&mut seed_conn);
assert!(
!libraries_vec.is_empty(),
"libraries table is empty after seed_or_patch_from_env"
);
drop(seed_conn);
// Initialize InsightGenerator with all data sources
let insight_generator = InsightGenerator::new(
@@ -148,7 +185,7 @@ impl Default for AppState {
Self::new(
Arc::new(StreamActor {}.start()),
base_path,
libraries_vec,
env::var("THUMBNAILS").expect("THUMBNAILS was not set in the env"),
env::var("VIDEO_PATH").expect("VIDEO_PATH was not set in the env"),
env::var("GIFS_DIRECTORY").expect("GIFS_DIRECTORY was not set in the env"),
@@ -227,9 +264,14 @@ impl AppState {
Arc::new(Mutex::new(Box::new(SqlitePreviewDao::new())));
// Create the AppState with the temporary paths
let test_libraries = vec![Library {
id: crate::libraries::PRIMARY_LIBRARY_ID,
name: "main".to_string(),
root_path: base_path_str.clone(),
}];
AppState::new(
Arc::new(StreamActor {}.start()),
base_path_str,
test_libraries,
thumbnail_path.to_string_lossy().to_string(),
video_path.to_string_lossy().to_string(),
gif_path.to_string_lossy().to_string(),