Debian Codegraph @

podcasts-gtk/src/thumbnail_generator.rs

raw symbols SCIP
1 use anyhow::{Result, anyhow};
2 use image::imageops::FilterType;
3 use std::collections::HashMap;
4 use std::fmt::Display;
5 use std::path::Path;
6
7 use crate::download_covers::determin_cover_path;
8 use podcasts_data::ShowCoverModel;
9 use podcasts_data::utils::get_cover_dir_path;
10 use std::sync::Arc;
11
12 // we only generate a fixed amount of thumbnails
13 // This enum is to avoid accidentally passing a thumb-size we didn't generate
14 #[derive(Copy, Clone, Hash, PartialEq, Eq)]
15 pub enum ThumbSize {
16 Thumb64,
17 Thumb128,
18 Thumb256,
19 Thumb512,
20 }
21 pub use self::ThumbSize::*;
22
23 impl ThumbSize {
24 fn pixels(&self) -> u32 {
25 match &self {
26 Thumb64 => 64,
27 Thumb128 => 128,
28 Thumb256 => 256,
29 Thumb512 => 512,
30 }
31 }
32 pub fn hidpi(self, scale: i32) -> Option<ThumbSize> {
33 // meh code
34 if scale >= 5 {
35 match self {
36 Thumb64 => Some(Thumb512),
37 Thumb128 => None,
38 Thumb256 => None,
39 Thumb512 => None,
40 }
41 } else if scale >= 3 {
42 match self {
43 Thumb64 => Some(Thumb256),
44 Thumb128 => Some(Thumb512),
45 Thumb256 => None,
46 Thumb512 => None,
47 }
48 } else if scale >= 2 {
49 match self {
50 Thumb64 => Some(Thumb128),
51 Thumb128 => Some(Thumb256),
52 Thumb256 => Some(Thumb512),
53 Thumb512 => None,
54 }
55 } else {
56 Some(self)
57 }
58 }
59 }
60
61 impl Display for ThumbSize {
62 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
63 write!(f, "{}", self.pixels())
64 }
65 }
66
67 pub async fn generate(
68 pd: &ShowCoverModel,
69 path: &Path,
70 ) -> Result<HashMap<ThumbSize, gtk::gdk::Texture>> {
71 let sizes: [ThumbSize; 4] = [Thumb64, Thumb128, Thumb256, Thumb512];
72 // All thumbs must generate, we rely on them existing if the main image exists.
73
74 let path = path.to_path_buf();
75 let image_full_size = crate::RUNTIME
76 .spawn_blocking(move || {
77 anyhow::Ok(Arc::new(
78 image::ImageReader::open(path)?
79 .with_guessed_format()?
80 .decode()?,
81 ))
82 })
83 .await??;
84
85 let dir = get_cover_dir_path(pd.title());
86 tokio::fs::create_dir_all(dir).await?;
87
88 let handles: Vec<_> = sizes
89 .into_iter()
90 .map(|size| {
91 let pixels = size.pixels();
92 let thumb_path = determin_cover_path(pd, Some(size));
93 let image_full_size = image_full_size.clone();
94
95 crate::RUNTIME.spawn(async move {
96 let tmp_path = thumb_path.with_extension(".part");
97 let tmp_path2 = tmp_path.clone();
98 // save and read gdk texture
99 let texture = crate::RUNTIME
100 .spawn_blocking(move || {
101 let image = image_full_size.resize(pixels, pixels, FilterType::Lanczos3);
102 image.save_with_format(&tmp_path2, image::ImageFormat::Png)?;
103 gtk::gdk::Texture::from_filename(&tmp_path2)
104 .map_err(|_| anyhow!("failed to read gtk texture"))
105 })
106 .await??;
107 tokio::fs::rename(&tmp_path, &thumb_path).await?;
108 Ok((size, texture))
109 })
110 })
111 .collect();
112 let result: Result<HashMap<ThumbSize, gtk::gdk::Texture>> =
113 futures_util::future::join_all(handles)
114 .await
115 .into_iter()
116 .map(|r| r.unwrap_or(Err(anyhow!("Failed to write cover thumbnail."))))
117 .collect();
118 result
119 }