| 1 | use std::fs; |
| 2 | use std::io; |
| 3 | use std::path::{Path, PathBuf}; |
| 4 | |
| 5 | use gpui_kit::App; |
| 6 | |
| 7 | use super::platform; |
| 8 | |
| 9 | pub const TEAMS_SOUND_BASE_URL: &str = |
| 10 | "https://statics.teams.cdn.office.net/evergreen-assets/audio/"; |
| 11 | const SOUND_DIRECTORY: &str = "sounds"; |
| 12 | const PARTIAL_SUFFIX: &str = ".partial"; |
| 13 | |
| 14 | pub fn cache_directory() -> Option<PathBuf> { |
| 15 | directories::ProjectDirs::from("", "", store::DATA_DIR_NAME) |
| 16 | .map(|directories| directories.cache_dir().join(SOUND_DIRECTORY)) |
| 17 | } |
| 18 | |
| 19 | pub fn is_mp3(bytes: &[u8]) -> bool { |
| 20 | bytes.starts_with(b"ID3") || matches!(bytes, [0xFF, second, ..] if second & 0xE0 == 0xE0) |
| 21 | } |
| 22 | |
| 23 | pub fn read_cached(directory: &Path, file_name: &str) -> Option<Vec<u8>> { |
| 24 | fs::read(directory.join(file_name)) |
| 25 | .ok() |
| 26 | .filter(|bytes| is_mp3(bytes)) |
| 27 | } |
| 28 | |
| 29 | pub fn write_cached(directory: &Path, file_name: &str, bytes: &[u8]) -> io::Result<()> { |
| 30 | fs::create_dir_all(directory)?; |
| 31 | let partial = directory.join(format!("{file_name}{PARTIAL_SUFFIX}")); |
| 32 | fs::write(&partial, bytes)?; |
| 33 | fs::rename(&partial, directory.join(file_name)) |
| 34 | } |
| 35 | |
| 36 | fn load(file_name: &'static str, cx: &App) -> gpui_kit::Task<Option<Vec<u8>>> { |
| 37 | let client = cx.http_client(); |
| 38 | cx.background_executor().spawn(async move { |
| 39 | let directory = cache_directory()?; |
| 40 | if let Some(bytes) = read_cached(&directory, file_name) { |
| 41 | return Some(bytes); |
| 42 | } |
| 43 | use futures_util::AsyncReadExt as _; |
| 44 | let url = format!("{TEAMS_SOUND_BASE_URL}{file_name}"); |
| 45 | let mut response = client.get(&url, ().into(), true).await.ok()?; |
| 46 | let mut body = Vec::new(); |
| 47 | response.body_mut().read_to_end(&mut body).await.ok()?; |
| 48 | if !response.status().is_success() || !is_mp3(&body) { |
| 49 | log::warn!( |
| 50 | "teams sound {file_name} not loaded: status {}", |
| 51 | response.status() |
| 52 | ); |
| 53 | return None; |
| 54 | } |
| 55 | if let Err(error) = write_cached(&directory, file_name, &body) { |
| 56 | log::warn!("teams sound {file_name} not cached: {error}"); |
| 57 | } |
| 58 | Some(body) |
| 59 | }) |
| 60 | } |
| 61 | |
| 62 | pub fn prefetch(file_names: &[&'static str], cx: &App) { |
| 63 | for file_name in file_names { |
| 64 | load(file_name, cx).detach(); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | pub fn play(file_name: &'static str, cx: &App) { |
| 69 | let loading = load(file_name, cx); |
| 70 | cx.background_executor() |
| 71 | .spawn(async move { |
| 72 | if let Some(bytes) = loading.await { |
| 73 | platform::play_mp3(bytes); |
| 74 | } |
| 75 | }) |
| 76 | .detach(); |
| 77 | } |
| 78 | |
| 79 | #[cfg(test)] |
| 80 | mod tests { |
| 81 | use super::*; |
| 82 | |
| 83 | #[test] |
| 84 | fn mp3_files_start_with_a_tag_or_a_frame_sync() { |
| 85 | assert!(is_mp3(b"ID3\x04\x00")); |
| 86 | assert!(is_mp3(&[0xFF, 0xFB, 0x90])); |
| 87 | assert!(!is_mp3(b"<?xml version")); |
| 88 | assert!(!is_mp3(&[])); |
| 89 | } |
| 90 | |
| 91 | #[test] |
| 92 | fn a_cached_sound_round_trips_and_junk_is_ignored() { |
| 93 | let directory = tempfile::tempdir().unwrap(); |
| 94 | let sounds = directory.path().join("sounds"); |
| 95 | write_cached(&sounds, "end.mp3", b"ID3 tone").unwrap(); |
| 96 | assert_eq!( |
| 97 | read_cached(&sounds, "end.mp3").as_deref(), |
| 98 | Some(&b"ID3 tone"[..]) |
| 99 | ); |
| 100 | assert!(!sounds.join("end.mp3.partial").exists()); |
| 101 | write_cached(&sounds, "junk.mp3", b"<Error/>").unwrap(); |
| 102 | assert_eq!(read_cached(&sounds, "junk.mp3"), None); |
| 103 | assert_eq!(read_cached(&sounds, "missing.mp3"), None); |
| 104 | } |
| 105 | } |