BlueGit

ict rusty-teams

Files

rusty-teams / crates / app-state / src / banner_image.rs

banner_image.rs

banner_image.rs 3.7 KiB Download Raw
1use std::path::{Path, PathBuf};
2use std::time::{Duration, SystemTime};
3
4use image::imageops::FilterType;
5use uuid::Uuid;
6
7/// Teams web crops announcement backgrounds to this size.
8pub const BANNER_IMAGE_WIDTH: u32 = 1317;
9pub const BANNER_IMAGE_HEIGHT: u32 = 366;
10const BANNER_DIRECTORY: &str = "banners";
11const ABANDONED_AFTER: Duration = Duration::from_secs(30 * 24 * 60 * 60);
12
13/// The centered box with the banner's aspect ratio: `(x, y, width, height)`.
14pub fn crop_box(width: u32, height: u32) -> (u32, u32, u32, u32) {
15 let wanted_height = (u64::from(width) * u64::from(BANNER_IMAGE_HEIGHT) / u64::from(BANNER_IMAGE_WIDTH)) as u32;
16 if wanted_height <= height {
17 (0, (height - wanted_height) / 2, width, wanted_height.max(1))
18 } else {
19 let wanted_width = (u64::from(height) * u64::from(BANNER_IMAGE_WIDTH) / u64::from(BANNER_IMAGE_HEIGHT)) as u32;
20 ((width - wanted_width) / 2, 0, wanted_width.max(1), height)
21 }
22}
23
24/// Crops and scales `source` to the banner size and saves it as a PNG under the app data folder.
25pub fn prepare_banner_image(source: &Path) -> Result<PathBuf, String> {
26 let picked = image::open(source).map_err(|error| error.to_string())?;
27 let (x, y, width, height) = crop_box(picked.width(), picked.height());
28 let banner = picked.crop_imm(x, y, width, height).resize_exact(
29 BANNER_IMAGE_WIDTH,
30 BANNER_IMAGE_HEIGHT,
31 FilterType::Triangle,
32 );
33 let directory = banner_directory().ok_or("no data folder")?;
34 std::fs::create_dir_all(&directory).map_err(|error| error.to_string())?;
35 remove_abandoned(&directory, SystemTime::now());
36 let target = directory.join(format!("{}.png", Uuid::new_v4().simple()));
37 banner
38 .to_rgb8()
39 .save_with_format(&target, image::ImageFormat::Png)
40 .map_err(|error| error.to_string())?;
41 Ok(target)
42}
43
44fn banner_directory() -> Option<PathBuf> {
45 directories::ProjectDirs::from("", "", store::DATA_DIR_NAME)
46 .map(|directories| directories.data_local_dir().join(BANNER_DIRECTORY))
47}
48
49/// Removes a prepared banner once it is uploaded; files outside the banner folder stay.
50pub fn discard_banner_image(path: &Path) {
51 if banner_directory().is_some_and(|directory| path.starts_with(directory)) {
52 let _ = std::fs::remove_file(path);
53 }
54}
55
56/// Banners of drafts that were never sent.
57fn remove_abandoned(directory: &Path, now: SystemTime) {
58 let Ok(entries) = std::fs::read_dir(directory) else {
59 return;
60 };
61 for entry in entries.flatten() {
62 let old = entry
63 .metadata()
64 .and_then(|metadata| metadata.modified())
65 .ok()
66 .and_then(|modified| now.duration_since(modified).ok())
67 .is_some_and(|age| age > ABANDONED_AFTER);
68 if old {
69 let _ = std::fs::remove_file(entry.path());
70 }
71 }
72}
73
74#[cfg(test)]
75mod tests {
76 use std::time::{Duration, SystemTime};
77
78 use super::{ABANDONED_AFTER, crop_box, remove_abandoned};
79
80 #[test]
81 fn only_banners_older_than_a_month_are_removed() {
82 let directory = tempfile::tempdir().unwrap();
83 let banner = directory.path().join("a.png");
84 std::fs::write(&banner, b"x").unwrap();
85 remove_abandoned(directory.path(), SystemTime::now());
86 assert!(banner.exists());
87 remove_abandoned(
88 directory.path(),
89 SystemTime::now() + ABANDONED_AFTER + Duration::from_secs(60),
90 );
91 assert!(!banner.exists());
92 }
93
94 #[test]
95 fn crop_keeps_the_middle_band_of_a_tall_or_wide_picture() {
96 assert_eq!(crop_box(1317, 1000), (0, 317, 1317, 366));
97 assert_eq!(crop_box(4000, 366), (1341, 0, 1317, 366));
98 assert_eq!(crop_box(1317, 366), (0, 0, 1317, 366));
99 }
100}