BlueGit

ict rusty-teams

Files

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

banner_image.rs

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