| 1 | use serde::Deserialize; |
| 2 | |
| 3 | /// The image buckets of a listing that this crate manages. |
| 4 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 5 | pub enum ImageType { |
| 6 | PhoneScreenshots, |
| 7 | SevenInchScreenshots, |
| 8 | TenInchScreenshots, |
| 9 | FeatureGraphic, |
| 10 | Icon, |
| 11 | } |
| 12 | |
| 13 | impl ImageType { |
| 14 | /// The path segment Play uses for this bucket. |
| 15 | pub fn api_name(self) -> &'static str { |
| 16 | match self { |
| 17 | Self::PhoneScreenshots => "phoneScreenshots", |
| 18 | Self::SevenInchScreenshots => "sevenInchScreenshots", |
| 19 | Self::TenInchScreenshots => "tenInchScreenshots", |
| 20 | Self::FeatureGraphic => "featureGraphic", |
| 21 | Self::Icon => "icon", |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | pub fn is_screenshot(self) -> bool { |
| 26 | !matches!(self, Self::FeatureGraphic | Self::Icon) |
| 27 | } |
| 28 | |
| 29 | /// Check an image's pixel size against Play's rules for this bucket. |
| 30 | pub fn check_dimensions(self, width: u32, height: u32) -> std::result::Result<(), String> { |
| 31 | match self { |
| 32 | Self::FeatureGraphic if (width, height) != (1024, 500) => Err(format!( |
| 33 | "feature graphic must be exactly 1024x500, got {width}x{height}" |
| 34 | )), |
| 35 | Self::Icon if (width, height) != (512, 512) => Err(format!( |
| 36 | "icon must be exactly 512x512, got {width}x{height}" |
| 37 | )), |
| 38 | _ if self.is_screenshot() => { |
| 39 | let (short, long) = (width.min(height), width.max(height)); |
| 40 | if short < 320 || long > 3840 { |
| 41 | Err(format!( |
| 42 | "screenshot sides must be between 320 and 3840 pixels, got {width}x{height}" |
| 43 | )) |
| 44 | } else if long > short * 2 { |
| 45 | Err(format!( |
| 46 | "screenshot long side may be at most twice the short side, got {width}x{height}" |
| 47 | )) |
| 48 | } else { |
| 49 | Ok(()) |
| 50 | } |
| 51 | } |
| 52 | _ => Ok(()), |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | /// Width and height from a PNG's IHDR chunk, or `None` for anything else. |
| 58 | pub fn png_dimensions(bytes: &[u8]) -> Option<(u32, u32)> { |
| 59 | const SIGNATURE: [u8; 8] = [0x89, b'P', b'N', b'G', 0x0D, 0x0A, 0x1A, 0x0A]; |
| 60 | if bytes.len() < 24 || bytes[..8] != SIGNATURE || &bytes[12..16] != b"IHDR" { |
| 61 | return None; |
| 62 | } |
| 63 | let width = u32::from_be_bytes(bytes[16..20].try_into().ok()?); |
| 64 | let height = u32::from_be_bytes(bytes[20..24].try_into().ok()?); |
| 65 | Some((width, height)) |
| 66 | } |
| 67 | |
| 68 | /// One uploaded image as Play lists it. |
| 69 | #[derive(Debug, Clone, Deserialize, serde::Serialize, PartialEq, Eq)] |
| 70 | pub struct Image { |
| 71 | #[serde(default)] |
| 72 | pub id: String, |
| 73 | #[serde(default)] |
| 74 | pub url: Option<String>, |
| 75 | #[serde(default)] |
| 76 | pub sha1: Option<String>, |
| 77 | #[serde(default)] |
| 78 | pub sha256: Option<String>, |
| 79 | } |
| 80 | |
| 81 | #[cfg(test)] |
| 82 | mod tests { |
| 83 | use super::*; |
| 84 | |
| 85 | fn png_header(width: u32, height: u32) -> Vec<u8> { |
| 86 | let mut bytes = vec![0x89, b'P', b'N', b'G', 0x0D, 0x0A, 0x1A, 0x0A, 0, 0, 0, 13]; |
| 87 | bytes.extend_from_slice(b"IHDR"); |
| 88 | bytes.extend_from_slice(&width.to_be_bytes()); |
| 89 | bytes.extend_from_slice(&height.to_be_bytes()); |
| 90 | bytes |
| 91 | } |
| 92 | |
| 93 | #[test] |
| 94 | fn png_dimensions_read_the_ihdr_chunk() { |
| 95 | assert_eq!(png_dimensions(&png_header(1080, 2400)), Some((1080, 2400))); |
| 96 | assert_eq!(png_dimensions(b"not a png at all, but long enough"), None); |
| 97 | } |
| 98 | |
| 99 | #[test] |
| 100 | fn api_names_match_the_play_path_segments() { |
| 101 | assert_eq!(ImageType::PhoneScreenshots.api_name(), "phoneScreenshots"); |
| 102 | assert_eq!( |
| 103 | ImageType::SevenInchScreenshots.api_name(), |
| 104 | "sevenInchScreenshots" |
| 105 | ); |
| 106 | assert_eq!( |
| 107 | ImageType::TenInchScreenshots.api_name(), |
| 108 | "tenInchScreenshots" |
| 109 | ); |
| 110 | assert_eq!(ImageType::FeatureGraphic.api_name(), "featureGraphic"); |
| 111 | assert_eq!(ImageType::Icon.api_name(), "icon"); |
| 112 | } |
| 113 | |
| 114 | #[test] |
| 115 | fn feature_graphic_and_icon_need_exact_sizes() { |
| 116 | assert!(ImageType::FeatureGraphic |
| 117 | .check_dimensions(1024, 500) |
| 118 | .is_ok()); |
| 119 | assert!(ImageType::FeatureGraphic |
| 120 | .check_dimensions(1024, 501) |
| 121 | .is_err()); |
| 122 | assert!(ImageType::Icon.check_dimensions(512, 512).is_ok()); |
| 123 | assert!(ImageType::Icon.check_dimensions(511, 512).is_err()); |
| 124 | } |
| 125 | |
| 126 | #[test] |
| 127 | fn screenshots_are_bounded_in_size_and_aspect() { |
| 128 | let phone = ImageType::PhoneScreenshots; |
| 129 | assert!(phone.check_dimensions(1080, 2160).is_ok()); |
| 130 | assert!(phone.check_dimensions(1080, 2161).is_err()); |
| 131 | assert!(phone.check_dimensions(319, 600).is_err()); |
| 132 | assert!(phone.check_dimensions(1000, 3841).is_err()); |
| 133 | } |
| 134 | } |