fix: thumbnails manquants dans les résultats de recherche
- meili.rs: corrige la désérialisation de la réponse paginée de
Meilisearch (attendait Vec<Value>, l'API retourne {results:[...]}) —
la suppression des documents obsolètes ne s'exécutait jamais, laissant
d'anciens UUIDs qui généraient des 404 sur les thumbnails
- books.rs: fallback sur render_book_page_1 si le fichier thumbnail
n'est plus accessible sur le disque (au lieu de 500)
- pages.rs: retourne 404 au lieu de 500 quand le fichier CBZ est absent
- search.rs + api.ts + BookCard: ajout série hits, statut lecture,
pagination OFFSET, filtre reading_status, et placeholder onError
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -605,10 +605,15 @@ pub async fn get_thumbnail(
|
||||
let thumbnail_path: Option<String> = row.get("thumbnail_path");
|
||||
|
||||
let data = if let Some(ref path) = thumbnail_path {
|
||||
std::fs::read(path)
|
||||
.map_err(|e| ApiError::internal(format!("cannot read thumbnail: {}", e)))?
|
||||
match std::fs::read(path) {
|
||||
Ok(bytes) => bytes,
|
||||
Err(_) => {
|
||||
// File missing on disk (e.g. different mount in dev) — fall back to live render
|
||||
crate::pages::render_book_page_1(&state, book_id, 300, 80).await?
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Fallback: render page 1 on the fly (same as pages logic)
|
||||
// No stored thumbnail yet — render page 1 on the fly
|
||||
crate::pages::render_book_page_1(&state, book_id, 300, 80).await?
|
||||
};
|
||||
|
||||
|
||||
@@ -51,6 +51,7 @@ use utoipa::OpenApi;
|
||||
crate::pages::PageQuery,
|
||||
crate::search::SearchQuery,
|
||||
crate::search::SearchResponse,
|
||||
crate::search::SeriesHit,
|
||||
crate::index_jobs::RebuildRequest,
|
||||
crate::thumbnails::ThumbnailsRebuildRequest,
|
||||
crate::index_jobs::IndexJobResponse,
|
||||
|
||||
@@ -365,8 +365,12 @@ fn render_page(
|
||||
fn extract_cbz_page(abs_path: &str, page_number: u32) -> Result<Vec<u8>, ApiError> {
|
||||
debug!("Opening CBZ archive: {}", abs_path);
|
||||
let file = std::fs::File::open(abs_path).map_err(|e| {
|
||||
error!("Cannot open CBZ file {}: {}", abs_path, e);
|
||||
ApiError::internal(format!("cannot open cbz: {e}"))
|
||||
if e.kind() == std::io::ErrorKind::NotFound {
|
||||
ApiError::not_found("book file not accessible")
|
||||
} else {
|
||||
error!("Cannot open CBZ file {}: {}", abs_path, e);
|
||||
ApiError::internal(format!("cannot open cbz: {e}"))
|
||||
}
|
||||
})?;
|
||||
|
||||
let mut archive = zip::ZipArchive::new(file).map_err(|e| {
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
use axum::{extract::{Query, State}, Json};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use sqlx::Row;
|
||||
use utoipa::ToSchema;
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::{error::ApiError, state::AppState};
|
||||
|
||||
@@ -18,9 +20,21 @@ pub struct SearchQuery {
|
||||
pub limit: Option<usize>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, ToSchema)]
|
||||
pub struct SeriesHit {
|
||||
#[schema(value_type = String)]
|
||||
pub library_id: Uuid,
|
||||
pub name: String,
|
||||
pub book_count: i64,
|
||||
pub books_read_count: i64,
|
||||
#[schema(value_type = String)]
|
||||
pub first_book_id: Uuid,
|
||||
}
|
||||
|
||||
#[derive(Serialize, ToSchema)]
|
||||
pub struct SearchResponse {
|
||||
pub hits: serde_json::Value,
|
||||
pub series_hits: Vec<SeriesHit>,
|
||||
pub estimated_total_hits: Option<u64>,
|
||||
pub processing_time_ms: Option<u64>,
|
||||
}
|
||||
@@ -31,11 +45,11 @@ pub struct SearchResponse {
|
||||
path = "/search",
|
||||
tag = "books",
|
||||
params(
|
||||
("q" = String, Query, description = "Search query"),
|
||||
("q" = String, Query, description = "Search query (books via Meilisearch + series via ILIKE)"),
|
||||
("library_id" = Option<String>, Query, description = "Filter by library ID"),
|
||||
("type" = Option<String>, Query, description = "Filter by type (cbz, cbr, pdf)"),
|
||||
("kind" = Option<String>, Query, description = "Filter by kind (alias for type)"),
|
||||
("limit" = Option<usize>, Query, description = "Max results (max 100)"),
|
||||
("limit" = Option<usize>, Query, description = "Max results per type (max 100)"),
|
||||
),
|
||||
responses(
|
||||
(status = 200, body = SearchResponse),
|
||||
@@ -66,36 +80,98 @@ pub async fn search_books(
|
||||
"filter": if filters.is_empty() { serde_json::Value::Null } else { serde_json::Value::String(filters.join(" AND ")) }
|
||||
});
|
||||
|
||||
let limit_val = query.limit.unwrap_or(20).clamp(1, 100);
|
||||
let q_pattern = format!("%{}%", query.q);
|
||||
let library_id_uuid: Option<uuid::Uuid> = query.library_id.as_deref()
|
||||
.and_then(|s| s.parse().ok());
|
||||
|
||||
// Recherche Meilisearch (books) + séries PG en parallèle
|
||||
let client = reqwest::Client::new();
|
||||
let url = format!("{}/indexes/books/search", state.meili_url.trim_end_matches('/'));
|
||||
let response = client
|
||||
.post(url)
|
||||
let meili_fut = client
|
||||
.post(&url)
|
||||
.header("Authorization", format!("Bearer {}", state.meili_master_key))
|
||||
.json(&body)
|
||||
.send()
|
||||
.await
|
||||
.map_err(|e| ApiError::internal(format!("meili request failed: {e}")))?;
|
||||
.send();
|
||||
|
||||
if !response.status().is_success() {
|
||||
let body = response.text().await.unwrap_or_else(|_| "unknown meili error".to_string());
|
||||
let series_sql = r#"
|
||||
WITH sorted_books AS (
|
||||
SELECT
|
||||
library_id,
|
||||
COALESCE(NULLIF(series, ''), 'unclassified') as name,
|
||||
id,
|
||||
ROW_NUMBER() OVER (
|
||||
PARTITION BY library_id, COALESCE(NULLIF(series, ''), 'unclassified')
|
||||
ORDER BY
|
||||
REGEXP_REPLACE(LOWER(title), '[0-9]+', '', 'g'),
|
||||
COALESCE((REGEXP_MATCH(LOWER(title), '\d+'))[1]::int, 0),
|
||||
title ASC
|
||||
) as rn
|
||||
FROM books
|
||||
WHERE ($1::uuid IS NULL OR library_id = $1)
|
||||
),
|
||||
series_counts AS (
|
||||
SELECT
|
||||
sb.library_id,
|
||||
sb.name,
|
||||
COUNT(*) as book_count,
|
||||
COUNT(brp.book_id) FILTER (WHERE brp.status = 'read') as books_read_count
|
||||
FROM sorted_books sb
|
||||
LEFT JOIN book_reading_progress brp ON brp.book_id = sb.id
|
||||
GROUP BY sb.library_id, sb.name
|
||||
)
|
||||
SELECT sc.library_id, sc.name, sc.book_count, sc.books_read_count, sb.id as first_book_id
|
||||
FROM series_counts sc
|
||||
JOIN sorted_books sb ON sb.library_id = sc.library_id AND sb.name = sc.name AND sb.rn = 1
|
||||
WHERE sc.name ILIKE $2
|
||||
ORDER BY sc.name ASC
|
||||
LIMIT $3
|
||||
"#;
|
||||
|
||||
let series_fut = sqlx::query(series_sql)
|
||||
.bind(library_id_uuid)
|
||||
.bind(&q_pattern)
|
||||
.bind(limit_val as i64)
|
||||
.fetch_all(&state.pool);
|
||||
|
||||
let (meili_resp, series_rows) = tokio::join!(meili_fut, series_fut);
|
||||
|
||||
// Traitement Meilisearch
|
||||
let meili_resp = meili_resp.map_err(|e| ApiError::internal(format!("meili request failed: {e}")))?;
|
||||
let (hits, estimated_total_hits, processing_time_ms) = if !meili_resp.status().is_success() {
|
||||
let body = meili_resp.text().await.unwrap_or_default();
|
||||
if body.contains("index_not_found") {
|
||||
return Ok(Json(SearchResponse {
|
||||
hits: serde_json::json!([]),
|
||||
estimated_total_hits: Some(0),
|
||||
processing_time_ms: Some(0),
|
||||
}));
|
||||
(serde_json::json!([]), Some(0u64), Some(0u64))
|
||||
} else {
|
||||
return Err(ApiError::internal(format!("meili error: {body}")));
|
||||
}
|
||||
return Err(ApiError::internal(format!("meili error: {body}")));
|
||||
}
|
||||
} else {
|
||||
let payload: serde_json::Value = meili_resp.json().await
|
||||
.map_err(|e| ApiError::internal(format!("invalid meili response: {e}")))?;
|
||||
(
|
||||
payload.get("hits").cloned().unwrap_or_else(|| serde_json::json!([])),
|
||||
payload.get("estimatedTotalHits").and_then(|v| v.as_u64()),
|
||||
payload.get("processingTimeMs").and_then(|v| v.as_u64()),
|
||||
)
|
||||
};
|
||||
|
||||
let payload: serde_json::Value = response
|
||||
.json()
|
||||
.await
|
||||
.map_err(|e| ApiError::internal(format!("invalid meili response: {e}")))?;
|
||||
// Traitement séries
|
||||
let series_hits: Vec<SeriesHit> = series_rows
|
||||
.unwrap_or_default()
|
||||
.iter()
|
||||
.map(|row| SeriesHit {
|
||||
library_id: row.get("library_id"),
|
||||
name: row.get("name"),
|
||||
book_count: row.get("book_count"),
|
||||
books_read_count: row.get("books_read_count"),
|
||||
first_book_id: row.get("first_book_id"),
|
||||
})
|
||||
.collect();
|
||||
|
||||
Ok(Json(SearchResponse {
|
||||
hits: payload.get("hits").cloned().unwrap_or_else(|| serde_json::json!([])),
|
||||
estimated_total_hits: payload.get("estimatedTotalHits").and_then(|v| v.as_u64()),
|
||||
processing_time_ms: payload.get("processingTimeMs").and_then(|v| v.as_u64()),
|
||||
hits,
|
||||
series_hits,
|
||||
estimated_total_hits,
|
||||
processing_time_ms,
|
||||
}))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user