Formatting

This commit is contained in:
Connor Johnstone
2026-03-18 15:37:10 -04:00
parent 1f36374394
commit 93392db27c
8 changed files with 250 additions and 161 deletions

View File

@@ -1,4 +1,4 @@
use actix_web::{web, HttpResponse};
use actix_web::{HttpResponse, web};
use serde::Deserialize;
use shanty_db::entities::download_queue::DownloadStatus;
@@ -25,13 +25,14 @@ pub fn configure(cfg: &mut web::ServiceConfig) {
);
}
async fn get_status(
state: web::Data<AppState>,
) -> Result<HttpResponse, ApiError> {
async fn get_status(state: web::Data<AppState>) -> Result<HttpResponse, ApiError> {
let summary = shanty_watch::library_summary(state.db.conn()).await?;
let pending_items = queries::downloads::list(state.db.conn(), Some(DownloadStatus::Pending)).await?;
let downloading_items = queries::downloads::list(state.db.conn(), Some(DownloadStatus::Downloading)).await?;
let failed_items = queries::downloads::list(state.db.conn(), Some(DownloadStatus::Failed)).await?;
let pending_items =
queries::downloads::list(state.db.conn(), Some(DownloadStatus::Pending)).await?;
let downloading_items =
queries::downloads::list(state.db.conn(), Some(DownloadStatus::Downloading)).await?;
let failed_items =
queries::downloads::list(state.db.conn(), Some(DownloadStatus::Failed)).await?;
let tasks = state.tasks.list();
let mut queue_items = Vec::new();
@@ -57,16 +58,16 @@ async fn get_status(
})))
}
async fn trigger_index(
state: web::Data<AppState>,
) -> Result<HttpResponse, ApiError> {
async fn trigger_index(state: web::Data<AppState>) -> Result<HttpResponse, ApiError> {
let task_id = state.tasks.register("index");
let state = state.clone();
let tid = task_id.clone();
tokio::spawn(async move {
let cfg = state.config.read().await.clone();
state.tasks.update_progress(&tid, 0, 0, "Scanning library...");
state
.tasks
.update_progress(&tid, 0, 0, "Scanning library...");
let scan_config = shanty_index::ScanConfig {
root: cfg.library_path.clone(),
dry_run: false,
@@ -81,16 +82,16 @@ async fn trigger_index(
Ok(HttpResponse::Accepted().json(serde_json::json!({ "task_id": task_id })))
}
async fn trigger_tag(
state: web::Data<AppState>,
) -> Result<HttpResponse, ApiError> {
async fn trigger_tag(state: web::Data<AppState>) -> Result<HttpResponse, ApiError> {
let task_id = state.tasks.register("tag");
let state = state.clone();
let tid = task_id.clone();
tokio::spawn(async move {
let cfg = state.config.read().await.clone();
state.tasks.update_progress(&tid, 0, 0, "Preparing tagger...");
state
.tasks
.update_progress(&tid, 0, 0, "Preparing tagger...");
let mb = match shanty_tag::MusicBrainzClient::new() {
Ok(c) => c,
Err(e) => {
@@ -113,16 +114,16 @@ async fn trigger_tag(
Ok(HttpResponse::Accepted().json(serde_json::json!({ "task_id": task_id })))
}
async fn trigger_organize(
state: web::Data<AppState>,
) -> Result<HttpResponse, ApiError> {
async fn trigger_organize(state: web::Data<AppState>) -> Result<HttpResponse, ApiError> {
let task_id = state.tasks.register("organize");
let state = state.clone();
let tid = task_id.clone();
tokio::spawn(async move {
let cfg = state.config.read().await.clone();
state.tasks.update_progress(&tid, 0, 0, "Organizing files...");
state
.tasks
.update_progress(&tid, 0, 0, "Organizing files...");
let org_config = shanty_org::OrgConfig {
target_dir: cfg.library_path.clone(),
format: cfg.organization_format.clone(),
@@ -149,9 +150,7 @@ async fn trigger_organize(
Ok(HttpResponse::Accepted().json(serde_json::json!({ "task_id": task_id })))
}
async fn trigger_pipeline(
state: web::Data<AppState>,
) -> Result<HttpResponse, ApiError> {
async fn trigger_pipeline(state: web::Data<AppState>) -> Result<HttpResponse, ApiError> {
let sync_id = state.tasks.register_pending("sync");
let download_id = state.tasks.register_pending("download");
let index_id = state.tasks.register_pending("index");
@@ -175,7 +174,9 @@ async fn trigger_pipeline(
// Step 1: Sync
state.tasks.start(&sync_id);
state.tasks.update_progress(&sync_id, 0, 0, "Syncing watchlist to download queue...");
state
.tasks
.update_progress(&sync_id, 0, 0, "Syncing watchlist to download queue...");
match shanty_dl::sync_wanted_to_queue(state.db.conn(), false).await {
Ok(stats) => state.tasks.complete(&sync_id, format!("{stats}")),
Err(e) => state.tasks.fail(&sync_id, e.to_string()),
@@ -184,9 +185,21 @@ async fn trigger_pipeline(
// Step 2: Download
state.tasks.start(&download_id);
let cookies = cfg.download.cookies_path.clone();
let format: shanty_dl::AudioFormat = cfg.download.format.parse().unwrap_or(shanty_dl::AudioFormat::Opus);
let source: shanty_dl::SearchSource = cfg.download.search_source.parse().unwrap_or(shanty_dl::SearchSource::YouTubeMusic);
let rate = if cookies.is_some() { cfg.download.rate_limit_auth } else { cfg.download.rate_limit };
let format: shanty_dl::AudioFormat = cfg
.download
.format
.parse()
.unwrap_or(shanty_dl::AudioFormat::Opus);
let source: shanty_dl::SearchSource = cfg
.download
.search_source
.parse()
.unwrap_or(shanty_dl::SearchSource::YouTubeMusic);
let rate = if cookies.is_some() {
cfg.download.rate_limit_auth
} else {
cfg.download.rate_limit
};
let backend = shanty_dl::YtDlpBackend::new(rate, source, cookies.clone());
let backend_config = shanty_dl::BackendConfig {
output_dir: cfg.download_path.clone(),
@@ -196,9 +209,19 @@ async fn trigger_pipeline(
let task_state = state.clone();
let progress_tid = download_id.clone();
let on_progress: shanty_dl::ProgressFn = Box::new(move |current, total, msg| {
task_state.tasks.update_progress(&progress_tid, current, total, msg);
task_state
.tasks
.update_progress(&progress_tid, current, total, msg);
});
match shanty_dl::run_queue_with_progress(state.db.conn(), &backend, &backend_config, false, Some(on_progress)).await {
match shanty_dl::run_queue_with_progress(
state.db.conn(),
&backend,
&backend_config,
false,
Some(on_progress),
)
.await
{
Ok(stats) => {
let _ = queries::cache::purge_prefix(state.db.conn(), "artist_totals:").await;
state.tasks.complete(&download_id, format!("{stats}"));
@@ -208,7 +231,9 @@ async fn trigger_pipeline(
// Step 3: Index
state.tasks.start(&index_id);
state.tasks.update_progress(&index_id, 0, 0, "Scanning library...");
state
.tasks
.update_progress(&index_id, 0, 0, "Scanning library...");
let scan_config = shanty_index::ScanConfig {
root: cfg.library_path.clone(),
dry_run: false,
@@ -221,7 +246,9 @@ async fn trigger_pipeline(
// Step 4: Tag
state.tasks.start(&tag_id);
state.tasks.update_progress(&tag_id, 0, 0, "Tagging tracks...");
state
.tasks
.update_progress(&tag_id, 0, 0, "Tagging tracks...");
match shanty_tag::MusicBrainzClient::new() {
Ok(mb) => {
let tag_config = shanty_tag::TagConfig {
@@ -239,7 +266,9 @@ async fn trigger_pipeline(
// Step 5: Organize
state.tasks.start(&organize_id);
state.tasks.update_progress(&organize_id, 0, 0, "Organizing files...");
state
.tasks
.update_progress(&organize_id, 0, 0, "Organizing files...");
let org_config = shanty_org::OrgConfig {
target_dir: cfg.library_path.clone(),
format: cfg.organization_format.clone(),
@@ -249,7 +278,8 @@ async fn trigger_pipeline(
match shanty_org::organize_from_db(state.db.conn(), &org_config).await {
Ok(stats) => {
let promoted = queries::wanted::promote_downloaded_to_owned(state.db.conn())
.await.unwrap_or(0);
.await
.unwrap_or(0);
let msg = if promoted > 0 {
format!("{stats}{promoted} items marked as owned")
} else {
@@ -262,9 +292,13 @@ async fn trigger_pipeline(
// Step 6: Enrich
state.tasks.start(&enrich_id);
state.tasks.update_progress(&enrich_id, 0, 0, "Refreshing artist data...");
state
.tasks
.update_progress(&enrich_id, 0, 0, "Refreshing artist data...");
match enrich_all_watched_artists(&state).await {
Ok(count) => state.tasks.complete(&enrich_id, format!("{count} artists refreshed")),
Ok(count) => state
.tasks
.complete(&enrich_id, format!("{count} artists refreshed")),
Err(e) => state.tasks.fail(&enrich_id, e.to_string()),
}
});
@@ -283,9 +317,7 @@ async fn get_task(
}
}
async fn list_watchlist(
state: web::Data<AppState>,
) -> Result<HttpResponse, ApiError> {
async fn list_watchlist(state: web::Data<AppState>) -> Result<HttpResponse, ApiError> {
let items = shanty_watch::list_items(state.db.conn(), None, None).await?;
Ok(HttpResponse::Ok().json(items))
}
@@ -299,9 +331,7 @@ async fn remove_watchlist(
Ok(HttpResponse::NoContent().finish())
}
async fn get_config(
state: web::Data<AppState>,
) -> Result<HttpResponse, ApiError> {
async fn get_config(state: web::Data<AppState>) -> Result<HttpResponse, ApiError> {
let config = state.config.read().await;
Ok(HttpResponse::Ok().json(&*config))
}
@@ -319,8 +349,9 @@ async fn save_config(
let new_config = body.into_inner().config;
// Persist to YAML
new_config.save(state.config_path.as_deref())
.map_err(|e| ApiError::Internal(e))?;
new_config
.save(state.config_path.as_deref())
.map_err(ApiError::Internal)?;
// Update in-memory config
let mut config = state.config.write().await;