Files
furumi-web/src/main.rs
T
sanapachandClaude Opus 5.5 d275e7326b Add the Import library page and wizard to the web player
Import library sits between Followed and Settings in the account menu and
opens #imports: the sources, the history with statistics and links to the
created playlists, and #imports/N with progress, per-playlist results, a
deleted-playlist notice with Create again, and the entries with status and
playlist filters, search, paging and the candidates of doubtful entries to
choose from. Retry unmatched (with the federated search consent), Cancel,
Delete history and a new Spotify sign-in for waiting imports call the
existing /api/imports endpoints. The page polls every 3 s while an import
runs. It follows the player's views, skeletons, dialogs, switches and
phrase table in English and Russian.

The New import wizard picks a source, then the data export or the Spotify
sign-in, then the playlists: imported ones cannot be selected, similar ones
need an explicit confirmation, and federated search is off until the user
turns it on. A ZIP export is opened in the browser with fflate 0.8.2,
vendored with its MIT licence and served at /vendor/fflate-0.8.2.js only
when an archive is picked. Only Playlist*.json and YourLibrary.json are
extracted; archives over 256 MiB, with more than 2,000 entries, more than
256 playlist files or more than 32 MiB declared for them are refused before
anything is decompressed, and a request over 32 MiB is not sent.

Federated tracks can now be removed from a playlist in edit mode:
DELETE /api/player/tracks/content/playlist checks the owner and records the
existing PlaylistTrackRemoved operation. The body-level dialogs get an
Alpine root, which also makes Get app open again.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-26 19:05:30 +02:00

686 lines
26 KiB
Rust

mod admin;
mod agent;
mod api;
mod auth;
mod config;
mod federation;
mod i18n;
mod import_playlists;
mod jobs;
mod lastfm;
mod library_cleanup;
mod library_import;
mod local_uploads;
mod lyrics;
mod media_paths;
mod metrics;
mod music;
mod offline;
mod oidc;
mod player;
mod podcasts;
mod scheduler;
mod search_relevance;
mod similarity;
mod torrents;
mod user;
mod youtube;
use std::sync::Arc;
use cot::auth::PasswordVerificationResult;
use cot::cli::CliMetadata;
use cot::common_types::Password;
use cot::config::{
DatabaseConfig, Expiry, MiddlewareConfig, ProjectConfig, SameSite, SessionMiddlewareConfig,
SessionStoreConfig, SessionStoreTypeConfig,
};
use cot::db::Database;
use cot::form::{Form, FormResult};
use cot::html::Html;
use cot::middleware::SessionMiddleware;
use cot::project::RegisterAppsContext;
use cot::request::extractors::{Path, RequestForm, UrlQuery};
use cot::response::IntoResponse;
use cot::router::method::get;
use cot::router::{Route, Router};
use cot::session::Session;
use cot::static_files::StaticFilesMiddleware;
use cot::{App, AppBuilder, Body, Project, Template};
use serde::Deserialize;
use crate::config::AppConfig;
use crate::i18n::{I18n, Translations};
use crate::scheduler::{JobRegistry, SchedulerHandle};
use crate::user::User;
// ---------------------------------------------------------------------------
// Build the job registry
// ---------------------------------------------------------------------------
fn build_registry() -> Arc<JobRegistry> {
let mut registry = JobRegistry::new();
registry.register(jobs::inbox_discover::InboxDiscoverJob);
registry.register(jobs::inbox_process::InboxProcessJob);
registry.register(jobs::inbox_process::FileProcessJob);
registry.register(jobs::archive_cleanup::ArchiveCleanupJob);
registry.register(jobs::artwork_backfill::ArtworkBackfillJob);
registry.register(jobs::lyrics_backfill::LyricsBackfillJob);
registry.register(jobs::metadata_backfill::MetadataBackfillJob);
registry.register(jobs::lastfm_popularity::LastfmPopularityJob);
registry.register(jobs::lastfm_scrobble::LastfmScrobbleJob);
registry.register(podcasts::PodcastRefreshJob);
registry.register(library_import::LibraryImportJob);
Arc::new(registry)
}
// ---------------------------------------------------------------------------
// Handlers
// ---------------------------------------------------------------------------
async fn index(session: Session, db: Database, i18n: I18n) -> cot::Result<cot::response::Response> {
render_player(session, db, i18n, String::new()).await
}
async fn render_player(
session: Session,
db: Database,
i18n: I18n,
share_token: String,
) -> cot::Result<cot::response::Response> {
let identity = auth::checked_session_identity(&session, &db).await?;
let user = identity.as_ref().filter(|user| user.is_active());
let guest = user.is_none();
if guest && share_token.is_empty() {
if identity.is_some() {
let mut response = Html::new(PendingTemplate { t: i18n.t }.render()?).into_response()?;
offline::identify(&mut response, 0);
return Ok(response);
}
return Ok(auth::redirect("/login"));
}
let (config, _) = AppConfig::load_with_db(&db).await;
let account_id = user.map_or(0, User::id_val);
if !guest {
metrics::record_active_user(account_id);
}
let is_member_shell = !guest && share_token.is_empty();
let mut response = Html::new(
player::PlayerPageTemplate {
t: i18n.t,
guest,
account_id,
share_token,
downloads_enabled: !guest && config.downloads_enabled,
torrent_downloads_enabled: !guest
&& config.downloads_enabled
&& config.torrent_downloads_enabled,
youtube_downloads_enabled: !guest
&& config.downloads_enabled
&& config.youtube_downloads_enabled,
}
.render()?,
)
.into_response()?;
response.headers_mut().insert("Cache-Control", "private, no-store".parse().unwrap());
if is_member_shell {
offline::identify(&mut response, account_id);
}
Ok(response)
}
#[derive(Debug, Template)]
#[template(path = "pending.html")]
struct PendingTemplate {
t: &'static Translations,
}
async fn access_status(session: Session, db: Database) -> cot::Result<cot::response::Response> {
let identity = auth::get_session_identity(&session, &db).await;
let status = match identity {
Some(user) if user.is_active() => "active",
Some(_) => "pending",
None => "anonymous",
};
let mut response = cot::json::Json(serde_json::json!({"status": status})).into_response()?;
response
.headers_mut()
.insert("Cache-Control", "private, no-store".parse().unwrap());
Ok(response)
}
fn is_share_token(token: &str) -> bool {
token.len() == 32 && token.bytes().all(|b| b.is_ascii_hexdigit())
}
#[derive(Deserialize)]
struct SetLangQuery {
lang: String,
next: Option<String>,
}
async fn set_lang(
UrlQuery(query): UrlQuery<SetLangQuery>,
) -> cot::Result<cot::http::Response<Body>> {
let lang = i18n::Lang::from_code(&query.lang).unwrap_or(i18n::Lang::En);
let next = query.next.as_deref().unwrap_or("/");
let response = cot::http::Response::builder()
.status(cot::http::StatusCode::SEE_OTHER)
.header(cot::http::header::LOCATION, next)
.header(cot::http::header::SET_COOKIE, i18n::lang_cookie(lang))
.body(Body::fixed(""))
.expect("valid response");
Ok(response)
}
// ---------------------------------------------------------------------------
// Login page
// ---------------------------------------------------------------------------
#[derive(Debug, Template)]
#[template(path = "login.html")]
struct LoginTemplate {
t: &'static Translations,
auth_password_enabled: bool,
auth_sso_enabled: bool,
oidc_button_text: String,
message: String,
}
async fn login_page_handler(
i18n: I18n,
_startup_config: &AppConfig,
db: Database,
message: String,
) -> cot::Result<Html> {
let (config, _) = AppConfig::load_with_db(&db).await;
let template = LoginTemplate {
t: i18n.t,
auth_password_enabled: config.auth_password_enabled,
auth_sso_enabled: config.auth_sso_enabled,
oidc_button_text: config.oidc_button_text,
message,
};
Ok(Html::new(template.render()?))
}
#[derive(Debug, Form)]
struct LoginForm {
username: String,
password: String,
}
#[derive(Deserialize)]
struct LoginQuery {
error: Option<String>,
next: Option<String>,
}
#[derive(Deserialize)]
struct SharePathToken {
token: String,
}
// ---------------------------------------------------------------------------
// Logout
// ---------------------------------------------------------------------------
async fn logout_handler(session: Session) -> cot::Result<cot::response::Response> {
auth::logout(&session).await?;
Ok(auth::redirect("/login"))
}
async fn metrics_handler(
config: Arc<AppConfig>,
pool: Arc<tokio::sync::OnceCell<sqlx::PgPool>>,
) -> cot::Result<cot::http::Response<Body>> {
if config.database_url.is_empty() {
return Ok(cot::http::Response::builder()
.status(cot::http::StatusCode::SERVICE_UNAVAILABLE)
.header(cot::http::header::CONTENT_TYPE, "text/plain; version=0.0.4")
.body(Body::fixed("furumusic_metrics_unavailable 1\n"))
.expect("valid response"));
}
let pg_pool = pool
.get_or_init(|| async {
sqlx::postgres::PgPoolOptions::new()
.max_connections(2)
.connect(&config.database_url)
.await
.expect("metrics pool")
})
.await;
let body = metrics::render(pg_pool, &config).await;
Ok(cot::http::Response::builder()
.status(cot::http::StatusCode::OK)
.header(cot::http::header::CONTENT_TYPE, "text/plain; version=0.0.4")
.body(Body::fixed(body))
.expect("valid response"))
}
async fn share_handler(
session: Session,
db: Database,
i18n: I18n,
Path(path): Path<SharePathToken>,
) -> cot::Result<cot::response::Response> {
if !is_share_token(&path.token) {
return Ok(cot::http::Response::builder()
.status(cot::http::StatusCode::NOT_FOUND)
.body(Body::fixed("Shared music not found"))
.expect("valid response"));
}
let mut response = render_player(session, db, i18n, path.token).await?;
response
.headers_mut()
.insert("Referrer-Policy", "no-referrer".parse().unwrap());
response
.headers_mut()
.insert("Cache-Control", "private, no-store".parse().unwrap());
Ok(response)
}
// ---------------------------------------------------------------------------
// App
// ---------------------------------------------------------------------------
struct FuruApp {
config: Arc<AppConfig>,
pool: Arc<tokio::sync::OnceCell<sqlx::PgPool>>,
}
impl App for FuruApp {
fn name(&self) -> &'static str {
env!("CARGO_PKG_NAME")
}
fn router(&self) -> Router {
Router::with_urls([
Route::with_handler_and_name(
"/admin",
get(|| async { Ok::<_, cot::Error>(auth::redirect("/admin/")) }),
"admin_redirect",
),
Route::with_handler_and_name(
"/imports",
get(|| async { Ok::<_, cot::Error>(auth::redirect("/#imports")) }),
"imports_redirect",
),
Route::with_handler_and_name(
"/swagger",
get(|| async { Ok::<_, cot::Error>(auth::redirect("/swagger/")) }),
"swagger_redirect",
),
Route::with_handler_and_name(
"/",
|session: Session, db: Database, i18n: I18n| async move {
index(session, db, i18n).await
},
"index",
),
Route::with_handler_and_name("/share/{token}", get(share_handler), "share"),
Route::with_handler_and_name("/auth/status", get(access_status), "access_status"),
Route::with_handler_and_name("/offline-worker.js", get(offline::worker), "offline_worker"),
Route::with_handler_and_name("/offline-catalog.js", get(offline::catalog), "offline_catalog"),
Route::with_handler_and_name("/offline-probe", get(offline::probe), "offline_probe"),
Route::with_handler_and_name("/player.webmanifest", get(offline::manifest), "player_manifest"),
Route::with_handler_and_name("/player-icon.svg", get(offline::icon), "player_icon"),
Route::with_handler_and_name(
"/vendor/fflate-0.8.2.js",
get(offline::fflate),
"vendor_fflate",
),
Route::with_handler_and_name(
"/metrics",
get({
let config = Arc::clone(&self.config);
let pool = Arc::clone(&self.pool);
move || {
let config = Arc::clone(&config);
let pool = Arc::clone(&pool);
async move { metrics_handler(config, pool).await }
}
}),
"metrics",
),
Route::with_handler_and_name(
"/login",
get({
let config = Arc::clone(&self.config);
move |i18n: I18n,
db: Database,
session: Session,
query: UrlQuery<LoginQuery>| {
let config = Arc::clone(&config);
async move {
// No users at all → redirect to first-run setup
if User::count_all(&db).await.unwrap_or(0) == 0 {
return Ok(auth::redirect("/admin/setup"));
}
if let Some(next) = query.0.next.as_deref() {
if next.strip_prefix("/share/").is_some_and(is_share_token) {
auth::remember_post_login_redirect(&session, next).await?;
}
} else {
auth::clear_post_login_redirect(&session).await?;
}
if auth::get_session_identity(&session, &db).await.is_some() {
let destination = auth::get_post_login_redirect(&session)
.await?
.unwrap_or_else(|| "/".into());
return Ok(auth::redirect(&destination));
}
let message = query.0.error.unwrap_or_default();
login_page_handler(i18n, &config, db, message)
.await?
.into_response()
}
}
})
.post({
let config = Arc::clone(&self.config);
move |i18n: I18n,
db: Database,
session: Session,
form: RequestForm<LoginForm>| {
let config = Arc::clone(&config);
async move {
let RequestForm(result) = form;
let data = match result {
FormResult::Ok(data) => data,
FormResult::ValidationError(_) => {
metrics::record_auth_attempt(
"password",
"failure",
"validation_error",
);
let msg = i18n.t.login_invalid.to_owned();
return login_page_handler(i18n, &config, db, msg)
.await?
.into_response();
}
};
let (live_config, _) = AppConfig::load_with_db(&db).await;
if !live_config.auth_password_enabled {
metrics::record_auth_attempt("password", "failure", "disabled");
let msg = i18n.t.login_disabled.to_owned();
return login_page_handler(i18n, &config, db, msg)
.await?
.into_response();
}
// Try to authenticate
if let Ok(Some(user)) = User::get_by_username(&db, &data.username).await
{
if let Some(hash) = user.password_ref() {
let password = Password::new(&data.password);
match hash.verify(&password) {
PasswordVerificationResult::Ok
| PasswordVerificationResult::OkObsolete(_) => {
let redirect_to =
auth::get_post_login_redirect(&session)
.await?
.unwrap_or_else(|| "/".to_string());
auth::login(&session, user.id_val()).await?;
auth::clear_post_login_redirect(&session).await?;
metrics::record_auth_attempt(
"password", "success", "ok",
);
metrics::record_session_created("password");
return Ok(auth::redirect(&redirect_to));
}
PasswordVerificationResult::Invalid => {}
}
}
}
metrics::record_auth_attempt("password", "failure", "bad_credentials");
let msg = i18n.t.login_invalid.to_owned();
login_page_handler(i18n, &config, db, msg)
.await?
.into_response()
}
}
}),
"login",
),
Route::with_handler_and_name("/logout", get(logout_handler), "logout"),
Route::with_handler_and_name("/set-lang", set_lang, "set_lang"),
Route::with_handler_and_name(
"/auth/oidc/start",
get(oidc::oidc_start_handler),
"oidc_start",
),
Route::with_handler_and_name(
"/auth/oidc/callback",
get(oidc::oidc_callback_handler),
"oidc_callback",
),
Route::with_handler_and_name(
"/auth/mobile/oidc/start",
get(oidc::oidc_mobile_start_handler),
"mobile_oidc_start",
),
Route::with_handler_and_name(
"/auth/mobile/oidc/callback",
get(oidc::oidc_mobile_callback_handler),
"mobile_oidc_callback",
),
])
}
}
// ---------------------------------------------------------------------------
// Project
// ---------------------------------------------------------------------------
struct FuruProject {
app_config: Arc<AppConfig>,
registry: Arc<JobRegistry>,
scheduler_handle: Arc<tokio::sync::OnceCell<Arc<SchedulerHandle>>>,
}
impl Project for FuruProject {
fn cli_metadata(&self) -> CliMetadata {
CliMetadata {
description: concat!(
env!("CARGO_PKG_DESCRIPTION"),
"\n\n",
"CONFIGURATION\n",
" All settings are available as FURU_-prefixed environment variables.\n",
" Priority: env var > DB override > compiled default.\n",
"\n",
" Database (required for most features):\n",
" FURU_DATABASE_URL PostgreSQL connection URL\n",
" Example: postgres://user:pass@localhost/furumusic\n",
"\n",
" Server:\n",
" FURU_LOG_LEVEL Tracing filter (default: info)\n",
"\n",
" Authentication:\n",
" FURU_AUTH_PASSWORD_ENABLED Enable password login (default: true)\n",
" FURU_AUTH_SSO_ENABLED Enable SSO/OIDC login (default: false)\n",
" FURU_OIDC_ISSUER OIDC issuer URL\n",
" FURU_OIDC_CLIENT_ID OIDC client ID\n",
" FURU_OIDC_CLIENT_SECRET OIDC client secret\n",
" FURU_OIDC_BUTTON_TEXT SSO button label (default: Sign in with SSO)\n",
" FURU_OIDC_ADMIN_GROUPS OIDC groups that grant admin role\n",
" FURU_OIDC_USER_GROUPS OIDC groups allowed to access the service\n",
"\n",
" API:\n",
" FURU_SWAGGER_ENABLED Enable Swagger UI at /swagger/ (default: false)\n",
"\n",
"QUICK START\n",
" export FURU_DATABASE_URL=postgres://user:pass@localhost/furumusic\n",
" furumusic run",
),
..cot::cli::metadata!()
}
}
fn config(&self, _config_name: &str) -> cot::Result<ProjectConfig> {
let mut builder = ProjectConfig::builder();
builder.debug(cfg!(debug_assertions));
if !self.app_config.database_url.is_empty() {
builder.database(
DatabaseConfig::builder()
.url(self.app_config.database_url.as_str())
.build(),
);
builder.middlewares(
MiddlewareConfig::builder()
.session(
SessionMiddlewareConfig::builder()
.expiry(Expiry::OnInactivity(std::time::Duration::from_secs(
365 * 24 * 60 * 60,
)))
.secure(false)
.same_site(SameSite::Lax)
.store(
SessionStoreConfig::builder()
.store_type(SessionStoreTypeConfig::Database)
.build(),
)
.build(),
)
.build(),
);
}
Ok(builder.build())
}
fn middlewares(
&self,
handler: cot::project::RootHandlerBuilder,
context: &cot::project::MiddlewareContext,
) -> cot::project::RootHandler {
handler
.middleware(metrics::MetricsLayer)
.middleware(StaticFilesMiddleware::from_context(context))
.middleware(SessionMiddleware::from_context(context))
.build()
}
fn register_apps(&self, apps: &mut AppBuilder, _context: &RegisterAppsContext) {
// Spawn the scheduler in background — it runs independently of HTTP
// requests. The OnceCell ensures it starts exactly once.
let sched_cell = Arc::clone(&self.scheduler_handle);
let sched_config = Arc::clone(&self.app_config);
let sched_registry = Arc::clone(&self.registry);
tokio::spawn(async move {
let _ = sched_cell
.get_or_init(|| async {
match scheduler::start_scheduler(&sched_config, sched_registry).await {
Ok(handle) => handle,
Err(e) => {
tracing::error!("Failed to start scheduler: {e:#}");
panic!("scheduler failed to start: {e}");
}
}
})
.await;
});
// Join the federation at boot when it was left enabled (the settings
// live in the config KV table; changes apply live from the admin).
let fed_config = Arc::clone(&self.app_config);
tokio::spawn(async move {
federation::handle().boot(&fed_config).await;
});
// Embedding calculation is an independent, server-wide background
// service. It remains useful locally when federation is disabled.
let similarity_config = Arc::clone(&self.app_config);
tokio::spawn(async move {
similarity::handle().boot(&similarity_config).await;
});
// Resume interrupted imports and restart workers whose lease expired.
library_import::job::spawn_boot(
Arc::clone(&self.app_config),
Arc::clone(&self.scheduler_handle),
);
let lyrics_config = Arc::clone(&self.app_config);
tokio::spawn(async move {
lyrics::search::handle().boot(&lyrics_config).await;
});
apps.register(cot::session::db::SessionApp::new());
apps.register_with_views(
FuruApp {
config: Arc::clone(&self.app_config),
pool: Arc::new(tokio::sync::OnceCell::new()),
},
"",
);
apps.register_with_views(
admin::AdminApp::new(
Arc::clone(&self.app_config),
Arc::clone(&self.registry),
Arc::clone(&self.scheduler_handle),
),
"/admin",
);
apps.register_with_views(api::ApiApp, "/api");
apps.register_with_views(
podcasts::PodcastApp::new(Arc::clone(&self.app_config)),
"/api/podcasts",
);
apps.register_with_views(
library_import::ImportApp::new(
Arc::clone(&self.app_config),
Arc::clone(&self.scheduler_handle),
),
"/api/imports",
);
apps.register_with_views(
player::PlayerApp::new(
Arc::clone(&self.app_config),
Arc::clone(&self.scheduler_handle),
),
"/api/player",
);
if self.app_config.swagger_enabled {
apps.register_with_views(cot::openapi::swagger_ui::SwaggerUi::new(), "/swagger");
}
}
}
// ---------------------------------------------------------------------------
// Entrypoint
// ---------------------------------------------------------------------------
#[cot::main]
fn main() -> impl Project {
let app_config = Arc::new(AppConfig::load());
// Initialise tracing subscriber with the configured log level.
// FURU_LOG_LEVEL (or the default "info") is parsed as an EnvFilter
// directive, so values like "debug", "warn,furumusic=trace" all work.
let filter =
tracing_subscriber::EnvFilter::try_new(&app_config.log_level).unwrap_or_else(|e| {
eprintln!(
"WARNING: invalid FURU_LOG_LEVEL {:?}: {e}; falling back to \"info\"",
app_config.log_level,
);
tracing_subscriber::EnvFilter::new("info")
});
tracing_subscriber::fmt().with_env_filter(filter).init();
tracing::info!("loaded config: {:?}", app_config);
let registry = build_registry();
FuruProject {
app_config,
registry,
scheduler_handle: Arc::new(tokio::sync::OnceCell::new()),
}
}