use std::{
path::{Path, PathBuf},
process::Command,
};
use tracing::{info, warn};
use crate::{
config::ThumbnailConfig,
error::{PinakesError, Result},
media_type::{BuiltinMediaType, MediaCategory, MediaType},
model::MediaId,
};
/// RAII guard that removes a file on drop.
struct TempFileGuard(PathBuf);
impl TempFileGuard {
const fn new(path: PathBuf) -> Self {
Self(path)
}
fn path(&self) -> &Path {
&self.0
}
}
impl Drop for TempFileGuard {
fn drop(&mut self) {
if self.0.exists()
&& let Err(e) = std::fs::remove_file(&self.0) {
warn!("failed to clean up temp file {}: {e}", self.0.display());
}
}
}
/// Generate a thumbnail for a media file and return the path to the thumbnail.
///
/// Supports images (via `image` crate), videos (via ffmpeg), PDFs (via
/// pdftoppm), and EPUBs (via cover image extraction).
///
/// # Errors
///
/// Returns [`PinakesError`] if thumbnail generation fails.
pub fn generate_thumbnail(
media_id: MediaId,
source_path: &Path,
media_type: &MediaType,
thumbnail_dir: &Path,
) -> Result