Two-tier fallback for articles without a featured image. Tier 1: images whose filename starts with [`FALLBACK_COVER_PREFIX`]. Tier 2: `featured_image` values from existing articles.
(
images_table: &Table,
articles_table: &Table,
)
| 329 | /// Tier 1: images whose filename starts with [`FALLBACK_COVER_PREFIX`]. |
| 330 | /// Tier 2: `featured_image` values from existing articles. |
| 331 | pub async fn query_fallback_cover( |
| 332 | images_table: &Table, |
| 333 | articles_table: &Table, |
| 334 | ) -> Result<Option<String>> { |
| 335 | // Tier 1: dedicated fallback covers |
| 336 | let filter = format!("filename LIKE '{FALLBACK_COVER_PREFIX}%'"); |
| 337 | let batches = images_table |
| 338 | .query() |
| 339 | .only_if(filter) |
| 340 | .select(Select::columns(&["id"])) |
| 341 | .execute() |
| 342 | .await? |
| 343 | .try_collect::<Vec<_>>() |
| 344 | .await?; |
| 345 | |
| 346 | let mut candidates: Vec<String> = Vec::new(); |
| 347 | for batch in &batches { |
| 348 | if let Some(arr) = batch |
| 349 | .column_by_name("id") |
| 350 | .and_then(|col| col.as_any().downcast_ref::<StringArray>()) |
| 351 | { |
| 352 | for i in 0..arr.len() { |
| 353 | if !arr.is_null(i) { |
| 354 | candidates.push(format!("images/{}", arr.value(i))); |
| 355 | } |
| 356 | } |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | if !candidates.is_empty() { |
| 361 | let pick = pick_random_cover(&candidates); |
| 362 | tracing::info!("Fallback cover (dedicated): {pick}"); |
| 363 | return Ok(Some(pick)); |
| 364 | } |
| 365 | |
| 366 | // Tier 2: reuse existing article covers |
| 367 | let batches = articles_table |
| 368 | .query() |
| 369 | .only_if("featured_image IS NOT NULL") |
| 370 | .select(Select::columns(&["featured_image"])) |
| 371 | .execute() |
| 372 | .await? |
| 373 | .try_collect::<Vec<_>>() |
| 374 | .await?; |
| 375 | |
| 376 | let mut candidates: Vec<String> = Vec::new(); |
| 377 | for batch in &batches { |
| 378 | if let Some(arr) = batch |
| 379 | .column_by_name("featured_image") |
| 380 | .and_then(|col| col.as_any().downcast_ref::<StringArray>()) |
| 381 | { |
| 382 | for i in 0..arr.len() { |
| 383 | if !arr.is_null(i) { |
| 384 | let val = arr.value(i); |
| 385 | if !val.is_empty() { |
| 386 | candidates.push(val.to_string()); |
| 387 | } |
| 388 | } |