BlueGit

ict rusty-teams

Files

rusty-teams / crates / app / src / remote_image.rs

remote_image.rs

remote_image.rs 4.0 KiB Download Raw
1use gpui_kit::{ImageSource, SharedString};
2use teams_core::{Gif, ImageRef};
3
4use crate::stickers::Sticker;
5
6pub const MAX_HEIGHT: u32 = 250;
7const STICKER_SIZE: u32 = 250;
8const GIF_ITEMTYPE: &str = "http://schema.skype.com/Giphy";
9const STICKER_ITEMTYPE: &str = "http://schema.skype.com/Sticker";
10
11#[derive(Debug, Clone, Copy, PartialEq, Eq)]
12pub enum RemoteKind {
13 Gif,
14 Sticker,
15}
16
17/// A GIF or sticker the message points at by URL; nothing is uploaded.
18#[derive(Debug, Clone, PartialEq, Eq)]
19pub struct RemoteImage {
20 pub kind: RemoteKind,
21 pub url: String,
22 pub title: String,
23 pub width: u32,
24 pub height: u32,
25}
26
27impl RemoteImage {
28 pub fn gif(gif: &Gif) -> Self {
29 let (width, height) = capped(gif.width, gif.height);
30 RemoteImage {
31 kind: RemoteKind::Gif,
32 url: gif.url.clone(),
33 title: gif.title.clone(),
34 width,
35 height,
36 }
37 }
38
39 pub fn sticker(sticker: &Sticker) -> Self {
40 RemoteImage {
41 kind: RemoteKind::Sticker,
42 url: sticker.url(),
43 title: sticker.name.to_owned(),
44 width: STICKER_SIZE,
45 height: STICKER_SIZE,
46 }
47 }
48
49 pub fn html(&self) -> String {
50 let itemtype = match self.kind {
51 RemoteKind::Gif => GIF_ITEMTYPE,
52 RemoteKind::Sticker => STICKER_ITEMTYPE,
53 };
54 format!(
55 "<img src=\"{}\" width=\"{}\" height=\"{}\" alt=\"{}\" itemtype=\"{itemtype}\">",
56 teams_core::escape_html(&self.url),
57 self.width,
58 self.height,
59 teams_core::escape_html(&self.title),
60 )
61 }
62
63 pub fn image_ref(&self) -> ImageRef {
64 ImageRef {
65 id: self.url.clone(),
66 url: self.url.clone(),
67 width: Some(self.width),
68 height: Some(self.height),
69 }
70 }
71}
72
73/// Height capped at `MAX_HEIGHT`, aspect ratio kept.
74pub fn capped(width: u32, height: u32) -> (u32, u32) {
75 if height <= MAX_HEIGHT || height == 0 {
76 return (width, height);
77 }
78 let scaled =
79 (u64::from(width) * u64::from(MAX_HEIGHT) + u64::from(height) / 2) / u64::from(height);
80 ((scaled as u32).max(1), MAX_HEIGHT)
81}
82
83/// Web URLs load through the app's HTTP client, anything else is a local file (demo mode).
84pub fn image_source(location: &str) -> ImageSource {
85 if location.starts_with("https://") {
86 ImageSource::from(SharedString::from(location.to_owned()))
87 } else {
88 ImageSource::from(std::path::PathBuf::from(location))
89 }
90}
91
92#[cfg(test)]
93mod tests {
94 use super::*;
95
96 fn gif(width: u32, height: u32) -> Gif {
97 Gif {
98 url: "https://media0.giphy.com/media/a/giphy.gif".into(),
99 preview_url: "https://media0.giphy.com/media/a/200w.gif".into(),
100 title: "Say \"hi\" & <wave>".into(),
101 width,
102 height,
103 preview_width: 200,
104 preview_height: 200,
105 }
106 }
107
108 #[test]
109 fn tall_gifs_are_capped_keeping_the_aspect_ratio() {
110 let image = RemoteImage::gif(&gif(500, 1000));
111 assert_eq!((image.width, image.height), (125, 250));
112 let small = RemoteImage::gif(&gif(200, 100));
113 assert_eq!((small.width, small.height), (200, 100));
114 }
115
116 #[test]
117 fn gif_html_escapes_the_title_and_names_the_giphy_itemtype() {
118 let html = RemoteImage::gif(&gif(358, 360)).html();
119 assert_eq!(
120 html,
121 "<img src=\"https://media0.giphy.com/media/a/giphy.gif\" width=\"249\" height=\"250\" alt=\"Say &quot;hi&quot; &amp; &lt;wave&gt;\" itemtype=\"http://schema.skype.com/Giphy\">"
122 );
123 }
124
125 #[test]
126 fn stickers_are_square_250() {
127 let sticker = crate::stickers::popular()[0];
128 let html = RemoteImage::sticker(sticker).html();
129 assert!(html.contains("width=\"250\" height=\"250\""));
130 assert!(html.ends_with("itemtype=\"http://schema.skype.com/Sticker\">"));
131 assert!(html.contains("clippy-250x250"));
132 }
133}