fix: remove sqlx from API layer, read TTL from TranscodeManager, init local_files from DB on startup

This commit is contained in:
2026-03-16 04:08:52 +01:00
parent d88afbfe2e
commit 50df852416
3 changed files with 54 additions and 19 deletions

View File

@@ -121,6 +121,57 @@ async fn main() -> anyhow::Result<()> {
registry.register("jellyfin", Arc::new(infra::JellyfinMediaProvider::new(cfg)));
}
}
#[cfg(feature = "local-files")]
"local_files" => {
if let k_core::db::DatabasePool::Sqlite(ref sqlite_pool) = db_pool {
if let Ok(cfg_map) = serde_json::from_str::<std::collections::HashMap<String, String>>(&row.config_json) {
if let Some(files_dir) = cfg_map.get("files_dir") {
let transcode_dir = cfg_map.get("transcode_dir")
.filter(|s| !s.is_empty())
.map(std::path::PathBuf::from);
let cleanup_ttl_hours: u32 = cfg_map.get("cleanup_ttl_hours")
.and_then(|s| s.parse().ok())
.unwrap_or(24);
let lf_cfg = infra::LocalFilesConfig {
root_dir: std::path::PathBuf::from(files_dir),
base_url: config.base_url.clone(),
transcode_dir: transcode_dir.clone(),
cleanup_ttl_hours,
};
tracing::info!("Loading local-files provider from DB config at {:?}", files_dir);
let idx = Arc::new(infra::LocalIndex::new(&lf_cfg, sqlite_pool.clone()).await);
local_index = Some(Arc::clone(&idx));
let scan_idx = Arc::clone(&idx);
tokio::spawn(async move { scan_idx.rescan().await; });
let tm = transcode_dir.as_ref().map(|td| {
std::fs::create_dir_all(td).ok();
tracing::info!("Transcoding enabled; cache dir: {:?}", td);
let tm = infra::TranscodeManager::new(td.clone(), cleanup_ttl_hours);
// Load persisted TTL override from transcode_settings table.
let tm_clone = Arc::clone(&tm);
let pool_clone = sqlite_pool.clone();
tokio::spawn(async move {
if let Ok(row) = sqlx::query_as::<_, (i64,)>(
"SELECT cleanup_ttl_hours FROM transcode_settings WHERE id = 1",
)
.fetch_one(&pool_clone)
.await
{
tm_clone.set_cleanup_ttl(row.0 as u32);
}
});
tm
});
registry.register(
"local",
Arc::new(infra::LocalFilesProvider::new(Arc::clone(&idx), lf_cfg, tm.clone())),
);
transcode_manager = tm;
sqlite_pool_for_state = Some(sqlite_pool.clone());
}
}
}
}
_ => {}
}
}