Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 4b8797bb2e | |||
| d425bf3087 | |||
| 82923c871e |
Generated
+1
-1
@@ -1397,7 +1397,7 @@ checksum = "e6d5a32815ae3f33302d95fdcb2ce17862f8c65363dcfd29360480ba1001fc9c"
|
||||
|
||||
[[package]]
|
||||
name = "furumusic"
|
||||
version = "0.1.12"
|
||||
version = "0.1.14"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"async-trait",
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "furumusic"
|
||||
version = "0.1.12"
|
||||
version = "0.1.15"
|
||||
edition = "2024"
|
||||
description = "Reusable web-app boilerplate: auth, OIDC/SSO, admin panel, user management, i18n, PostgreSQL"
|
||||
|
||||
|
||||
@@ -264,6 +264,20 @@ impl App for AdminApp {
|
||||
}),
|
||||
"admin_v2_job_run",
|
||||
),
|
||||
Route::with_handler_and_name(
|
||||
"/v2/api/settings",
|
||||
get(move |session: Session, db: Database| async move {
|
||||
v2::settings(session, db).await
|
||||
})
|
||||
.post(
|
||||
move |session: Session,
|
||||
db: Database,
|
||||
json: Json<v2::UpdateSettingsRequest>| async move {
|
||||
v2::update_settings(session, db, json).await
|
||||
},
|
||||
),
|
||||
"admin_v2_settings",
|
||||
),
|
||||
Route::with_handler_and_name(
|
||||
"/v2/api/jobs/{name}/toggle",
|
||||
cot::router::method::post({
|
||||
|
||||
+44
-1
@@ -1,6 +1,6 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use cot::db::Database;
|
||||
use cot::db::{Database, Model};
|
||||
use cot::html::Html;
|
||||
use cot::http::StatusCode;
|
||||
use cot::http::header::CONTENT_TYPE;
|
||||
@@ -14,6 +14,7 @@ use sqlx::{PgPool, Postgres, QueryBuilder};
|
||||
|
||||
use super::BUILD_INFO;
|
||||
use crate::auth::{self, AuthenticatedUser, Role};
|
||||
use crate::config::{AppConfig, ConfigEntry};
|
||||
use crate::i18n::{I18n, Translations};
|
||||
use crate::scheduler::{JobRegistry, ScheduledJob};
|
||||
|
||||
@@ -214,6 +215,17 @@ struct MutationResponse {
|
||||
affected: u64,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, JsonSchema)]
|
||||
struct AdminSettingsDto {
|
||||
lastfm_api_key: String,
|
||||
lastfm_api_key_configured: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub(super) struct UpdateSettingsRequest {
|
||||
lastfm_api_key: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, JsonSchema)]
|
||||
struct LibraryOverviewDto {
|
||||
artists: i64,
|
||||
@@ -458,6 +470,37 @@ pub async fn jobs(
|
||||
Json(jobs).into_response()
|
||||
}
|
||||
|
||||
pub async fn settings(session: Session, db: Database) -> cot::Result<cot::response::Response> {
|
||||
if let Err(response) = require_admin_json(&session, &db).await {
|
||||
return Ok(response);
|
||||
}
|
||||
let (config, _) = AppConfig::load_with_db(&db).await;
|
||||
Json(AdminSettingsDto {
|
||||
lastfm_api_key_configured: !config.lastfm_api_key.trim().is_empty(),
|
||||
lastfm_api_key: config.lastfm_api_key,
|
||||
})
|
||||
.into_response()
|
||||
}
|
||||
|
||||
pub async fn update_settings(
|
||||
session: Session,
|
||||
db: Database,
|
||||
Json(body): Json<UpdateSettingsRequest>,
|
||||
) -> cot::Result<cot::response::Response> {
|
||||
if let Err(response) = require_admin_json(&session, &db).await {
|
||||
return Ok(response);
|
||||
}
|
||||
let mut entry = ConfigEntry::new(
|
||||
"lastfm_api_key".to_string(),
|
||||
body.lastfm_api_key.trim().to_string(),
|
||||
);
|
||||
entry
|
||||
.save(&db)
|
||||
.await
|
||||
.map_err(|e| cot::Error::internal(e.to_string()))?;
|
||||
Json(serde_json::json!({ "ok": true })).into_response()
|
||||
}
|
||||
|
||||
pub async fn run_job(
|
||||
session: Session,
|
||||
db: Database,
|
||||
|
||||
@@ -133,6 +133,7 @@ pub struct ConfigSources {
|
||||
pub agent_confidence_threshold: ConfigSource,
|
||||
pub agent_context_limit: ConfigSource,
|
||||
pub agent_concurrency: ConfigSource,
|
||||
pub lastfm_api_key: ConfigSource,
|
||||
}
|
||||
|
||||
impl Default for ConfigSources {
|
||||
@@ -158,6 +159,7 @@ impl Default for ConfigSources {
|
||||
agent_confidence_threshold: ConfigSource::Default,
|
||||
agent_context_limit: ConfigSource::Default,
|
||||
agent_concurrency: ConfigSource::Default,
|
||||
lastfm_api_key: ConfigSource::Default,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -262,6 +264,8 @@ pub struct AppConfig {
|
||||
pub agent_context_limit: u64,
|
||||
/// Number of files to process in parallel via the LLM.
|
||||
pub agent_concurrency: u64,
|
||||
/// Last.fm API key for weekly popularity enrichment.
|
||||
pub lastfm_api_key: String,
|
||||
}
|
||||
|
||||
impl Default for AppConfig {
|
||||
@@ -287,6 +291,7 @@ impl Default for AppConfig {
|
||||
agent_confidence_threshold: 0.85,
|
||||
agent_context_limit: 8192,
|
||||
agent_concurrency: 2,
|
||||
lastfm_api_key: String::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -313,6 +318,7 @@ impl_env_overrides!(
|
||||
agent_confidence_threshold,
|
||||
agent_context_limit,
|
||||
agent_concurrency,
|
||||
lastfm_api_key,
|
||||
);
|
||||
|
||||
impl AppConfig {
|
||||
@@ -396,6 +402,7 @@ impl AppConfig {
|
||||
apply_db_field!(agent_confidence_threshold);
|
||||
apply_db_field!(agent_context_limit);
|
||||
apply_db_field!(agent_concurrency);
|
||||
apply_db_field!(lastfm_api_key);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+16
-2
@@ -321,6 +321,11 @@ translations! {
|
||||
player_audio: "Audio" , "Аудио";
|
||||
player_size: "Size" , "Размер";
|
||||
player_uploader: "Uploader" , "Загрузил";
|
||||
player_lastfm_rating: "Last.fm popularity" , "Популярность Last.fm";
|
||||
player_lastfm_listeners: "Last.fm listeners" , "Слушатели Last.fm";
|
||||
player_lastfm_playcount: "Last.fm plays" , "Прослушивания Last.fm";
|
||||
player_lastfm_updated: "Last.fm updated" , "Last.fm обновлён";
|
||||
player_lastfm_not_loaded: "not loaded yet" , "ещё не загружено";
|
||||
player_play: "Play" , "Играть";
|
||||
player_like: "Like" , "Лайк";
|
||||
player_add_to_queue: "Add to queue" , "Добавить в очередь";
|
||||
@@ -357,21 +362,27 @@ translations! {
|
||||
player_saved_torrents: "Saved torrents" , "Сохранённые торренты";
|
||||
player_refresh: "Refresh" , "Обновить";
|
||||
player_no_saved_torrents: "No saved torrents" , "Сохранённых торрентов нет";
|
||||
player_upload: "Upload" , "Загрузить";
|
||||
player_choose_saved_or_add_torrent: "Choose a saved item or upload new files." , "Выберите сохранённый элемент или загрузите новые файлы.";
|
||||
player_local_files: "Local audio files" , "Локальные аудиофайлы";
|
||||
player_torrent_file: "Torrent file" , "Torrent-файл";
|
||||
player_magnet_link: "Magnet link" , "Magnet-ссылка";
|
||||
player_preview_content: "Preview content" , "Предпросмотр";
|
||||
player_upload_content: "Upload" , "Загрузить";
|
||||
player_download_selected: "Download selected" , "Скачать выбранное";
|
||||
player_pause_download: "Pause download" , "Поставить на паузу";
|
||||
player_expand_all: "Expand all" , "Развернуть всё";
|
||||
player_collapse: "Collapse" , "Свернуть";
|
||||
player_selected: "selected" , "выбрано";
|
||||
player_preview: "Preview" , "Предпросмотр";
|
||||
player_resolving: "Resolving metadata" , "Получаю метаданные";
|
||||
player_downloading: "Downloading" , "Скачивается";
|
||||
player_moving: "Moving" , "Перемещается";
|
||||
player_completed: "Completed" , "Готово";
|
||||
player_failed: "Failed" , "Ошибка";
|
||||
player_paused: "Paused" , "Пауза";
|
||||
player_no_torrent_selected: "No torrent selected" , "Торрент не выбран";
|
||||
player_downloaded: "Downloaded" , "Загружено";
|
||||
player_speed: "Speed" , "Скорость";
|
||||
player_down: "down" , "вниз";
|
||||
player_up: "up" , "вверх";
|
||||
player_peers: "peers" , "пиры";
|
||||
@@ -385,7 +396,10 @@ translations! {
|
||||
player_no_plays_yet: "No plays yet" , "Прослушиваний пока нет";
|
||||
player_page: "Page" , "Страница";
|
||||
player_of: "of" , "из";
|
||||
player_choose_torrent: "Choose a .torrent file or paste a magnet link." , "Выберите .torrent файл или вставьте magnet-ссылку.";
|
||||
player_choose_torrent: "Choose local files, paste a magnet link, or choose a .torrent file." , "Выберите локальные файлы, вставьте magnet-ссылку или выберите .torrent файл.";
|
||||
player_uploading_files: "Uploading files..." , "Загружаю файлы...";
|
||||
player_upload_complete: "Upload complete. Files are queued for processing." , "Загрузка завершена. Файлы поставлены в обработку.";
|
||||
player_upload_failed: "Upload failed" , "Загрузка не удалась";
|
||||
player_reading_torrent: "Reading torrent file..." , "Читаю torrent-файл...";
|
||||
player_resolving_magnet: "Resolving magnet metadata. This can take a while..." , "Получаю метаданные magnet-ссылки. Это может занять время...";
|
||||
player_preview_failed: "Preview failed" , "Предпросмотр не удался";
|
||||
|
||||
@@ -0,0 +1,249 @@
|
||||
use serde::Deserialize;
|
||||
|
||||
use crate::scheduler::{Job, JobContext, JobLog};
|
||||
|
||||
pub struct LastfmPopularityJob;
|
||||
|
||||
const LASTFM_REQUEST_DELAY: std::time::Duration = std::time::Duration::from_millis(1200);
|
||||
|
||||
#[derive(Debug, sqlx::FromRow)]
|
||||
struct TrackLookupRow {
|
||||
id: i64,
|
||||
title: String,
|
||||
artist_name: Option<String>,
|
||||
lastfm_updated_at: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
struct LastfmTrackInfoResponse {
|
||||
track: Option<LastfmTrack>,
|
||||
error: Option<i32>,
|
||||
message: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
struct LastfmTrack {
|
||||
listeners: Option<String>,
|
||||
playcount: Option<String>,
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl Job for LastfmPopularityJob {
|
||||
fn name(&self) -> &'static str {
|
||||
"lastfm_popularity"
|
||||
}
|
||||
|
||||
fn description(&self) -> &'static str {
|
||||
"Update Last.fm playcount/listener popularity for library tracks"
|
||||
}
|
||||
|
||||
fn default_cron(&self) -> &'static str {
|
||||
// Sundays at 04:15
|
||||
"0 15 4 * * Sun"
|
||||
}
|
||||
|
||||
async fn run(&self, ctx: &JobContext, log: &mut JobLog) -> anyhow::Result<()> {
|
||||
let api_key = ctx.config.lastfm_api_key.trim();
|
||||
if api_key.is_empty() {
|
||||
log.warn("lastfm_api_key is not configured, skipping Last.fm popularity update");
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let tracks = sqlx::query_as::<_, TrackLookupRow>(
|
||||
r#"SELECT t.id,
|
||||
t.title::text AS title,
|
||||
t.lastfm_updated_at::text AS lastfm_updated_at,
|
||||
(
|
||||
SELECT a.name::text
|
||||
FROM furumusic__track_artist ta
|
||||
JOIN furumusic__artist a ON a.id = ta.artist_id
|
||||
WHERE ta.track_id = t.id AND ta.role <> 'featuring'
|
||||
ORDER BY ta.position
|
||||
LIMIT 1
|
||||
) AS artist_name
|
||||
FROM furumusic__track t
|
||||
WHERE t.is_hidden = false
|
||||
ORDER BY t.lastfm_updated_at IS NOT NULL, t.lastfm_updated_at ASC, t.id ASC"#,
|
||||
)
|
||||
.fetch_all(&ctx.pool)
|
||||
.await?;
|
||||
|
||||
if tracks.is_empty() {
|
||||
log.info("No visible tracks found for Last.fm popularity update");
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
log.info(&format!(
|
||||
"Starting Last.fm popularity update for {} visible tracks; oldest or missing ratings are processed first; request delay is {} ms; rating formula is ln(playcount + 1) * ln(listeners + 1)",
|
||||
tracks.len(),
|
||||
LASTFM_REQUEST_DELAY.as_millis()
|
||||
));
|
||||
|
||||
let client = reqwest::Client::builder()
|
||||
.user_agent("furumusic-lastfm-popularity/0.1")
|
||||
.timeout(std::time::Duration::from_secs(15))
|
||||
.build()?;
|
||||
let mut updated = 0u64;
|
||||
let mut skipped = 0u64;
|
||||
let mut failed = 0u64;
|
||||
|
||||
for (index, track) in tracks.iter().enumerate() {
|
||||
let Some(artist) = track
|
||||
.artist_name
|
||||
.as_deref()
|
||||
.map(str::trim)
|
||||
.filter(|v| !v.is_empty())
|
||||
else {
|
||||
skipped += 1;
|
||||
log.warn(&format!(
|
||||
"Skipping track {} \"{}\": no primary artist",
|
||||
track.id, track.title
|
||||
));
|
||||
continue;
|
||||
};
|
||||
|
||||
log.info(&format!(
|
||||
"Last.fm lookup {}/{}: track {} \"{}\" by \"{}\" (previous update: {})",
|
||||
index + 1,
|
||||
tracks.len(),
|
||||
track.id,
|
||||
track.title,
|
||||
artist,
|
||||
track.lastfm_updated_at.as_deref().unwrap_or("never")
|
||||
));
|
||||
let result = fetch_track_info(&client, api_key, artist, &track.title).await;
|
||||
match result {
|
||||
Ok(Some((listeners, playcount))) => {
|
||||
let rating = popularity_rating(listeners, playcount);
|
||||
let fetched_at = chrono::Utc::now().format("%Y-%m-%dT%H:%M:%SZ").to_string();
|
||||
sqlx::query(
|
||||
r#"UPDATE furumusic__track
|
||||
SET lastfm_listeners = $2,
|
||||
lastfm_playcount = $3,
|
||||
lastfm_rating = $4,
|
||||
lastfm_updated_at = $5
|
||||
WHERE id = $1"#,
|
||||
)
|
||||
.bind(track.id)
|
||||
.bind(listeners)
|
||||
.bind(playcount)
|
||||
.bind(rating)
|
||||
.bind(&fetched_at)
|
||||
.execute(&ctx.pool)
|
||||
.await?;
|
||||
sqlx::query(
|
||||
r#"INSERT INTO furumusic__track_popularity_history
|
||||
(track_id, source, listeners, playcount, rating, fetched_at)
|
||||
VALUES ($1, 'lastfm', $2, $3, $4, $5)"#,
|
||||
)
|
||||
.bind(track.id)
|
||||
.bind(listeners)
|
||||
.bind(playcount)
|
||||
.bind(rating)
|
||||
.bind(&fetched_at)
|
||||
.execute(&ctx.pool)
|
||||
.await?;
|
||||
updated += 1;
|
||||
log.info(&format!(
|
||||
"Updated track {} \"{}\" by \"{}\": listeners={listeners}, playcount={playcount}, rating={rating:.4}",
|
||||
track.id, track.title, artist
|
||||
));
|
||||
}
|
||||
Ok(None) => {
|
||||
skipped += 1;
|
||||
log.warn(&format!(
|
||||
"Last.fm has no usable match for track {} \"{}\" by \"{}\"",
|
||||
track.id, track.title, artist
|
||||
));
|
||||
}
|
||||
Err(err) if err.to_string().contains("Last.fm rate limit exceeded") => {
|
||||
failed += 1;
|
||||
log.error("Last.fm rate limit exceeded; stopping this run early");
|
||||
break;
|
||||
}
|
||||
Err(err) => {
|
||||
failed += 1;
|
||||
log.warn(&format!(
|
||||
"Last.fm lookup failed for track {} \"{}\" / \"{}\": {err}",
|
||||
track.id, artist, track.title
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
if (index + 1) % 50 == 0 {
|
||||
log.info(&format!(
|
||||
"Last.fm progress: {}/{} tracks, {updated} updated, {skipped} skipped, {failed} failed",
|
||||
index + 1,
|
||||
tracks.len()
|
||||
));
|
||||
}
|
||||
tokio::time::sleep(LASTFM_REQUEST_DELAY).await;
|
||||
}
|
||||
|
||||
log.info(&format!(
|
||||
"Last.fm popularity update finished: {updated} updated, {skipped} skipped, {failed} failed, {} considered",
|
||||
tracks.len()
|
||||
));
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
async fn fetch_track_info(
|
||||
client: &reqwest::Client,
|
||||
api_key: &str,
|
||||
artist: &str,
|
||||
track: &str,
|
||||
) -> anyhow::Result<Option<(i64, i64)>> {
|
||||
let response = client
|
||||
.get("https://ws.audioscrobbler.com/2.0/")
|
||||
.query(&[
|
||||
("method", "track.getInfo"),
|
||||
("api_key", api_key),
|
||||
("artist", artist),
|
||||
("track", track),
|
||||
("autocorrect", "1"),
|
||||
("format", "json"),
|
||||
])
|
||||
.send()
|
||||
.await?;
|
||||
|
||||
if response.status() == reqwest::StatusCode::NOT_FOUND {
|
||||
return Ok(None);
|
||||
}
|
||||
let response = response.error_for_status()?;
|
||||
let body: LastfmTrackInfoResponse = response.json().await?;
|
||||
if let Some(code) = body.error {
|
||||
if code == 29 {
|
||||
anyhow::bail!("Last.fm rate limit exceeded");
|
||||
}
|
||||
if code == 6 || code == 7 {
|
||||
return Ok(None);
|
||||
}
|
||||
anyhow::bail!(
|
||||
"Last.fm API error {code}: {}",
|
||||
body.message.unwrap_or_else(|| "unknown error".to_string())
|
||||
);
|
||||
}
|
||||
let Some(info) = body.track else {
|
||||
return Ok(None);
|
||||
};
|
||||
let listeners = info
|
||||
.listeners
|
||||
.as_deref()
|
||||
.unwrap_or("0")
|
||||
.parse::<i64>()
|
||||
.unwrap_or(0);
|
||||
let playcount = info
|
||||
.playcount
|
||||
.as_deref()
|
||||
.unwrap_or("0")
|
||||
.parse::<i64>()
|
||||
.unwrap_or(0);
|
||||
Ok(Some((listeners.max(0), playcount.max(0))))
|
||||
}
|
||||
|
||||
fn popularity_rating(listeners: i64, playcount: i64) -> f64 {
|
||||
let listeners = listeners.max(0) as f64;
|
||||
let playcount = playcount.max(0) as f64;
|
||||
playcount.ln_1p() * listeners.ln_1p()
|
||||
}
|
||||
@@ -3,6 +3,7 @@ pub mod artist_track_image_backfill;
|
||||
pub mod cover_backfill;
|
||||
pub mod inbox_discover;
|
||||
pub mod inbox_process;
|
||||
pub mod lastfm_popularity;
|
||||
pub mod metadata_backfill;
|
||||
|
||||
use std::path::{Component, Path, PathBuf};
|
||||
|
||||
@@ -53,6 +53,7 @@ fn build_registry() -> Arc<JobRegistry> {
|
||||
registry.register(jobs::artist_image_backfill::ArtistImageBackfillJob);
|
||||
registry.register(jobs::artist_track_image_backfill::ArtistTrackImageBackfillJob);
|
||||
registry.register(jobs::metadata_backfill::MetadataBackfillJob);
|
||||
registry.register(jobs::lastfm_popularity::LastfmPopularityJob);
|
||||
Arc::new(registry)
|
||||
}
|
||||
|
||||
|
||||
@@ -1637,6 +1637,61 @@ pub mod db_migrations {
|
||||
&[Operation::custom(create_torrent_session).build()];
|
||||
}
|
||||
|
||||
// -- M0032: Last.fm track popularity ------------------------------------
|
||||
|
||||
#[cot::db::migrations::migration_op]
|
||||
async fn create_lastfm_track_popularity(
|
||||
ctx: migrations::MigrationContext<'_>,
|
||||
) -> cot::db::Result<()> {
|
||||
ctx.db
|
||||
.raw("ALTER TABLE furumusic__track ADD COLUMN lastfm_listeners BIGINT")
|
||||
.await?;
|
||||
ctx.db
|
||||
.raw("ALTER TABLE furumusic__track ADD COLUMN lastfm_playcount BIGINT")
|
||||
.await?;
|
||||
ctx.db
|
||||
.raw("ALTER TABLE furumusic__track ADD COLUMN lastfm_rating DOUBLE PRECISION")
|
||||
.await?;
|
||||
ctx.db
|
||||
.raw("ALTER TABLE furumusic__track ADD COLUMN lastfm_updated_at VARCHAR(32)")
|
||||
.await?;
|
||||
ctx.db
|
||||
.raw(
|
||||
"CREATE TABLE IF NOT EXISTS furumusic__track_popularity_history (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
track_id BIGINT NOT NULL,
|
||||
source VARCHAR(32) NOT NULL,
|
||||
listeners BIGINT NOT NULL,
|
||||
playcount BIGINT NOT NULL,
|
||||
rating DOUBLE PRECISION NOT NULL,
|
||||
fetched_at VARCHAR(32) NOT NULL
|
||||
)",
|
||||
)
|
||||
.await?;
|
||||
ctx.db
|
||||
.raw(
|
||||
"CREATE INDEX IF NOT EXISTS idx_track_popularity_history_track
|
||||
ON furumusic__track_popularity_history (track_id, fetched_at DESC)",
|
||||
)
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[derive(Debug, Copy, Clone)]
|
||||
pub struct M0032CreateLastfmTrackPopularity;
|
||||
|
||||
impl migrations::Migration for M0032CreateLastfmTrackPopularity {
|
||||
const APP_NAME: &'static str = "furumusic";
|
||||
const MIGRATION_NAME: &'static str = "m_0032_create_lastfm_track_popularity";
|
||||
const DEPENDENCIES: &'static [migrations::MigrationDependency] =
|
||||
&[migrations::MigrationDependency::migration(
|
||||
"furumusic",
|
||||
"m_0031_create_torrent_session",
|
||||
)];
|
||||
const OPERATIONS: &'static [Operation] =
|
||||
&[Operation::custom(create_lastfm_track_popularity).build()];
|
||||
}
|
||||
|
||||
pub const MIGRATIONS: &[&SyncDynMigration] = &[
|
||||
&M0006CreateMediaFile,
|
||||
&M0007CreateArtist,
|
||||
@@ -1659,5 +1714,6 @@ pub mod db_migrations {
|
||||
&M0029AddPlaybackVolume,
|
||||
&M0030AddMediaFileUploader,
|
||||
&M0031CreateTorrentSession,
|
||||
&M0032CreateLastfmTrackPopularity,
|
||||
];
|
||||
}
|
||||
|
||||
+1
-5
@@ -389,11 +389,7 @@ pub async fn oidc_callback_handler(
|
||||
config.oidc_user_groups,
|
||||
);
|
||||
|
||||
if !is_allowed_by_groups(
|
||||
&groups,
|
||||
&config.oidc_user_groups,
|
||||
&config.oidc_admin_groups,
|
||||
) {
|
||||
if !is_allowed_by_groups(&groups, &config.oidc_user_groups, &config.oidc_admin_groups) {
|
||||
tracing::warn!(
|
||||
"OIDC login denied by group allowlist: sub={sub}, groups={groups:?}, user_groups={:?}, admin_groups={:?}",
|
||||
config.oidc_user_groups,
|
||||
|
||||
@@ -64,6 +64,10 @@ pub(super) struct TrackItem {
|
||||
pub(super) audio_sample_rate: Option<i32>,
|
||||
pub(super) audio_bit_depth: Option<i32>,
|
||||
pub(super) file_size_bytes: Option<i64>,
|
||||
pub(super) lastfm_listeners: Option<i64>,
|
||||
pub(super) lastfm_playcount: Option<i64>,
|
||||
pub(super) lastfm_rating: Option<f64>,
|
||||
pub(super) lastfm_updated_at: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, JsonSchema)]
|
||||
@@ -84,6 +88,10 @@ pub(super) struct ArtistAppearanceTrack {
|
||||
pub(super) audio_sample_rate: Option<i32>,
|
||||
pub(super) audio_bit_depth: Option<i32>,
|
||||
pub(super) file_size_bytes: Option<i64>,
|
||||
pub(super) lastfm_listeners: Option<i64>,
|
||||
pub(super) lastfm_playcount: Option<i64>,
|
||||
pub(super) lastfm_rating: Option<f64>,
|
||||
pub(super) lastfm_updated_at: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, JsonSchema)]
|
||||
|
||||
+233
-16
@@ -2,7 +2,9 @@ use std::sync::Arc;
|
||||
|
||||
use cot::db::Database;
|
||||
use cot::http::StatusCode;
|
||||
use cot::http::header::{ACCEPT_RANGES, CONTENT_LENGTH, CONTENT_RANGE, CONTENT_TYPE, RANGE};
|
||||
use cot::http::header::{
|
||||
ACCEPT_RANGES, CONTENT_LENGTH, CONTENT_RANGE, CONTENT_TYPE, HeaderName, RANGE,
|
||||
};
|
||||
use cot::json::Json;
|
||||
use cot::request::extractors::Path;
|
||||
use cot::response::IntoResponse;
|
||||
@@ -40,6 +42,13 @@ fn json_error(status: StatusCode, message: &str) -> cot::response::Response {
|
||||
.expect("valid response")
|
||||
}
|
||||
|
||||
#[derive(serde::Serialize)]
|
||||
struct LocalUploadResponse {
|
||||
ok: bool,
|
||||
filename: String,
|
||||
size: u64,
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// SPA shell
|
||||
// ---------------------------------------------------------------------------
|
||||
@@ -304,7 +313,11 @@ async fn artist_detail_handler(
|
||||
mf.audio_bitrate,
|
||||
mf.audio_sample_rate,
|
||||
mf.audio_bit_depth,
|
||||
mf.file_size_bytes
|
||||
mf.file_size_bytes,
|
||||
t.lastfm_listeners,
|
||||
t.lastfm_playcount,
|
||||
t.lastfm_rating,
|
||||
t.lastfm_updated_at
|
||||
FROM furumusic__track_artist ta
|
||||
JOIN furumusic__track t ON t.id = ta.track_id
|
||||
JOIN furumusic__release r ON r.id = t.release_id
|
||||
@@ -381,6 +394,10 @@ async fn artist_detail_handler(
|
||||
audio_sample_rate: t.audio_sample_rate,
|
||||
audio_bit_depth: t.audio_bit_depth,
|
||||
file_size_bytes: t.file_size_bytes,
|
||||
lastfm_listeners: t.lastfm_listeners,
|
||||
lastfm_playcount: t.lastfm_playcount,
|
||||
lastfm_rating: t.lastfm_rating,
|
||||
lastfm_updated_at: t.lastfm_updated_at,
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
@@ -450,7 +467,11 @@ async fn release_detail_handler(
|
||||
mf.audio_bitrate,
|
||||
mf.audio_sample_rate,
|
||||
mf.audio_bit_depth,
|
||||
mf.file_size_bytes
|
||||
mf.file_size_bytes,
|
||||
t.lastfm_listeners,
|
||||
t.lastfm_playcount,
|
||||
t.lastfm_rating,
|
||||
t.lastfm_updated_at
|
||||
FROM furumusic__track t
|
||||
JOIN furumusic__release r ON r.id = t.release_id
|
||||
LEFT JOIN furumusic__media_file mf ON mf.id = t.audio_file_id
|
||||
@@ -526,6 +547,10 @@ async fn release_detail_handler(
|
||||
audio_sample_rate: t.audio_sample_rate,
|
||||
audio_bit_depth: t.audio_bit_depth,
|
||||
file_size_bytes: t.file_size_bytes,
|
||||
lastfm_listeners: t.lastfm_listeners,
|
||||
lastfm_playcount: t.lastfm_playcount,
|
||||
lastfm_rating: t.lastfm_rating,
|
||||
lastfm_updated_at: t.lastfm_updated_at,
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
@@ -680,7 +705,11 @@ async fn playlist_detail_handler(
|
||||
mf.audio_bitrate,
|
||||
mf.audio_sample_rate,
|
||||
mf.audio_bit_depth,
|
||||
mf.file_size_bytes
|
||||
mf.file_size_bytes,
|
||||
t.lastfm_listeners,
|
||||
t.lastfm_playcount,
|
||||
t.lastfm_rating,
|
||||
t.lastfm_updated_at
|
||||
FROM furumusic__playlist_track pt
|
||||
JOIN furumusic__track t ON t.id = pt.track_id
|
||||
JOIN furumusic__release r ON r.id = t.release_id
|
||||
@@ -776,6 +805,10 @@ async fn build_track_items(
|
||||
audio_sample_rate: t.audio_sample_rate,
|
||||
audio_bit_depth: t.audio_bit_depth,
|
||||
file_size_bytes: t.file_size_bytes,
|
||||
lastfm_listeners: t.lastfm_listeners,
|
||||
lastfm_playcount: t.lastfm_playcount,
|
||||
lastfm_rating: t.lastfm_rating,
|
||||
lastfm_updated_at: t.lastfm_updated_at,
|
||||
}
|
||||
})
|
||||
.collect())
|
||||
@@ -910,6 +943,140 @@ async fn stream_handler(
|
||||
Ok(response)
|
||||
}
|
||||
|
||||
async fn local_upload_handler(
|
||||
session: Session,
|
||||
db: Database,
|
||||
config: AppConfig,
|
||||
scheduler_handle: Arc<tokio::sync::OnceCell<Arc<SchedulerHandle>>>,
|
||||
request: cot::request::Request,
|
||||
) -> cot::Result<cot::http::Response<Body>> {
|
||||
let Some(user) = auth::get_session_user(&session, &db).await else {
|
||||
return Ok(json_error(StatusCode::UNAUTHORIZED, "not authenticated"));
|
||||
};
|
||||
|
||||
let inbox_dir = config.agent_inbox_dir.trim();
|
||||
if inbox_dir.is_empty() {
|
||||
return Ok(json_error(
|
||||
StatusCode::BAD_REQUEST,
|
||||
"agent_inbox_dir is not configured",
|
||||
));
|
||||
}
|
||||
let inbox_root = std::path::PathBuf::from(inbox_dir);
|
||||
if !inbox_root.is_absolute() {
|
||||
return Ok(json_error(
|
||||
StatusCode::BAD_REQUEST,
|
||||
"agent_inbox_dir must be an absolute path",
|
||||
));
|
||||
}
|
||||
|
||||
let filename_header = HeaderName::from_static("x-furumusic-filename");
|
||||
let original_name = request
|
||||
.headers()
|
||||
.get(filename_header)
|
||||
.and_then(|value| value.to_str().ok())
|
||||
.map(percent_decode_header)
|
||||
.map(|value| value.trim().to_string())
|
||||
.filter(|value| !value.is_empty())
|
||||
.unwrap_or_else(|| "upload.mp3".to_string());
|
||||
let filename = sanitize_upload_filename(&original_name);
|
||||
|
||||
let bytes = request
|
||||
.into_body()
|
||||
.into_bytes()
|
||||
.await
|
||||
.map_err(|err| cot::Error::internal(err.to_string()))?;
|
||||
if bytes.is_empty() {
|
||||
return Ok(json_error(
|
||||
StatusCode::BAD_REQUEST,
|
||||
"uploaded file is empty",
|
||||
));
|
||||
}
|
||||
|
||||
let upload_dir = inbox_root
|
||||
.join("user_uploads")
|
||||
.join(user.id.to_string())
|
||||
.join(format!("local-{}", uuid::Uuid::new_v4()));
|
||||
tokio::fs::create_dir_all(&upload_dir)
|
||||
.await
|
||||
.map_err(|err| cot::Error::internal(err.to_string()))?;
|
||||
let destination = upload_dir.join(&filename);
|
||||
tokio::fs::write(&destination, &bytes)
|
||||
.await
|
||||
.map_err(|err| cot::Error::internal(err.to_string()))?;
|
||||
|
||||
if let Some(handle) = scheduler_handle.get() {
|
||||
let handle = Arc::clone(handle);
|
||||
tokio::spawn(async move {
|
||||
if let Err(err) = handle.trigger_job_now("inbox_discover").await {
|
||||
tracing::warn!("failed to trigger inbox_discover after local upload: {err}");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Json(LocalUploadResponse {
|
||||
ok: true,
|
||||
filename,
|
||||
size: bytes.len() as u64,
|
||||
})
|
||||
.into_response()
|
||||
}
|
||||
|
||||
fn sanitize_upload_filename(value: &str) -> String {
|
||||
let name = std::path::Path::new(value)
|
||||
.file_name()
|
||||
.and_then(|name| name.to_str())
|
||||
.unwrap_or("upload.mp3");
|
||||
let sanitized: String = name
|
||||
.chars()
|
||||
.map(|c| match c {
|
||||
'/' | '\\' | ':' | '*' | '?' | '"' | '<' | '>' | '|' => '_',
|
||||
c if c.is_control() => '_',
|
||||
c => c,
|
||||
})
|
||||
.collect();
|
||||
let trimmed = sanitized.trim().trim_matches('.').trim();
|
||||
if trimmed.is_empty() {
|
||||
"upload.mp3".to_string()
|
||||
} else {
|
||||
trimmed.to_string()
|
||||
}
|
||||
}
|
||||
|
||||
fn percent_decode_header(value: &str) -> String {
|
||||
let bytes = value.as_bytes();
|
||||
let mut out = Vec::with_capacity(bytes.len());
|
||||
let mut index = 0;
|
||||
while index < bytes.len() {
|
||||
match bytes[index] {
|
||||
b'%' if index + 2 < bytes.len() => {
|
||||
let hi = hex_value(bytes[index + 1]);
|
||||
let lo = hex_value(bytes[index + 2]);
|
||||
if let (Some(hi), Some(lo)) = (hi, lo) {
|
||||
out.push((hi << 4) | lo);
|
||||
index += 3;
|
||||
} else {
|
||||
out.push(bytes[index]);
|
||||
index += 1;
|
||||
}
|
||||
}
|
||||
byte => {
|
||||
out.push(byte);
|
||||
index += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
String::from_utf8_lossy(&out).to_string()
|
||||
}
|
||||
|
||||
fn hex_value(byte: u8) -> Option<u8> {
|
||||
match byte {
|
||||
b'0'..=b'9' => Some(byte - b'0'),
|
||||
b'a'..=b'f' => Some(byte - b'a' + 10),
|
||||
b'A'..=b'F' => Some(byte - b'A' + 10),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_range(header: &str, file_size: u64) -> Option<(u64, u64)> {
|
||||
let bytes_prefix = "bytes=";
|
||||
if !header.starts_with(bytes_prefix) {
|
||||
@@ -1264,7 +1431,11 @@ async fn search_handler(
|
||||
mf.audio_bitrate,
|
||||
mf.audio_sample_rate,
|
||||
mf.audio_bit_depth,
|
||||
mf.file_size_bytes
|
||||
mf.file_size_bytes,
|
||||
t.lastfm_listeners,
|
||||
t.lastfm_playcount,
|
||||
t.lastfm_rating,
|
||||
t.lastfm_updated_at
|
||||
FROM furumusic__track t
|
||||
JOIN furumusic__release rel ON rel.id = t.release_id
|
||||
LEFT JOIN furumusic__media_file mf ON mf.id = t.audio_file_id
|
||||
@@ -1329,7 +1500,7 @@ async fn search_handler(
|
||||
let t = sqlx::query_as::<_, SearchTrackRow>(
|
||||
r#"SELECT id, title, track_number, disc_number, duration_seconds, cover_file_id,
|
||||
release_cover_file_id, release_year, uploader_name, audio_format, audio_bitrate,
|
||||
audio_sample_rate, audio_bit_depth, file_size_bytes FROM (
|
||||
audio_sample_rate, audio_bit_depth, file_size_bytes, lastfm_listeners, lastfm_playcount, lastfm_rating, lastfm_updated_at FROM (
|
||||
SELECT t.id, t.title::text AS title, t.track_number, t.disc_number,
|
||||
t.duration_seconds, t.cover_file_id,
|
||||
rel.cover_file_id AS release_cover_file_id,
|
||||
@@ -1340,20 +1511,27 @@ async fn search_handler(
|
||||
mf.audio_sample_rate,
|
||||
mf.audio_bit_depth,
|
||||
mf.file_size_bytes,
|
||||
t.lastfm_listeners,
|
||||
t.lastfm_playcount,
|
||||
t.lastfm_rating,
|
||||
t.lastfm_updated_at,
|
||||
MAX(sim) AS similarity
|
||||
FROM (
|
||||
SELECT id, title, title_sort, track_number, disc_number, duration_seconds, cover_file_id, release_id, audio_file_id,
|
||||
lastfm_listeners, lastfm_playcount, lastfm_rating, lastfm_updated_at,
|
||||
similarity(title_sort, $1) AS sim
|
||||
FROM furumusic__track WHERE is_hidden = false AND title_sort % $1
|
||||
UNION ALL
|
||||
SELECT id, title, title_sort, track_number, disc_number, duration_seconds, cover_file_id, release_id, audio_file_id,
|
||||
lastfm_listeners, lastfm_playcount, lastfm_rating, lastfm_updated_at,
|
||||
0.01::real AS sim
|
||||
FROM furumusic__track WHERE is_hidden = false AND title_sort ILIKE '%' || $1 || '%'
|
||||
) t
|
||||
JOIN furumusic__release rel ON rel.id = t.release_id
|
||||
LEFT JOIN furumusic__media_file mf ON mf.id = t.audio_file_id
|
||||
GROUP BY t.id, t.title, t.track_number, t.disc_number, t.duration_seconds, t.cover_file_id, rel.cover_file_id, rel.year,
|
||||
mf.uploader_name, mf.audio_format, mf.audio_bitrate, mf.audio_sample_rate, mf.audio_bit_depth, mf.file_size_bytes
|
||||
mf.uploader_name, mf.audio_format, mf.audio_bitrate, mf.audio_sample_rate, mf.audio_bit_depth, mf.file_size_bytes,
|
||||
t.lastfm_listeners, t.lastfm_playcount, t.lastfm_rating, t.lastfm_updated_at
|
||||
ORDER BY similarity DESC
|
||||
LIMIT $2
|
||||
) sub"#,
|
||||
@@ -1457,6 +1635,10 @@ async fn search_handler(
|
||||
audio_sample_rate: t.audio_sample_rate,
|
||||
audio_bit_depth: t.audio_bit_depth,
|
||||
file_size_bytes: t.file_size_bytes,
|
||||
lastfm_listeners: t.lastfm_listeners,
|
||||
lastfm_playcount: t.lastfm_playcount,
|
||||
lastfm_rating: t.lastfm_rating,
|
||||
lastfm_updated_at: t.lastfm_updated_at,
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
@@ -2021,7 +2203,11 @@ async fn tracks_by_ids_handler(
|
||||
mf.audio_bitrate,
|
||||
mf.audio_sample_rate,
|
||||
mf.audio_bit_depth,
|
||||
mf.file_size_bytes
|
||||
mf.file_size_bytes,
|
||||
t.lastfm_listeners,
|
||||
t.lastfm_playcount,
|
||||
t.lastfm_rating,
|
||||
t.lastfm_updated_at
|
||||
FROM furumusic__track t
|
||||
JOIN furumusic__release r ON r.id = t.release_id
|
||||
LEFT JOIN furumusic__media_file mf ON mf.id = t.audio_file_id
|
||||
@@ -2096,6 +2282,10 @@ async fn tracks_by_ids_handler(
|
||||
audio_sample_rate: t.audio_sample_rate,
|
||||
audio_bit_depth: t.audio_bit_depth,
|
||||
file_size_bytes: t.file_size_bytes,
|
||||
lastfm_listeners: t.lastfm_listeners,
|
||||
lastfm_playcount: t.lastfm_playcount,
|
||||
lastfm_rating: t.lastfm_rating,
|
||||
lastfm_updated_at: t.lastfm_updated_at,
|
||||
},
|
||||
);
|
||||
}
|
||||
@@ -2268,9 +2458,7 @@ impl App for PlayerApp {
|
||||
.await;
|
||||
let service = torrent_service
|
||||
.get_or_init(|| async {
|
||||
Arc::new(TorrentService::new(Arc::clone(
|
||||
&scheduler_handle,
|
||||
)))
|
||||
Arc::new(TorrentService::new(Arc::clone(&scheduler_handle)))
|
||||
})
|
||||
.await;
|
||||
match service.details(pg_pool, user.id, &path.0.id).await {
|
||||
@@ -2282,7 +2470,8 @@ impl App for PlayerApp {
|
||||
}
|
||||
}
|
||||
})
|
||||
.delete(move |session: Session, db: Database, path: Path<PathStringId>| {
|
||||
.delete(
|
||||
move |session: Session, db: Database, path: Path<PathStringId>| {
|
||||
let pool = Arc::clone(&pool);
|
||||
let pool_config = Arc::clone(&pool_config);
|
||||
let torrent_service = Arc::clone(&torrent_service);
|
||||
@@ -2309,13 +2498,16 @@ impl App for PlayerApp {
|
||||
})
|
||||
.await;
|
||||
match service.remove(pg_pool, user.id, &path.0.id).await {
|
||||
Ok(()) => Json(serde_json::json!({ "ok": true })).into_response(),
|
||||
Ok(()) => {
|
||||
Json(serde_json::json!({ "ok": true })).into_response()
|
||||
}
|
||||
Err(err) => {
|
||||
Ok(json_error(StatusCode::NOT_FOUND, &err.to_string()))
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
},
|
||||
)
|
||||
},
|
||||
"player_torrent_detail",
|
||||
),
|
||||
@@ -2365,6 +2557,29 @@ impl App for PlayerApp {
|
||||
},
|
||||
"player_torrent_preview",
|
||||
),
|
||||
Route::with_handler_and_name(
|
||||
"/uploads/local",
|
||||
{
|
||||
let scheduler_handle = Arc::clone(&self.scheduler_handle);
|
||||
post(
|
||||
move |session: Session, db: Database, request: cot::request::Request| {
|
||||
let scheduler_handle = Arc::clone(&scheduler_handle);
|
||||
async move {
|
||||
let (live_config, _) = AppConfig::load_with_db(&db).await;
|
||||
local_upload_handler(
|
||||
session,
|
||||
db,
|
||||
live_config,
|
||||
scheduler_handle,
|
||||
request,
|
||||
)
|
||||
.await
|
||||
}
|
||||
},
|
||||
)
|
||||
},
|
||||
"player_local_upload",
|
||||
),
|
||||
Route::with_handler_and_name(
|
||||
"/torrents/{id}/start",
|
||||
{
|
||||
@@ -2431,7 +2646,8 @@ impl App for PlayerApp {
|
||||
let pool_config = Arc::clone(&pool_config);
|
||||
let torrent_service = Arc::clone(&torrent_service);
|
||||
let scheduler_handle = Arc::clone(&self.scheduler_handle);
|
||||
post(move |session: Session, db: Database, path: Path<PathStringId>| {
|
||||
post(
|
||||
move |session: Session, db: Database, path: Path<PathStringId>| {
|
||||
let pool = Arc::clone(&pool);
|
||||
let pool_config = Arc::clone(&pool_config);
|
||||
let torrent_service = Arc::clone(&torrent_service);
|
||||
@@ -2464,7 +2680,8 @@ impl App for PlayerApp {
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
},
|
||||
)
|
||||
},
|
||||
"player_torrent_pause",
|
||||
),
|
||||
|
||||
@@ -44,6 +44,10 @@ pub(super) struct TrackRow {
|
||||
pub(super) audio_sample_rate: Option<i32>,
|
||||
pub(super) audio_bit_depth: Option<i32>,
|
||||
pub(super) file_size_bytes: Option<i64>,
|
||||
pub(super) lastfm_listeners: Option<i64>,
|
||||
pub(super) lastfm_playcount: Option<i64>,
|
||||
pub(super) lastfm_rating: Option<f64>,
|
||||
pub(super) lastfm_updated_at: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(sqlx::FromRow)]
|
||||
@@ -110,6 +114,10 @@ pub(super) struct PlaylistTrackRow {
|
||||
pub(super) audio_sample_rate: Option<i32>,
|
||||
pub(super) audio_bit_depth: Option<i32>,
|
||||
pub(super) file_size_bytes: Option<i64>,
|
||||
pub(super) lastfm_listeners: Option<i64>,
|
||||
pub(super) lastfm_playcount: Option<i64>,
|
||||
pub(super) lastfm_rating: Option<f64>,
|
||||
pub(super) lastfm_updated_at: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(sqlx::FromRow)]
|
||||
@@ -128,6 +136,10 @@ pub(super) struct AppearanceTrackRow {
|
||||
pub(super) audio_sample_rate: Option<i32>,
|
||||
pub(super) audio_bit_depth: Option<i32>,
|
||||
pub(super) file_size_bytes: Option<i64>,
|
||||
pub(super) lastfm_listeners: Option<i64>,
|
||||
pub(super) lastfm_playcount: Option<i64>,
|
||||
pub(super) lastfm_rating: Option<f64>,
|
||||
pub(super) lastfm_updated_at: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(sqlx::FromRow)]
|
||||
@@ -165,6 +177,10 @@ pub(super) struct SearchTrackRow {
|
||||
pub(super) audio_sample_rate: Option<i32>,
|
||||
pub(super) audio_bit_depth: Option<i32>,
|
||||
pub(super) file_size_bytes: Option<i64>,
|
||||
pub(super) lastfm_listeners: Option<i64>,
|
||||
pub(super) lastfm_playcount: Option<i64>,
|
||||
pub(super) lastfm_rating: Option<f64>,
|
||||
pub(super) lastfm_updated_at: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(sqlx::FromRow)]
|
||||
|
||||
@@ -1347,7 +1347,7 @@ async fn run_scheduled_job(
|
||||
|
||||
// Check agent_enabled (re-read from DB every run)
|
||||
let (live_config, _) = AppConfig::load_with_db(db).await;
|
||||
if !live_config.agent_enabled {
|
||||
if !live_config.agent_enabled && job_name != "lastfm_popularity" {
|
||||
tracing::warn!(job = job_name, "Skipping: agent_enabled=false");
|
||||
return;
|
||||
}
|
||||
|
||||
+321
-27
@@ -1,4 +1,4 @@
|
||||
use std::collections::HashMap;
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
@@ -98,6 +98,7 @@ pub struct TorrentStartRequest {
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
enum TorrentJobStatus {
|
||||
Resolving,
|
||||
Preview,
|
||||
Downloading,
|
||||
Moving,
|
||||
@@ -110,6 +111,7 @@ impl TorrentJobStatus {
|
||||
fn as_str(self) -> &'static str {
|
||||
match self {
|
||||
Self::Preview => "preview",
|
||||
Self::Resolving => "resolving",
|
||||
Self::Downloading => "downloading",
|
||||
Self::Moving => "moving",
|
||||
Self::Complete => "complete",
|
||||
@@ -121,6 +123,7 @@ impl TorrentJobStatus {
|
||||
fn from_str(value: &str) -> Self {
|
||||
match value {
|
||||
"downloading" => Self::Downloading,
|
||||
"resolving" => Self::Resolving,
|
||||
"moving" => Self::Moving,
|
||||
"complete" => Self::Complete,
|
||||
"failed" => Self::Failed,
|
||||
@@ -194,10 +197,14 @@ impl TorrentSessionRow {
|
||||
self.status.as_str()
|
||||
};
|
||||
let stats = handle.map(|h| h.stats());
|
||||
let downloaded_bytes = stats
|
||||
let mut downloaded_bytes = stats
|
||||
.as_ref()
|
||||
.map(|s| s.progress_bytes)
|
||||
.unwrap_or_else(|| i64_to_u64(self.downloaded_bytes));
|
||||
let selected_size = i64_to_u64(self.selected_size);
|
||||
if status == "complete" {
|
||||
downloaded_bytes = selected_size;
|
||||
}
|
||||
let uploaded_bytes = stats
|
||||
.as_ref()
|
||||
.map(|s| s.uploaded_bytes)
|
||||
@@ -225,7 +232,7 @@ impl TorrentSessionRow {
|
||||
status: status.to_string(),
|
||||
client_state: stats.as_ref().map(|s| s.state.to_string()),
|
||||
total_size: i64_to_u64(self.total_size),
|
||||
selected_size: i64_to_u64(self.selected_size),
|
||||
selected_size,
|
||||
downloaded_bytes,
|
||||
uploaded_bytes,
|
||||
progress_percent,
|
||||
@@ -313,10 +320,14 @@ impl TorrentJob {
|
||||
|
||||
fn dto(&self) -> TorrentJobDto {
|
||||
let stats = self.handle.as_ref().map(|h| h.stats());
|
||||
let downloaded_bytes = stats
|
||||
let mut downloaded_bytes = stats
|
||||
.as_ref()
|
||||
.map(|s| s.progress_bytes)
|
||||
.unwrap_or(self.downloaded_bytes);
|
||||
let selected_size = self.selected_size();
|
||||
if self.status == TorrentJobStatus::Complete {
|
||||
downloaded_bytes = selected_size;
|
||||
}
|
||||
let uploaded_bytes = stats
|
||||
.as_ref()
|
||||
.map(|s| s.uploaded_bytes)
|
||||
@@ -336,7 +347,7 @@ impl TorrentJob {
|
||||
status: self.status.as_str().to_string(),
|
||||
client_state: stats.as_ref().map(|s| s.state.to_string()),
|
||||
total_size: self.total_size(),
|
||||
selected_size: self.selected_size(),
|
||||
selected_size,
|
||||
downloaded_bytes,
|
||||
uploaded_bytes,
|
||||
progress_percent: if self.status == TorrentJobStatus::Complete {
|
||||
@@ -364,6 +375,7 @@ pub struct TorrentService {
|
||||
temp_root: PathBuf,
|
||||
session: OnceCell<Arc<Session>>,
|
||||
jobs: Mutex<HashMap<String, TorrentJob>>,
|
||||
resolving_jobs: Mutex<HashSet<String>>,
|
||||
scheduler_handle: Arc<OnceCell<Arc<SchedulerHandle>>>,
|
||||
}
|
||||
|
||||
@@ -373,6 +385,7 @@ impl TorrentService {
|
||||
temp_root: std::env::temp_dir().join("furumusic").join("torrents"),
|
||||
session: OnceCell::new(),
|
||||
jobs: Mutex::new(HashMap::new()),
|
||||
resolving_jobs: Mutex::new(HashSet::new()),
|
||||
scheduler_handle,
|
||||
}
|
||||
}
|
||||
@@ -396,7 +409,11 @@ impl TorrentService {
|
||||
.cloned()
|
||||
}
|
||||
|
||||
pub async fn list(&self, pool: &PgPool, user_id: i64) -> anyhow::Result<Vec<TorrentJobDto>> {
|
||||
pub async fn list(
|
||||
self: &Arc<Self>,
|
||||
pool: &PgPool,
|
||||
user_id: i64,
|
||||
) -> anyhow::Result<Vec<TorrentJobDto>> {
|
||||
let rows = sqlx::query_as::<_, TorrentSessionRow>(
|
||||
r#"SELECT id, user_id, name, info_hash, source_kind, source_label, torrent_bytes,
|
||||
files_json, selected_files_json, status, total_size, selected_size,
|
||||
@@ -404,7 +421,7 @@ impl TorrentService {
|
||||
created_at, updated_at, completed_at
|
||||
FROM furumusic__torrent_session
|
||||
WHERE user_id = $1
|
||||
ORDER BY updated_at DESC, created_at DESC
|
||||
ORDER BY created_at DESC, id DESC
|
||||
LIMIT $2"#,
|
||||
)
|
||||
.bind(user_id)
|
||||
@@ -419,6 +436,21 @@ impl TorrentService {
|
||||
.collect::<HashMap<_, _>>()
|
||||
};
|
||||
|
||||
for row in rows.iter().filter(|row| row.status == "resolving") {
|
||||
if row.source_kind == "magnet" {
|
||||
if let Some(magnet) = row.source_label.clone() {
|
||||
self.spawn_resolve_pending_magnet(
|
||||
pool.clone(),
|
||||
user_id,
|
||||
row.id.clone(),
|
||||
magnet,
|
||||
row.created_at.clone(),
|
||||
)
|
||||
.await;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(rows
|
||||
.iter()
|
||||
.map(|row| row.dto(handles.get(&row.id)))
|
||||
@@ -447,7 +479,7 @@ impl TorrentService {
|
||||
}
|
||||
|
||||
pub async fn preview(
|
||||
&self,
|
||||
self: &Arc<Self>,
|
||||
pool: &PgPool,
|
||||
user_id: i64,
|
||||
request: TorrentPreviewRequest,
|
||||
@@ -465,17 +497,31 @@ impl TorrentService {
|
||||
.filter(|s| !s.is_empty())
|
||||
.map(str::to_owned);
|
||||
|
||||
let add = match request.kind {
|
||||
TorrentPreviewKind::Magnet => {
|
||||
if matches!(request.kind, TorrentPreviewKind::Magnet) {
|
||||
let magnet = request
|
||||
.magnet
|
||||
.as_deref()
|
||||
.map(str::trim)
|
||||
.filter(|s| !s.is_empty())
|
||||
.context("magnet link is empty")?;
|
||||
AddTorrent::from_url(magnet.to_string())
|
||||
.context("magnet link is empty")?
|
||||
.to_string();
|
||||
let info_hash = extract_magnet_info_hash(&magnet).context("invalid magnet link")?;
|
||||
let name = magnet_display_name(&magnet)
|
||||
.or(source_label)
|
||||
.unwrap_or_else(|| info_hash.clone());
|
||||
let now = now_string();
|
||||
insert_pending_magnet(pool, &id, user_id, &name, &info_hash, &magnet, &now).await?;
|
||||
self.spawn_resolve_pending_magnet(pool.clone(), user_id, id.clone(), magnet, now)
|
||||
.await;
|
||||
|
||||
let row = load_row(pool, user_id, &id).await?;
|
||||
return Ok(TorrentSessionDto {
|
||||
job: row.dto(None),
|
||||
preview: row.preview()?,
|
||||
selected_files: row.selected_files(),
|
||||
});
|
||||
}
|
||||
TorrentPreviewKind::TorrentFile => {
|
||||
|
||||
let encoded = request
|
||||
.torrent_base64
|
||||
.as_deref()
|
||||
@@ -484,23 +530,17 @@ impl TorrentService {
|
||||
let bytes = base64::engine::general_purpose::STANDARD
|
||||
.decode(encoded)
|
||||
.context("invalid torrent file encoding")?;
|
||||
AddTorrent::from_bytes(bytes)
|
||||
}
|
||||
};
|
||||
|
||||
let response = tokio::time::timeout(
|
||||
METADATA_TIMEOUT,
|
||||
session.add_torrent(
|
||||
add,
|
||||
let response = session
|
||||
.add_torrent(
|
||||
AddTorrent::from_bytes(bytes),
|
||||
Some(AddTorrentOptions {
|
||||
list_only: true,
|
||||
output_folder: Some(output_dir.to_string_lossy().to_string()),
|
||||
..Default::default()
|
||||
}),
|
||||
),
|
||||
)
|
||||
.await
|
||||
.context("timed out while resolving torrent metadata")??;
|
||||
.await?;
|
||||
|
||||
let AddTorrentResponse::ListOnly(list) = response else {
|
||||
bail!("torrent was unexpectedly added instead of previewed");
|
||||
@@ -564,6 +604,114 @@ impl TorrentService {
|
||||
Ok(dto)
|
||||
}
|
||||
|
||||
async fn spawn_resolve_pending_magnet(
|
||||
self: &Arc<Self>,
|
||||
pool: PgPool,
|
||||
user_id: i64,
|
||||
id: String,
|
||||
magnet: String,
|
||||
created_at: String,
|
||||
) {
|
||||
{
|
||||
let mut resolving = self.resolving_jobs.lock().await;
|
||||
if !resolving.insert(id.clone()) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
let service = Arc::clone(self);
|
||||
tokio::spawn(async move {
|
||||
let result = service
|
||||
.resolve_pending_magnet(&pool, user_id, &id, &magnet, &created_at)
|
||||
.await;
|
||||
if let Err(err) = result {
|
||||
update_resolving_error(&pool, &id, &err.to_string()).await;
|
||||
}
|
||||
service.resolving_jobs.lock().await.remove(&id);
|
||||
});
|
||||
}
|
||||
|
||||
async fn resolve_pending_magnet(
|
||||
&self,
|
||||
pool: &PgPool,
|
||||
user_id: i64,
|
||||
id: &str,
|
||||
magnet: &str,
|
||||
created_at: &str,
|
||||
) -> anyhow::Result<()> {
|
||||
let session = self.session().await?;
|
||||
let output_dir = self.temp_root.join(id).join("download");
|
||||
tokio::fs::create_dir_all(&output_dir).await?;
|
||||
let response = tokio::time::timeout(
|
||||
METADATA_TIMEOUT,
|
||||
session.add_torrent(
|
||||
AddTorrent::from_url(magnet.to_string()),
|
||||
Some(AddTorrentOptions {
|
||||
list_only: true,
|
||||
output_folder: Some(output_dir.to_string_lossy().to_string()),
|
||||
..Default::default()
|
||||
}),
|
||||
),
|
||||
)
|
||||
.await
|
||||
.context("timed out while resolving torrent metadata")??;
|
||||
|
||||
let AddTorrentResponse::ListOnly(list) = response else {
|
||||
bail!("torrent was unexpectedly added instead of previewed");
|
||||
};
|
||||
|
||||
let name = list
|
||||
.info
|
||||
.name
|
||||
.as_ref()
|
||||
.map(|b| String::from_utf8_lossy(b.as_ref()).to_string())
|
||||
.filter(|s| !s.is_empty())
|
||||
.or_else(|| magnet_display_name(magnet))
|
||||
.unwrap_or_else(|| list.info_hash.as_string());
|
||||
|
||||
let mut files = Vec::new();
|
||||
for (index, details) in list.info.iter_file_details()?.enumerate() {
|
||||
let name = details
|
||||
.filename
|
||||
.to_string()
|
||||
.unwrap_or_else(|_| "<invalid filename>".to_string());
|
||||
files.push(TorrentFileDto {
|
||||
index,
|
||||
name,
|
||||
components: details.filename.to_vec().unwrap_or_default(),
|
||||
length: details.len,
|
||||
selected: true,
|
||||
});
|
||||
}
|
||||
|
||||
let selected_files = files.iter().map(|f| f.index).collect::<Vec<_>>();
|
||||
let job = TorrentJob {
|
||||
id: id.to_string(),
|
||||
user_id,
|
||||
name,
|
||||
info_hash: list.info_hash.as_string(),
|
||||
source_kind: "magnet".to_string(),
|
||||
source_label: Some(magnet.to_string()),
|
||||
torrent_bytes: list.torrent_bytes.to_vec(),
|
||||
files,
|
||||
status: TorrentJobStatus::Preview,
|
||||
output_dir,
|
||||
selected_files,
|
||||
handle: None,
|
||||
downloaded_bytes: 0,
|
||||
uploaded_bytes: 0,
|
||||
progress_percent: 0.0,
|
||||
error: None,
|
||||
created_at: created_at.to_string(),
|
||||
updated_at: now_string(),
|
||||
completed_at: None,
|
||||
};
|
||||
|
||||
update_resolved_job(pool, &job).await?;
|
||||
self.jobs.lock().await.insert(id.to_string(), job);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn status(
|
||||
&self,
|
||||
pool: &PgPool,
|
||||
@@ -598,9 +746,8 @@ impl TorrentService {
|
||||
self.stop_torrent(&handle).await;
|
||||
}
|
||||
|
||||
let result = sqlx::query(
|
||||
"DELETE FROM furumusic__torrent_session WHERE id = $1 AND user_id = $2",
|
||||
)
|
||||
let result =
|
||||
sqlx::query("DELETE FROM furumusic__torrent_session WHERE id = $1 AND user_id = $2")
|
||||
.bind(id)
|
||||
.bind(user_id)
|
||||
.execute(pool)
|
||||
@@ -636,7 +783,12 @@ impl TorrentService {
|
||||
if job.user_id != uploader_user_id {
|
||||
bail!("torrent job not found");
|
||||
}
|
||||
if job.handle.is_some() && matches!(job.status, TorrentJobStatus::Downloading | TorrentJobStatus::Moving) {
|
||||
if job.handle.is_some()
|
||||
&& matches!(
|
||||
job.status,
|
||||
TorrentJobStatus::Downloading | TorrentJobStatus::Moving
|
||||
)
|
||||
{
|
||||
bail!("torrent job is already running");
|
||||
}
|
||||
validate_selection(&job.files, &selected_files)?;
|
||||
@@ -941,6 +1093,89 @@ async fn insert_job(pool: &PgPool, job: &TorrentJob) -> anyhow::Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn insert_pending_magnet(
|
||||
pool: &PgPool,
|
||||
id: &str,
|
||||
user_id: i64,
|
||||
name: &str,
|
||||
info_hash: &str,
|
||||
magnet: &str,
|
||||
now: &str,
|
||||
) -> anyhow::Result<()> {
|
||||
sqlx::query(
|
||||
r#"INSERT INTO furumusic__torrent_session
|
||||
(id, user_id, name, info_hash, source_kind, source_label, torrent_bytes,
|
||||
files_json, selected_files_json, status, total_size, selected_size,
|
||||
downloaded_bytes, uploaded_bytes, progress_percent, error,
|
||||
created_at, updated_at, completed_at)
|
||||
VALUES ($1, $2, $3, $4, 'magnet', $5, $6,
|
||||
'[]', '[]', 'resolving', 0, 0,
|
||||
0, 0, 0, NULL,
|
||||
$7, $8, NULL)"#,
|
||||
)
|
||||
.bind(id)
|
||||
.bind(user_id)
|
||||
.bind(name)
|
||||
.bind(info_hash)
|
||||
.bind(magnet)
|
||||
.bind(Vec::<u8>::new())
|
||||
.bind(now)
|
||||
.bind(now)
|
||||
.execute(pool)
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn update_resolved_job(pool: &PgPool, job: &TorrentJob) -> anyhow::Result<()> {
|
||||
sqlx::query(
|
||||
r#"UPDATE furumusic__torrent_session
|
||||
SET name = $2,
|
||||
info_hash = $3,
|
||||
torrent_bytes = $4,
|
||||
files_json = $5,
|
||||
selected_files_json = $6,
|
||||
status = 'preview',
|
||||
total_size = $7,
|
||||
selected_size = $8,
|
||||
downloaded_bytes = 0,
|
||||
uploaded_bytes = 0,
|
||||
progress_percent = 0,
|
||||
error = NULL,
|
||||
updated_at = $9,
|
||||
completed_at = NULL
|
||||
WHERE id = $1"#,
|
||||
)
|
||||
.bind(&job.id)
|
||||
.bind(&job.name)
|
||||
.bind(&job.info_hash)
|
||||
.bind(&job.torrent_bytes)
|
||||
.bind(serde_json::to_string(&job.files)?)
|
||||
.bind(serde_json::to_string(&job.selected_files)?)
|
||||
.bind(u64_to_i64(job.total_size()))
|
||||
.bind(u64_to_i64(job.selected_size()))
|
||||
.bind(&job.updated_at)
|
||||
.execute(pool)
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn update_resolving_error(pool: &PgPool, id: &str, error: &str) {
|
||||
if let Err(err) = sqlx::query(
|
||||
r#"UPDATE furumusic__torrent_session
|
||||
SET error = $2,
|
||||
updated_at = $3
|
||||
WHERE id = $1 AND status = 'resolving'"#,
|
||||
)
|
||||
.bind(id)
|
||||
.bind(error)
|
||||
.bind(now_string())
|
||||
.execute(pool)
|
||||
.await
|
||||
{
|
||||
tracing::warn!("failed to persist torrent metadata resolving error: {err}");
|
||||
}
|
||||
}
|
||||
|
||||
async fn mark_job_started(
|
||||
pool: &PgPool,
|
||||
id: &str,
|
||||
@@ -1048,6 +1283,65 @@ fn i64_to_u64(value: i64) -> u64 {
|
||||
value.max(0) as u64
|
||||
}
|
||||
|
||||
fn extract_magnet_info_hash(magnet: &str) -> Option<String> {
|
||||
if !magnet.starts_with("magnet:?") {
|
||||
return None;
|
||||
}
|
||||
magnet
|
||||
.split(['?', '&'])
|
||||
.find_map(|part| part.strip_prefix("xt=urn:btih:"))
|
||||
.map(|hash| percent_decode(hash).to_ascii_lowercase())
|
||||
.filter(|hash| !hash.is_empty())
|
||||
}
|
||||
|
||||
fn magnet_display_name(magnet: &str) -> Option<String> {
|
||||
magnet
|
||||
.split(['?', '&'])
|
||||
.find_map(|part| part.strip_prefix("dn="))
|
||||
.map(percent_decode)
|
||||
.map(|value| value.trim().to_string())
|
||||
.filter(|value| !value.is_empty())
|
||||
}
|
||||
|
||||
fn percent_decode(value: &str) -> String {
|
||||
let bytes = value.as_bytes();
|
||||
let mut out = Vec::with_capacity(bytes.len());
|
||||
let mut index = 0;
|
||||
while index < bytes.len() {
|
||||
match bytes[index] {
|
||||
b'+' => {
|
||||
out.push(b' ');
|
||||
index += 1;
|
||||
}
|
||||
b'%' if index + 2 < bytes.len() => {
|
||||
let hi = hex_value(bytes[index + 1]);
|
||||
let lo = hex_value(bytes[index + 2]);
|
||||
if let (Some(hi), Some(lo)) = (hi, lo) {
|
||||
out.push((hi << 4) | lo);
|
||||
index += 3;
|
||||
} else {
|
||||
out.push(bytes[index]);
|
||||
index += 1;
|
||||
}
|
||||
}
|
||||
byte => {
|
||||
out.push(byte);
|
||||
index += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
String::from_utf8_lossy(&out).to_string()
|
||||
}
|
||||
|
||||
fn hex_value(byte: u8) -> Option<u8> {
|
||||
match byte {
|
||||
b'0'..=b'9' => Some(byte - b'0'),
|
||||
b'a'..=b'f' => Some(byte - b'a' + 10),
|
||||
b'A'..=b'F' => Some(byte - b'A' + 10),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn sanitize_path_component(value: &str) -> String {
|
||||
let sanitized: String = value
|
||||
.chars()
|
||||
|
||||
@@ -667,6 +667,24 @@ tbody tr:hover {
|
||||
display: block;
|
||||
}
|
||||
|
||||
.settings-page {
|
||||
display: grid;
|
||||
grid-template-columns: minmax(560px, 760px) minmax(260px, 1fr);
|
||||
gap: 14px;
|
||||
align-items: start;
|
||||
}
|
||||
|
||||
.settings-card {
|
||||
padding: 14px;
|
||||
}
|
||||
|
||||
.settings-note {
|
||||
padding: 14px;
|
||||
color: var(--text-secondary);
|
||||
font-size: 12px;
|
||||
line-height: 1.55;
|
||||
}
|
||||
|
||||
.library-row {
|
||||
display: grid;
|
||||
grid-template-columns: 38px minmax(0, 1fr) 300px 130px;
|
||||
@@ -818,6 +836,11 @@ tbody tr:hover {
|
||||
<i data-lucide="wrench"></i>
|
||||
<span>Future Tools</span>
|
||||
</button>
|
||||
<button class="nav-btn" :class="{active: activeView === 'settings'}" @click="activeView = 'settings'; loadSettings()">
|
||||
<i data-lucide="settings"></i>
|
||||
<span>Settings</span>
|
||||
<span class="nav-count" x-text="settings.lastfm_api_key_configured ? 'ok' : ''"></span>
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<div class="nav-group">
|
||||
@@ -1236,6 +1259,48 @@ tbody tr:hover {
|
||||
</div>
|
||||
</section>
|
||||
</div>
|
||||
|
||||
<div class="content" x-show="activeView === 'settings'">
|
||||
<div class="settings-page">
|
||||
<section class="panel">
|
||||
<div class="panel-head">
|
||||
<div class="panel-title">
|
||||
<strong>External APIs</strong>
|
||||
<span>Keys used by scheduled enrichment jobs</span>
|
||||
</div>
|
||||
<span class="badge" :class="settings.lastfm_api_key_configured ? 'ok' : 'disabled'" x-text="settings.lastfm_api_key_configured ? 'configured' : 'not configured'"></span>
|
||||
</div>
|
||||
<form class="settings-card" @submit.prevent="saveSettings()">
|
||||
<div class="field">
|
||||
<label>Last.fm API key</label>
|
||||
<input type="password" x-model="settingsDraft.lastfm_api_key" autocomplete="off" placeholder="Paste Last.fm API key" />
|
||||
</div>
|
||||
<div class="toolbar">
|
||||
<button class="btn primary" type="submit">
|
||||
<i data-lucide="save"></i>
|
||||
Save
|
||||
</button>
|
||||
<button class="btn" type="button" @click="loadSettings()">
|
||||
<i data-lucide="refresh-cw"></i>
|
||||
Reload
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
</section>
|
||||
|
||||
<section class="panel">
|
||||
<div class="panel-head">
|
||||
<div class="panel-title">
|
||||
<strong>Last.fm Popularity</strong>
|
||||
<span>Weekly track rating refresh</span>
|
||||
</div>
|
||||
</div>
|
||||
<div class="settings-note">
|
||||
The scheduler uses Last.fm track.getInfo for each track, stores listeners, playcount, current rating, and a history row. The job processes tracks with missing or oldest ratings first and waits between requests to avoid Last.fm API limits.
|
||||
</div>
|
||||
</section>
|
||||
</div>
|
||||
</div>
|
||||
</main>
|
||||
|
||||
<div class="modal-backdrop" x-show="reviewModalOpen && activeReview" x-transition @click.self="reviewModalOpen = false">
|
||||
@@ -1366,6 +1431,8 @@ function adminV2() {
|
||||
activeLibraryItem: null,
|
||||
editorOpen: false,
|
||||
editorDraft: { title: '', hidden: 'false' },
|
||||
settings: { lastfm_api_key: '', lastfm_api_key_configured: false },
|
||||
settingsDraft: { lastfm_api_key: '' },
|
||||
poller: null,
|
||||
|
||||
async init() {
|
||||
@@ -1399,6 +1466,7 @@ function adminV2() {
|
||||
this.jobs = data.jobs || [];
|
||||
this.recentRuns = data.recent_runs || [];
|
||||
if (!this.activeJobName && this.jobs.length) this.activeJobName = this.jobs[0].name;
|
||||
await this.loadSettings(false);
|
||||
await this.loadLibrary(false);
|
||||
} catch (error) {
|
||||
this.showToast(error.message);
|
||||
@@ -1462,6 +1530,32 @@ function adminV2() {
|
||||
}
|
||||
},
|
||||
|
||||
async loadSettings(showErrors = true) {
|
||||
try {
|
||||
this.settings = await this.request(`${this.apiBase}/settings`);
|
||||
this.settingsDraft.lastfm_api_key = this.settings.lastfm_api_key || '';
|
||||
} catch (error) {
|
||||
if (showErrors) this.showToast(error.message);
|
||||
} finally {
|
||||
this.icons();
|
||||
}
|
||||
},
|
||||
|
||||
async saveSettings() {
|
||||
try {
|
||||
await this.request(`${this.apiBase}/settings`, {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({
|
||||
lastfm_api_key: this.settingsDraft.lastfm_api_key || ''
|
||||
})
|
||||
});
|
||||
await this.loadSettings(false);
|
||||
this.showToast('Settings saved');
|
||||
} catch (error) {
|
||||
this.showToast(error.message);
|
||||
}
|
||||
},
|
||||
|
||||
setReviewStatus(status) {
|
||||
this.reviewFilter.status = status;
|
||||
this.loadReviews();
|
||||
@@ -1770,6 +1864,7 @@ function adminV2() {
|
||||
if (this.activeView === 'library') return 'Library Workbench';
|
||||
if (this.activeView === 'jobs') return 'Tasks';
|
||||
if (this.activeView === 'tools') return 'Future Tools';
|
||||
if (this.activeView === 'settings') return 'Settings';
|
||||
return 'Review Queue';
|
||||
},
|
||||
|
||||
@@ -1777,6 +1872,7 @@ function adminV2() {
|
||||
if (this.activeView === 'library') return 'Fast entity control surface for artists, releases, and playlists';
|
||||
if (this.activeView === 'jobs') return 'Scheduler state, recent runs, and manual controls in one place';
|
||||
if (this.activeView === 'tools') return 'Reserved space for merge, split, enrichment, and destructive workflows';
|
||||
if (this.activeView === 'settings') return 'Application configuration and external API credentials';
|
||||
return 'Full-screen review triage with filter-aware bulk actions';
|
||||
},
|
||||
|
||||
|
||||
@@ -64,6 +64,15 @@
|
||||
:class="{ error: $store.torrents.error }"
|
||||
x-text="$store.torrents.message"></p>
|
||||
</div>
|
||||
<button class="torrent-modal-close"
|
||||
@click="$store.torrents.close()"
|
||||
title="{{ t.player_close }}"
|
||||
aria-label="{{ t.player_close }}">
|
||||
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.2">
|
||||
<line x1="18" y1="6" x2="6" y2="18"/>
|
||||
<line x1="6" y1="6" x2="18" y2="18"/>
|
||||
</svg>
|
||||
</button>
|
||||
<div class="torrent-client-status">
|
||||
<span class="torrent-status-pill"
|
||||
:class="{ active: $store.torrents.activeCount() > 0 }"
|
||||
@@ -74,7 +83,7 @@
|
||||
<span x-text="$store.torrents.agentSummary()"></span>
|
||||
</span>
|
||||
<span class="torrent-status-pill"
|
||||
x-text="$store.torrents.sessions.length + ' saved'"></span>
|
||||
x-text="$store.torrents.sessions.length + ' ' + T.saved"></span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -94,7 +103,7 @@
|
||||
</template>
|
||||
<template x-for="job in $store.torrents.sessions" :key="job.id">
|
||||
<div class="torrent-session-row"
|
||||
:class="{ active: $store.torrents.previewData && $store.torrents.previewData.id === job.id }"
|
||||
:class="{ active: $store.torrents.workspaceMode === 'session' && $store.torrents.previewData && $store.torrents.previewData.id === job.id }"
|
||||
@click="$store.torrents.openSession(job.id)">
|
||||
<div class="torrent-session-main">
|
||||
<div class="torrent-session-topline">
|
||||
@@ -109,19 +118,34 @@
|
||||
:style="'width:' + $store.torrents.progressValue(job) + '%'"></div>
|
||||
</div>
|
||||
</div>
|
||||
<button class="torrent-session-remove"
|
||||
@click.stop="$store.torrents.removeSession(job.id)">{{ t.player_delete }}</button>
|
||||
</div>
|
||||
</template>
|
||||
<button type="button"
|
||||
class="torrent-session-row torrent-session-add"
|
||||
:class="{ active: $store.torrents.isImporting() }"
|
||||
@click="$store.torrents.addNew()"
|
||||
:disabled="$store.torrents.loading">
|
||||
<span class="torrent-session-add-icon">+</span>
|
||||
<span>{{ t.player_upload }}</span>
|
||||
</button>
|
||||
</div>
|
||||
</aside>
|
||||
|
||||
<section class="torrent-workspace">
|
||||
<template x-if="$store.torrents.workspaceMode === 'empty'">
|
||||
<div class="empty-state torrent-workspace-empty">
|
||||
<p x-text="T.chooseSavedOrAddTorrent"></p>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<template x-if="$store.torrents.isImporting()">
|
||||
<div class="torrent-import-panel">
|
||||
<div class="torrent-modal-grid">
|
||||
<div>
|
||||
<label for="torrent-file-input">{{ t.player_torrent_file }}</label>
|
||||
<input id="torrent-file-input" type="file" accept=".torrent,application/x-bittorrent"
|
||||
@change="$store.torrents.file = $event.target.files[0] || null">
|
||||
<label for="local-file-input">{{ t.player_local_files }}</label>
|
||||
<input id="local-file-input" type="file" multiple accept="audio/*,.mp3,.flac,.wav,.m4a,.ogg,.opus,.aac"
|
||||
@change="$store.torrents.setLocalFiles($event.target.files)">
|
||||
<div class="torrent-upload-summary" x-text="$store.torrents.localUploadSummary()"></div>
|
||||
</div>
|
||||
<div>
|
||||
<label for="torrent-magnet-input">{{ t.player_magnet_link }}</label>
|
||||
@@ -129,13 +153,30 @@
|
||||
x-model="$store.torrents.magnet"
|
||||
placeholder="magnet:?xt=urn:btih:...">
|
||||
</div>
|
||||
<div>
|
||||
<label for="torrent-file-input">{{ t.player_torrent_file }}</label>
|
||||
<input id="torrent-file-input" type="file" accept=".torrent,application/x-bittorrent"
|
||||
@change="$store.torrents.file = $event.target.files[0] || null">
|
||||
</div>
|
||||
</div>
|
||||
<div class="torrent-upload-progress"
|
||||
x-show="$store.torrents.uploadProgress > 0 || ($store.torrents.localFiles.length > 0 && $store.torrents.loading)">
|
||||
<div class="torrent-progress-head">
|
||||
<span x-text="$store.torrents.uploadProgress >= 100 ? T.uploadComplete : T.uploadingFiles"></span>
|
||||
<span x-text="$store.torrents.uploadProgressText"></span>
|
||||
</div>
|
||||
<div class="torrent-progress-track">
|
||||
<div class="torrent-progress-bar"
|
||||
:style="'width:' + $store.torrents.uploadProgress + '%'"></div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="torrent-actions">
|
||||
<button class="modal-btn modal-btn-primary" @click="$store.torrents.preview()" :disabled="$store.torrents.loading">
|
||||
{{ t.player_preview_content }}
|
||||
{{ t.player_upload_content }}
|
||||
</button>
|
||||
<button class="modal-btn modal-btn-ghost" @click="$store.torrents.clearSelection()" :disabled="!$store.torrents.previewData">{{ t.player_clear }}</button>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<template x-if="$store.torrents.currentJob">
|
||||
<div class="torrent-progress-card">
|
||||
@@ -147,10 +188,32 @@
|
||||
<div class="torrent-progress-bar"
|
||||
:style="'width:' + $store.torrents.progressValue($store.torrents.currentJob) + '%'"></div>
|
||||
</div>
|
||||
<div class="torrent-progress-details">
|
||||
<span x-text="$store.torrents.bytes($store.torrents.currentJob.downloaded_bytes) + ' / ' + $store.torrents.bytes($store.torrents.currentJob.selected_size || $store.torrents.currentJob.total_size)"></span>
|
||||
<span x-text="$store.torrents.speedText($store.torrents.currentJob)"></span>
|
||||
<span x-text="$store.torrents.peerText($store.torrents.currentJob)"></span>
|
||||
<div class="torrent-progress-details"
|
||||
:class="{ completed: $store.torrents.isCompleted($store.torrents.currentJob) }">
|
||||
<span class="torrent-progress-metric">
|
||||
<span class="torrent-progress-label"
|
||||
x-text="$store.torrents.isCompleted($store.torrents.currentJob) ? T.size : T.downloaded"></span>
|
||||
<span class="torrent-progress-value"
|
||||
x-text="$store.torrents.progressDetailText($store.torrents.currentJob)"></span>
|
||||
</span>
|
||||
<span class="torrent-progress-metric"
|
||||
x-show="!$store.torrents.isCompleted($store.torrents.currentJob)">
|
||||
<span class="torrent-progress-label" x-text="T.speed"></span>
|
||||
<span class="torrent-progress-value"
|
||||
x-text="$store.torrents.speedText($store.torrents.currentJob)"></span>
|
||||
</span>
|
||||
<span class="torrent-progress-metric"
|
||||
x-show="!$store.torrents.isCompleted($store.torrents.currentJob)">
|
||||
<span class="torrent-progress-label" x-text="T.peers"></span>
|
||||
<span class="torrent-progress-value"
|
||||
x-text="$store.torrents.peerText($store.torrents.currentJob)"></span>
|
||||
</span>
|
||||
<span class="torrent-progress-metric"
|
||||
x-show="!$store.torrents.isCompleted($store.torrents.currentJob) && $store.torrents.etaText($store.torrents.currentJob)">
|
||||
<span class="torrent-progress-label" x-text="T.eta"></span>
|
||||
<span class="torrent-progress-value"
|
||||
x-text="$store.torrents.etaText($store.torrents.currentJob)"></span>
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
@@ -162,12 +225,19 @@
|
||||
<div class="torrent-preview-meta"
|
||||
x-text="$store.torrents.previewData.files.length + ' {{ t.player_files_count }} - ' + $store.torrents.bytes($store.torrents.previewData.total_size)"></div>
|
||||
</div>
|
||||
<div class="torrent-preview-actions">
|
||||
<button class="modal-btn"
|
||||
:class="$store.torrents.isCurrentDownloading() ? 'modal-btn-pause' : 'modal-btn-primary'"
|
||||
@click="$store.torrents.isCurrentDownloading() ? $store.torrents.pause() : $store.torrents.start()"
|
||||
:disabled="$store.torrents.loading">
|
||||
<span x-text="$store.torrents.isCurrentDownloading() ? '{{ t.player_pause_download }}' : '{{ t.player_download_selected }}'"></span>
|
||||
:class="$store.torrents.actionButtonClass()"
|
||||
@click="$store.torrents.toggleDownloadAction()"
|
||||
:disabled="$store.torrents.actionButtonDisabled()">
|
||||
<span x-text="$store.torrents.actionButtonText()"></span>
|
||||
</button>
|
||||
<button class="modal-btn modal-btn-danger"
|
||||
@click="$store.torrents.removeSession($store.torrents.previewData.id)"
|
||||
:disabled="$store.torrents.loading">
|
||||
{{ t.player_delete }}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
<div class="torrent-tree-toolbar">
|
||||
<div class="torrent-selected-summary"
|
||||
|
||||
+244
-16
@@ -21,6 +21,11 @@ const T = {
|
||||
audio: "{{ t.player_audio }}",
|
||||
size: "{{ t.player_size }}",
|
||||
uploader: "{{ t.player_uploader }}",
|
||||
lastfmRating: "{{ t.player_lastfm_rating }}",
|
||||
lastfmListeners: "{{ t.player_lastfm_listeners }}",
|
||||
lastfmPlaycount: "{{ t.player_lastfm_playcount }}",
|
||||
lastfmUpdated: "{{ t.player_lastfm_updated }}",
|
||||
lastfmNotLoaded: "{{ t.player_lastfm_not_loaded }}",
|
||||
trackWord: "{{ t.player_tracks_count }}",
|
||||
clientIdle: "{{ t.player_client_idle }}",
|
||||
active: "{{ t.player_active }}",
|
||||
@@ -29,13 +34,20 @@ const T = {
|
||||
processing: "{{ t.player_processing }}",
|
||||
queued: "{{ t.player_queued }}",
|
||||
saved: "{{ t.player_saved }}",
|
||||
chooseSavedOrAddTorrent: "{{ t.player_choose_saved_or_add_torrent }}",
|
||||
uploadFailed: "{{ t.player_upload_failed }}",
|
||||
uploadComplete: "{{ t.player_upload_complete }}",
|
||||
uploadingFiles: "{{ t.player_uploading_files }}",
|
||||
preview: "{{ t.player_preview }}",
|
||||
resolving: "{{ t.player_resolving }}",
|
||||
downloading: "{{ t.player_downloading }}",
|
||||
moving: "{{ t.player_moving }}",
|
||||
completed: "{{ t.player_completed }}",
|
||||
failed: "{{ t.player_failed }}",
|
||||
paused: "{{ t.player_paused }}",
|
||||
noTorrentSelected: "{{ t.player_no_torrent_selected }}",
|
||||
downloaded: "{{ t.player_downloaded }}",
|
||||
speed: "{{ t.player_speed }}",
|
||||
down: "{{ t.player_down }}",
|
||||
up: "{{ t.player_up }}",
|
||||
peers: "{{ t.player_peers }}",
|
||||
@@ -43,6 +55,8 @@ const T = {
|
||||
seen: "{{ t.player_seen }}",
|
||||
eta: "{{ t.player_eta }}",
|
||||
selected: "{{ t.player_selected }}",
|
||||
downloadSelected: "{{ t.player_download_selected }}",
|
||||
pauseDownload: "{{ t.player_pause_download }}",
|
||||
chooseTorrent: "{{ t.player_choose_torrent }}",
|
||||
readingTorrent: "{{ t.player_reading_torrent }}",
|
||||
resolvingMagnet: "{{ t.player_resolving_magnet }}",
|
||||
@@ -816,6 +830,15 @@ document.addEventListener('alpine:init', () => {
|
||||
`${T.size}: ${this.bytes(track.file_size_bytes)}`,
|
||||
`${T.uploader}: ${track.uploader_name || 'UFO'}`,
|
||||
];
|
||||
if (track.lastfm_rating != null || track.lastfm_listeners != null || track.lastfm_playcount != null) {
|
||||
const rating = Number(track.lastfm_rating || 0);
|
||||
lines.push(`${T.lastfmRating}: ${Number.isFinite(rating) ? rating.toFixed(2) : T.unknown}`);
|
||||
lines.push(`${T.lastfmListeners}: ${new Intl.NumberFormat().format(track.lastfm_listeners || 0)}`);
|
||||
lines.push(`${T.lastfmPlaycount}: ${new Intl.NumberFormat().format(track.lastfm_playcount || 0)}`);
|
||||
if (track.lastfm_updated_at) lines.push(`${T.lastfmUpdated}: ${track.lastfm_updated_at}`);
|
||||
} else {
|
||||
lines.push(`${T.lastfmRating}: ${T.lastfmNotLoaded}`);
|
||||
}
|
||||
return lines.join('\n');
|
||||
},
|
||||
|
||||
@@ -1078,11 +1101,13 @@ document.addEventListener('alpine:init', () => {
|
||||
Alpine.store('torrents', {
|
||||
modal: false,
|
||||
file: null,
|
||||
localFiles: [],
|
||||
magnet: '',
|
||||
sessions: [],
|
||||
loadingSessions: false,
|
||||
currentJob: null,
|
||||
previewData: null,
|
||||
workspaceMode: 'empty',
|
||||
treeRoot: null,
|
||||
selected: new Set(),
|
||||
expanded: new Set(),
|
||||
@@ -1090,10 +1115,13 @@ document.addEventListener('alpine:init', () => {
|
||||
message: '',
|
||||
error: false,
|
||||
_pollTimer: null,
|
||||
_pollJobId: null,
|
||||
_refreshTimer: null,
|
||||
queuedTasks: 0,
|
||||
processingTasks: 0,
|
||||
loadingAgentStatus: false,
|
||||
uploadProgress: 0,
|
||||
uploadProgressText: '',
|
||||
|
||||
open() {
|
||||
this.modal = true;
|
||||
@@ -1107,6 +1135,34 @@ document.addEventListener('alpine:init', () => {
|
||||
close() {
|
||||
this.modal = false;
|
||||
this._stopRefresh();
|
||||
this._stopPoll();
|
||||
},
|
||||
|
||||
_stopPoll() {
|
||||
if (this._pollTimer) clearInterval(this._pollTimer);
|
||||
this._pollTimer = null;
|
||||
this._pollJobId = null;
|
||||
},
|
||||
|
||||
isImporting() {
|
||||
return this.workspaceMode === 'new';
|
||||
},
|
||||
|
||||
addNew() {
|
||||
if (this.loading) return;
|
||||
this._stopPoll();
|
||||
this.workspaceMode = 'new';
|
||||
this.file = null;
|
||||
this.localFiles = [];
|
||||
this.magnet = '';
|
||||
this.uploadProgress = 0;
|
||||
this.uploadProgressText = '';
|
||||
this.currentJob = null;
|
||||
this.previewData = null;
|
||||
this.treeRoot = null;
|
||||
this.selected = new Set();
|
||||
this.expanded = new Set();
|
||||
this._setMessage(T.chooseTorrent);
|
||||
},
|
||||
|
||||
_setMessage(message, error = false) {
|
||||
@@ -1138,6 +1194,35 @@ document.addEventListener('alpine:init', () => {
|
||||
return this.isDownloading(this.currentJob);
|
||||
},
|
||||
|
||||
isCompleted(job) {
|
||||
return this.normalizedStatus(job) === 'completed';
|
||||
},
|
||||
|
||||
isCurrentCompleted() {
|
||||
return this.isCompleted(this.currentJob);
|
||||
},
|
||||
|
||||
selectedArray() {
|
||||
return [...this.selected].sort((a, b) => Number(a) - Number(b));
|
||||
},
|
||||
|
||||
selectionMatchesJob(job) {
|
||||
if (!job) return false;
|
||||
const saved = Array.isArray(job.selected_files) ? job.selected_files : [];
|
||||
const selected = this.selectedArray();
|
||||
if (saved.length !== selected.length) return false;
|
||||
const savedSet = new Set(saved.map(index => Number(index)));
|
||||
return selected.every(index => savedSet.has(Number(index)));
|
||||
},
|
||||
|
||||
hasCurrentSelectionChanges() {
|
||||
return !!this.currentJob && !this.selectionMatchesJob(this.currentJob);
|
||||
},
|
||||
|
||||
isCurrentCompletedLocked() {
|
||||
return this.isCurrentCompleted() && !this.hasCurrentSelectionChanges();
|
||||
},
|
||||
|
||||
normalizedStatus(job) {
|
||||
const status = String(job?.status || 'preview').toLowerCase();
|
||||
if (status === 'complete') return 'completed';
|
||||
@@ -1147,6 +1232,7 @@ document.addEventListener('alpine:init', () => {
|
||||
statusLabel(job) {
|
||||
const labels = {
|
||||
preview: T.preview,
|
||||
resolving: T.resolving,
|
||||
downloading: T.downloading,
|
||||
moving: T.moving,
|
||||
completed: T.completed,
|
||||
@@ -1195,15 +1281,51 @@ document.addEventListener('alpine:init', () => {
|
||||
speedText(job) {
|
||||
if (!job) return '0 B/s';
|
||||
const down = Number(job.download_speed_mbps || 0);
|
||||
const up = Number(job.upload_speed_mbps || 0);
|
||||
return T.down + ' ' + down.toFixed(2) + ' MiB/s - ' + T.up + ' ' + up.toFixed(2) + ' MiB/s';
|
||||
return down.toFixed(2) + ' MiB/s';
|
||||
},
|
||||
|
||||
peerText(job) {
|
||||
if (!job) return T.peers + ' n/a';
|
||||
if (!job) return 'n/a';
|
||||
const live = job.peers_live == null ? '?' : job.peers_live;
|
||||
const seen = job.peers_seen == null ? '?' : job.peers_seen;
|
||||
return T.peers + ' ' + live + ' ' + T.live + ' / ' + seen + ' ' + T.seen + (job.eta ? ' - ' + T.eta + ' ' + job.eta : '');
|
||||
return live + ' ' + T.live + ' / ' + seen + ' ' + T.seen;
|
||||
},
|
||||
|
||||
etaText(job) {
|
||||
return job && job.eta ? job.eta : '';
|
||||
},
|
||||
|
||||
progressDetailText(job) {
|
||||
if (!job) return '';
|
||||
const size = this.bytes(job.selected_size || job.total_size);
|
||||
if (this.isCompleted(job)) return size;
|
||||
return this.bytes(job.downloaded_bytes) + ' / ' + size;
|
||||
},
|
||||
|
||||
actionButtonClass() {
|
||||
if (this.isCurrentCompletedLocked()) return 'modal-btn-ghost';
|
||||
return this.isCurrentDownloading() ? 'modal-btn-pause' : 'modal-btn-primary';
|
||||
},
|
||||
|
||||
actionButtonText() {
|
||||
if (this.normalizedStatus(this.currentJob) === 'resolving') return T.resolving;
|
||||
if (this.isCurrentCompletedLocked()) return T.completed;
|
||||
return this.isCurrentDownloading() ? T.pauseDownload : T.downloadSelected;
|
||||
},
|
||||
|
||||
actionButtonDisabled() {
|
||||
return this.loading
|
||||
|| this.isCurrentCompletedLocked()
|
||||
|| this.normalizedStatus(this.currentJob) === 'resolving'
|
||||
|| !this.previewData
|
||||
|| !Array.isArray(this.previewData.files)
|
||||
|| this.previewData.files.length === 0;
|
||||
},
|
||||
|
||||
toggleDownloadAction() {
|
||||
if (this.isCurrentCompletedLocked()) return;
|
||||
if (this.isCurrentDownloading()) this.pause();
|
||||
else this.start();
|
||||
},
|
||||
|
||||
sessionMeta(job) {
|
||||
@@ -1245,13 +1367,33 @@ document.addEventListener('alpine:init', () => {
|
||||
_rememberJob(job) {
|
||||
if (!job || !job.id) return;
|
||||
const rest = this.sessions.filter(item => item.id !== job.id);
|
||||
this.sessions = [job, ...rest].sort((a, b) => String(b.updated_at || '').localeCompare(String(a.updated_at || '')));
|
||||
this.sessions = [job, ...rest].sort((a, b) => String(b.created_at || '').localeCompare(String(a.created_at || '')));
|
||||
if (this.currentJob && this.currentJob.id === job.id) this.currentJob = job;
|
||||
},
|
||||
|
||||
_isSelectedJob(id) {
|
||||
return this.workspaceMode === 'session'
|
||||
&& this.currentJob
|
||||
&& this.currentJob.id === id
|
||||
&& this.previewData
|
||||
&& this.previewData.id === id;
|
||||
},
|
||||
|
||||
_syncCurrentJobFromSessions() {
|
||||
if (!this.currentJob || !this.previewData) return;
|
||||
const selected = this.sessions.find(job => job.id === this.currentJob.id && job.id === this.previewData.id);
|
||||
if (selected) this.currentJob = selected;
|
||||
},
|
||||
|
||||
_applySession(data) {
|
||||
const preview = data.preview || data;
|
||||
const job = data.job || null;
|
||||
this.workspaceMode = 'session';
|
||||
this.file = null;
|
||||
this.localFiles = [];
|
||||
this.magnet = '';
|
||||
this.uploadProgress = 0;
|
||||
this.uploadProgressText = '';
|
||||
this.previewData = preview;
|
||||
this.currentJob = job;
|
||||
const selected = Array.isArray(data.selected_files) && data.selected_files.length
|
||||
@@ -1269,6 +1411,8 @@ document.addEventListener('alpine:init', () => {
|
||||
const data = await res.json();
|
||||
if (!res.ok) throw new Error(data.error || T.loadTorrentsFailed);
|
||||
this.sessions = Array.isArray(data) ? data : [];
|
||||
this._syncCurrentJobFromSessions();
|
||||
await this._refreshResolvedSelection();
|
||||
} catch (err) {
|
||||
this._setMessage(err.message || String(err), true);
|
||||
} finally {
|
||||
@@ -1276,8 +1420,22 @@ document.addEventListener('alpine:init', () => {
|
||||
}
|
||||
},
|
||||
|
||||
async _refreshResolvedSelection() {
|
||||
if (!this.currentJob || !this.previewData || (this.previewData.files || []).length > 0) return;
|
||||
const selected = this.sessions.find(job => job.id === this.currentJob.id);
|
||||
if (!selected || this.normalizedStatus(selected) === 'resolving') return;
|
||||
try {
|
||||
const res = await fetch(`/api/player/torrents/session/${selected.id}`);
|
||||
const data = await res.json();
|
||||
if (!res.ok) return;
|
||||
this._applySession(data);
|
||||
this._setMessage(T.allFilesSelected);
|
||||
} catch {}
|
||||
},
|
||||
|
||||
async openSession(id) {
|
||||
if (!id || this.loading) return;
|
||||
this._stopPoll();
|
||||
this.loading = true;
|
||||
this._setMessage(T.openingSavedTorrent);
|
||||
try {
|
||||
@@ -1310,6 +1468,7 @@ document.addEventListener('alpine:init', () => {
|
||||
this.currentJob = null;
|
||||
this.treeRoot = null;
|
||||
this.selected = new Set();
|
||||
this.workspaceMode = this.sessions.length ? 'empty' : 'new';
|
||||
}
|
||||
this._setMessage(T.torrentRemoved);
|
||||
} catch (err) {
|
||||
@@ -1331,8 +1490,76 @@ document.addEventListener('alpine:init', () => {
|
||||
});
|
||||
},
|
||||
|
||||
setLocalFiles(files) {
|
||||
this.localFiles = Array.from(files || []);
|
||||
},
|
||||
|
||||
localUploadBytes() {
|
||||
return this.localFiles.reduce((sum, file) => sum + Number(file.size || 0), 0);
|
||||
},
|
||||
|
||||
localUploadSummary() {
|
||||
const count = this.localFiles.length;
|
||||
if (count === 0) return '';
|
||||
return count + ' ' + T.selected + ' - ' + this.bytes(this.localUploadBytes());
|
||||
},
|
||||
|
||||
uploadLocalFile(file, loadedBefore, totalBytes) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const xhr = new XMLHttpRequest();
|
||||
xhr.open('POST', '/api/player/uploads/local');
|
||||
xhr.setRequestHeader('Content-Type', file.type || 'application/octet-stream');
|
||||
xhr.setRequestHeader('X-Furumusic-Filename', encodeURIComponent(file.name || 'upload.mp3'));
|
||||
xhr.upload.onprogress = event => {
|
||||
if (!event.lengthComputable || totalBytes <= 0) return;
|
||||
const loaded = loadedBefore + event.loaded;
|
||||
this.uploadProgress = Math.max(0, Math.min(100, loaded / totalBytes * 100));
|
||||
this.uploadProgressText = this.uploadProgress.toFixed(1) + '%';
|
||||
};
|
||||
xhr.onload = () => {
|
||||
let data = {};
|
||||
try { data = JSON.parse(xhr.responseText || '{}'); } catch {}
|
||||
if (xhr.status >= 200 && xhr.status < 300) resolve(data);
|
||||
else reject(new Error(data.error || T.uploadFailed));
|
||||
};
|
||||
xhr.onerror = () => reject(new Error(T.uploadFailed));
|
||||
xhr.send(file);
|
||||
});
|
||||
},
|
||||
|
||||
async uploadLocalFiles() {
|
||||
if (this.loading || this.localFiles.length === 0) return;
|
||||
this.loading = true;
|
||||
this.uploadProgress = 0;
|
||||
this.uploadProgressText = '0.0%';
|
||||
this._setMessage(T.uploadingFiles);
|
||||
const totalBytes = this.localUploadBytes();
|
||||
let loadedBefore = 0;
|
||||
try {
|
||||
for (const file of this.localFiles) {
|
||||
await this.uploadLocalFile(file, loadedBefore, totalBytes);
|
||||
loadedBefore += Number(file.size || 0);
|
||||
this.uploadProgress = totalBytes > 0 ? Math.min(100, loadedBefore / totalBytes * 100) : 100;
|
||||
this.uploadProgressText = this.uploadProgress.toFixed(1) + '%';
|
||||
}
|
||||
this.localFiles = [];
|
||||
this.uploadProgress = 100;
|
||||
this.uploadProgressText = '100.0%';
|
||||
this._setMessage(T.uploadComplete);
|
||||
await this.loadAgentStatus();
|
||||
} catch (err) {
|
||||
this._setMessage(err.message || String(err), true);
|
||||
} finally {
|
||||
this.loading = false;
|
||||
}
|
||||
},
|
||||
|
||||
async preview() {
|
||||
if (this.loading) return;
|
||||
if (this.localFiles.length > 0) {
|
||||
await this.uploadLocalFiles();
|
||||
return;
|
||||
}
|
||||
const magnet = this.magnet.trim();
|
||||
if (!this.file && !magnet) {
|
||||
this._setMessage(T.chooseTorrent, true);
|
||||
@@ -1343,6 +1570,7 @@ document.addEventListener('alpine:init', () => {
|
||||
this.previewData = null;
|
||||
this.treeRoot = null;
|
||||
this.currentJob = null;
|
||||
this.workspaceMode = 'new';
|
||||
this.selected = new Set();
|
||||
this.expanded = new Set();
|
||||
this._setMessage(this.file ? T.readingTorrent : T.resolvingMagnet);
|
||||
@@ -1360,7 +1588,7 @@ document.addEventListener('alpine:init', () => {
|
||||
if (!res.ok) throw new Error(data.error || T.previewFailed);
|
||||
|
||||
this._applySession(data);
|
||||
this._setMessage(T.allFilesSelected);
|
||||
this._setMessage((data.preview?.files || []).length ? T.allFilesSelected : T.resolving);
|
||||
await this.loadSessions();
|
||||
} catch (err) {
|
||||
this._setMessage(err.message || String(err), true);
|
||||
@@ -1554,7 +1782,7 @@ document.addEventListener('alpine:init', () => {
|
||||
|
||||
async start() {
|
||||
if (!this.previewData || this.loading) return;
|
||||
const selected = [...this.selected];
|
||||
const selected = this.selectedArray();
|
||||
if (selected.length === 0) {
|
||||
this._setMessage(T.selectOneFile, true);
|
||||
return;
|
||||
@@ -1596,8 +1824,7 @@ document.addEventListener('alpine:init', () => {
|
||||
this.currentJob = data;
|
||||
this._rememberJob(data);
|
||||
this._setMessage(T.downloadPaused);
|
||||
if (this._pollTimer) clearInterval(this._pollTimer);
|
||||
this._pollTimer = null;
|
||||
this._stopPoll();
|
||||
await this.loadSessions();
|
||||
} catch (err) {
|
||||
this._setMessage(err.message || String(err), true);
|
||||
@@ -1607,28 +1834,29 @@ document.addEventListener('alpine:init', () => {
|
||||
},
|
||||
|
||||
_poll(id) {
|
||||
if (this._pollTimer) clearInterval(this._pollTimer);
|
||||
this._stopPoll();
|
||||
this._pollJobId = id;
|
||||
this._pollTimer = setInterval(async () => {
|
||||
try {
|
||||
const res = await fetch(`/api/player/torrents/${id}/status`);
|
||||
const data = await res.json();
|
||||
if (!res.ok) throw new Error(data.error || T.statusFailed);
|
||||
this.currentJob = data;
|
||||
this._rememberJob(data);
|
||||
if (this._isSelectedJob(id)) {
|
||||
this.currentJob = data;
|
||||
this._setMessage(
|
||||
this.statusLabel(data) + ' - ' + this.progressValue(data).toFixed(1) + '% - ' + this.bytes(data.downloaded_bytes),
|
||||
data.status === 'failed'
|
||||
);
|
||||
}
|
||||
if (data.status === 'complete' || data.status === 'failed') {
|
||||
clearInterval(this._pollTimer);
|
||||
this._pollTimer = null;
|
||||
this._stopPoll();
|
||||
this.loadSessions();
|
||||
this.loadAgentStatus();
|
||||
}
|
||||
} catch (err) {
|
||||
this._setMessage(err.message || String(err), true);
|
||||
clearInterval(this._pollTimer);
|
||||
this._pollTimer = null;
|
||||
if (this._isSelectedJob(id)) this._setMessage(err.message || String(err), true);
|
||||
this._stopPoll();
|
||||
}
|
||||
}, 2000);
|
||||
},
|
||||
|
||||
+178
-43
@@ -1741,6 +1741,11 @@ button.user-stat:hover {
|
||||
color: #111;
|
||||
}
|
||||
.modal-btn-ghost { background: transparent; color: var(--text-secondary); }
|
||||
.modal-btn-danger {
|
||||
background: rgba(229,96,96,0.16);
|
||||
color: #ffb9b9;
|
||||
border: 1px solid rgba(229,96,96,0.32);
|
||||
}
|
||||
|
||||
.modal-footer {
|
||||
display: flex;
|
||||
@@ -1749,9 +1754,10 @@ button.user-stat:hover {
|
||||
}
|
||||
|
||||
.torrent-modal {
|
||||
width: min(860px, calc(100vw - 32px));
|
||||
max-width: 860px;
|
||||
max-height: min(88dvh, 760px);
|
||||
width: min(1180px, calc(100vw - 48px));
|
||||
max-width: 1180px;
|
||||
height: min(820px, calc(100dvh - 64px));
|
||||
max-height: calc(100dvh - 64px);
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
@@ -1761,6 +1767,8 @@ button.user-stat:hover {
|
||||
justify-content: space-between;
|
||||
gap: 14px;
|
||||
margin-bottom: 12px;
|
||||
flex: 0 0 auto;
|
||||
position: relative;
|
||||
}
|
||||
|
||||
.torrent-modal-head h3 {
|
||||
@@ -1792,6 +1800,25 @@ button.user-stat:hover {
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
.torrent-modal-close {
|
||||
display: none;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
flex: 0 0 auto;
|
||||
width: 34px;
|
||||
height: 34px;
|
||||
border: 1px solid var(--border-color);
|
||||
border-radius: 999px;
|
||||
background: var(--bg-primary);
|
||||
color: var(--text-secondary);
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.torrent-modal-close svg {
|
||||
width: 18px;
|
||||
height: 18px;
|
||||
}
|
||||
|
||||
.torrent-status-pill.active {
|
||||
border-color: rgba(29,185,84,0.42);
|
||||
color: #9ff0b9;
|
||||
@@ -1816,9 +1843,10 @@ button.user-stat:hover {
|
||||
|
||||
.torrent-manager-layout {
|
||||
display: grid;
|
||||
grid-template-columns: minmax(210px, 260px) minmax(0, 1fr);
|
||||
grid-template-columns: minmax(260px, 320px) minmax(0, 1fr);
|
||||
gap: 14px;
|
||||
min-height: 0;
|
||||
flex: 1 1 auto;
|
||||
}
|
||||
|
||||
.torrent-manager-sidebar,
|
||||
@@ -1851,19 +1879,52 @@ button.user-stat:hover {
|
||||
|
||||
.torrent-session-list {
|
||||
overflow-y: auto;
|
||||
min-height: 150px;
|
||||
max-height: min(52vh, 470px);
|
||||
min-height: 0;
|
||||
max-height: none;
|
||||
flex: 1 1 auto;
|
||||
}
|
||||
|
||||
.torrent-session-row {
|
||||
display: grid;
|
||||
grid-template-columns: minmax(0, 1fr) auto;
|
||||
grid-template-columns: minmax(0, 1fr);
|
||||
gap: 8px;
|
||||
padding: 10px 12px;
|
||||
border-bottom: 1px solid var(--border-color);
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.torrent-session-add {
|
||||
width: 100%;
|
||||
grid-template-columns: auto minmax(0, 1fr);
|
||||
align-items: center;
|
||||
border: 0;
|
||||
border-bottom: 1px solid var(--border-color);
|
||||
background: transparent;
|
||||
color: var(--text-secondary);
|
||||
text-align: left;
|
||||
font: inherit;
|
||||
font-size: 13px;
|
||||
font-weight: 800;
|
||||
}
|
||||
|
||||
.torrent-session-add:disabled {
|
||||
cursor: default;
|
||||
opacity: 0.6;
|
||||
}
|
||||
|
||||
.torrent-session-add-icon {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
width: 22px;
|
||||
height: 22px;
|
||||
border-radius: 999px;
|
||||
border: 1px solid rgba(29,185,84,0.38);
|
||||
color: #9ff0b9;
|
||||
font-size: 16px;
|
||||
line-height: 1;
|
||||
}
|
||||
|
||||
.torrent-session-row:last-child { border-bottom: 0; }
|
||||
.torrent-session-row:hover,
|
||||
.torrent-session-row.active { background: var(--bg-hover); }
|
||||
@@ -1903,19 +1964,28 @@ button.user-stat:hover {
|
||||
}
|
||||
|
||||
.torrent-status-badge.status-preview {
|
||||
background: rgba(122,162,255,0.14);
|
||||
color: #a8c0ff;
|
||||
background: rgba(122,162,255,0.16);
|
||||
color: #adc3ff;
|
||||
}
|
||||
|
||||
.torrent-status-badge.status-downloading,
|
||||
.torrent-status-badge.status-moving {
|
||||
.torrent-status-badge.status-resolving {
|
||||
background: rgba(182,141,255,0.16);
|
||||
color: #d0b6ff;
|
||||
}
|
||||
|
||||
.torrent-status-badge.status-downloading {
|
||||
background: rgba(29,185,84,0.16);
|
||||
color: #9ff0b9;
|
||||
}
|
||||
|
||||
.torrent-status-badge.status-moving {
|
||||
background: rgba(75,198,240,0.16);
|
||||
color: #a8e8ff;
|
||||
}
|
||||
|
||||
.torrent-status-badge.status-completed {
|
||||
background: rgba(105,214,161,0.2);
|
||||
color: #b8ffd2;
|
||||
background: rgba(110,211,123,0.16);
|
||||
color: #b8f7be;
|
||||
}
|
||||
|
||||
.torrent-status-badge.status-paused {
|
||||
@@ -1953,23 +2023,6 @@ button.user-stat:hover {
|
||||
transition: width 0.25s ease;
|
||||
}
|
||||
|
||||
.torrent-session-remove {
|
||||
align-self: flex-start;
|
||||
border: 1px solid rgba(229,96,96,0.24);
|
||||
background: rgba(229,96,96,0.12);
|
||||
color: #ffb9b9;
|
||||
border-radius: 5px;
|
||||
padding: 4px 7px;
|
||||
font-size: 11px;
|
||||
font-weight: 800;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.torrent-session-remove:hover {
|
||||
background: rgba(229,96,96,0.2);
|
||||
color: #ffd7d7;
|
||||
}
|
||||
|
||||
.torrent-progress-card {
|
||||
margin-top: 10px;
|
||||
padding: 10px 12px;
|
||||
@@ -2005,12 +2058,44 @@ button.user-stat:hover {
|
||||
}
|
||||
|
||||
.torrent-progress-details {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
gap: 8px 12px;
|
||||
display: grid;
|
||||
grid-template-columns: repeat(4, minmax(0, 1fr));
|
||||
gap: 8px;
|
||||
margin-top: 8px;
|
||||
color: var(--text-subdued);
|
||||
min-height: 38px;
|
||||
}
|
||||
|
||||
.torrent-progress-details.completed {
|
||||
grid-template-columns: minmax(0, 1fr);
|
||||
}
|
||||
|
||||
.torrent-progress-metric {
|
||||
min-width: 0;
|
||||
min-height: 38px;
|
||||
padding: 5px 7px;
|
||||
border-radius: 6px;
|
||||
background: var(--bg-secondary);
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
justify-content: center;
|
||||
gap: 2px;
|
||||
}
|
||||
|
||||
.torrent-progress-label {
|
||||
color: var(--text-muted);
|
||||
font-size: 10px;
|
||||
line-height: 12px;
|
||||
text-transform: uppercase;
|
||||
font-weight: 800;
|
||||
}
|
||||
|
||||
.torrent-progress-value {
|
||||
color: var(--text-secondary);
|
||||
font-size: 12px;
|
||||
line-height: 14px;
|
||||
font-weight: 700;
|
||||
overflow-wrap: anywhere;
|
||||
}
|
||||
|
||||
.history-modal {
|
||||
@@ -2074,6 +2159,26 @@ button.user-stat:hover {
|
||||
max-width: 360px;
|
||||
}
|
||||
|
||||
.torrent-import-panel,
|
||||
.torrent-workspace-empty {
|
||||
min-height: 150px;
|
||||
}
|
||||
|
||||
.torrent-upload-summary {
|
||||
min-height: 16px;
|
||||
margin-top: 5px;
|
||||
color: var(--text-subdued);
|
||||
font-size: 11px;
|
||||
}
|
||||
|
||||
.torrent-upload-progress {
|
||||
margin-top: 10px;
|
||||
padding: 10px 12px;
|
||||
border: 1px solid var(--border-color);
|
||||
border-radius: 8px;
|
||||
background: var(--bg-primary);
|
||||
}
|
||||
|
||||
.torrent-modal label {
|
||||
display: block;
|
||||
margin-bottom: 6px;
|
||||
@@ -2118,6 +2223,13 @@ button.user-stat:hover {
|
||||
margin-top: 16px;
|
||||
}
|
||||
|
||||
.torrent-preview-actions {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
justify-content: flex-end;
|
||||
gap: 8px;
|
||||
}
|
||||
|
||||
.torrent-preview-title {
|
||||
min-width: 0;
|
||||
font-size: 14px;
|
||||
@@ -2174,7 +2286,7 @@ button.user-stat:hover {
|
||||
margin-top: 10px;
|
||||
overflow-y: auto;
|
||||
min-height: 140px;
|
||||
max-height: min(46vh, 420px);
|
||||
max-height: none;
|
||||
border: 1px solid var(--border-color);
|
||||
border-radius: 8px;
|
||||
background: var(--bg-primary);
|
||||
@@ -2373,7 +2485,7 @@ button.user-stat:hover {
|
||||
}
|
||||
|
||||
.torrent-modal {
|
||||
width: calc(100vw - 24px);
|
||||
width: min(1180px, calc(100vw - 32px));
|
||||
}
|
||||
|
||||
.card-grid {
|
||||
@@ -2717,34 +2829,49 @@ button.user-stat:hover {
|
||||
}
|
||||
|
||||
.info-modal,
|
||||
.torrent-modal,
|
||||
.history-modal {
|
||||
width: min(400px, calc(100vw - 24px));
|
||||
max-width: 400px;
|
||||
}
|
||||
|
||||
.torrent-modal {
|
||||
max-height: min(82dvh, 640px);
|
||||
padding: 20px;
|
||||
width: 100vw;
|
||||
max-width: none;
|
||||
height: 100dvh;
|
||||
max-height: none;
|
||||
border-radius: 0;
|
||||
padding: calc(14px + env(safe-area-inset-top)) 14px calc(14px + env(safe-area-inset-bottom));
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
.torrent-modal-head {
|
||||
flex-direction: column;
|
||||
display: grid;
|
||||
grid-template-columns: minmax(0, 1fr) auto;
|
||||
gap: 8px;
|
||||
align-items: start;
|
||||
}
|
||||
|
||||
.torrent-client-status {
|
||||
grid-column: 1 / -1;
|
||||
justify-content: flex-start;
|
||||
}
|
||||
|
||||
.torrent-modal-close {
|
||||
display: inline-flex;
|
||||
}
|
||||
|
||||
.torrent-manager-layout {
|
||||
grid-template-columns: 1fr;
|
||||
gap: 10px;
|
||||
}
|
||||
|
||||
.torrent-session-list {
|
||||
max-height: 148px;
|
||||
min-height: 96px;
|
||||
max-height: none;
|
||||
min-height: 0;
|
||||
}
|
||||
|
||||
.torrent-manager-sidebar {
|
||||
flex: 0 0 178px;
|
||||
}
|
||||
|
||||
.torrent-progress-head {
|
||||
@@ -2753,6 +2880,14 @@ button.user-stat:hover {
|
||||
gap: 4px;
|
||||
}
|
||||
|
||||
.torrent-progress-details {
|
||||
grid-template-columns: repeat(2, minmax(0, 1fr));
|
||||
}
|
||||
|
||||
.torrent-progress-details.completed {
|
||||
grid-template-columns: minmax(0, 1fr);
|
||||
}
|
||||
|
||||
.torrent-modal h3 {
|
||||
margin-bottom: 12px;
|
||||
}
|
||||
@@ -2790,8 +2925,8 @@ button.user-stat:hover {
|
||||
}
|
||||
|
||||
.torrent-file-tree {
|
||||
min-height: 120px;
|
||||
max-height: min(32dvh, 260px);
|
||||
min-height: 0;
|
||||
max-height: none;
|
||||
}
|
||||
|
||||
.torrent-tree-row {
|
||||
|
||||
Reference in New Issue
Block a user