|
| 1 | +use std::time::Duration; |
| 2 | + |
| 3 | +use futures_util::StreamExt; |
| 4 | +use reqwest::{ |
| 5 | + header::{ACCEPT, CONTENT_TYPE, USER_AGENT}, |
| 6 | + redirect::Policy, |
| 7 | +}; |
| 8 | +use url::Url; |
| 9 | + |
| 10 | +const MAX_TITLE_FETCH_BYTES: usize = 256 * 1024; |
| 11 | +const TITLE_FETCH_TIMEOUT: Duration = Duration::from_secs(4); |
| 12 | + |
| 13 | +#[tauri::command] |
| 14 | +pub async fn fetch_link_preview_title(href: String) -> Result<Option<String>, String> { |
| 15 | + let url = Url::parse(href.trim()).map_err(|error| format!("invalid URL: {error}"))?; |
| 16 | + if !is_supported_google_link(&url) { |
| 17 | + return Ok(None); |
| 18 | + } |
| 19 | + |
| 20 | + let client = reqwest::Client::builder() |
| 21 | + .redirect(Policy::none()) |
| 22 | + .pool_idle_timeout(Duration::from_secs(10)) |
| 23 | + .pool_max_idle_per_host(1) |
| 24 | + .build() |
| 25 | + .map_err(|error| format!("link preview title client failed: {error}"))?; |
| 26 | + |
| 27 | + let request = client |
| 28 | + .get(url.as_str()) |
| 29 | + .header( |
| 30 | + ACCEPT, |
| 31 | + "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", |
| 32 | + ) |
| 33 | + .header(USER_AGENT, "Buzz Desktop link preview"); |
| 34 | + |
| 35 | + let response = tokio::time::timeout(TITLE_FETCH_TIMEOUT, request.send()) |
| 36 | + .await |
| 37 | + .map_err(|_| "link preview title request timed out".to_string())? |
| 38 | + .map_err(|error| format!("link preview title request failed: {error}"))?; |
| 39 | + |
| 40 | + if !response.status().is_success() { |
| 41 | + return Ok(None); |
| 42 | + } |
| 43 | + |
| 44 | + let is_html = response |
| 45 | + .headers() |
| 46 | + .get(CONTENT_TYPE) |
| 47 | + .and_then(|value| value.to_str().ok()) |
| 48 | + .map(|value| value.to_ascii_lowercase().contains("text/html")) |
| 49 | + .unwrap_or(true); |
| 50 | + if !is_html { |
| 51 | + return Ok(None); |
| 52 | + } |
| 53 | + |
| 54 | + let body = read_limited_text(response).await?; |
| 55 | + Ok(extract_google_title(&body)) |
| 56 | +} |
| 57 | + |
| 58 | +fn is_supported_google_link(url: &Url) -> bool { |
| 59 | + if url.scheme() != "https" { |
| 60 | + return false; |
| 61 | + } |
| 62 | + |
| 63 | + let Some(host) = url.host_str().map(|host| host.to_ascii_lowercase()) else { |
| 64 | + return false; |
| 65 | + }; |
| 66 | + let segments = url |
| 67 | + .path_segments() |
| 68 | + .map(|segments| segments.collect::<Vec<_>>()) |
| 69 | + .unwrap_or_default(); |
| 70 | + |
| 71 | + match host.trim_start_matches("www.") { |
| 72 | + "docs.google.com" => { |
| 73 | + matches!( |
| 74 | + segments.as_slice(), |
| 75 | + ["document", "d", _, ..] |
| 76 | + | ["spreadsheets", "d", _, ..] |
| 77 | + | ["presentation", "d", _, ..] |
| 78 | + ) |
| 79 | + } |
| 80 | + "drive.google.com" => { |
| 81 | + matches!(segments.as_slice(), ["file", "d", _, ..]) |
| 82 | + || matches!(segments.as_slice(), ["drive", "folders", _, ..]) |
| 83 | + || (segments.first() == Some(&"open") |
| 84 | + && url.query_pairs().any(|(key, _)| key == "id")) |
| 85 | + } |
| 86 | + _ => false, |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +async fn read_limited_text(response: reqwest::Response) -> Result<String, String> { |
| 91 | + let mut stream = response.bytes_stream(); |
| 92 | + let mut bytes = Vec::new(); |
| 93 | + |
| 94 | + while let Some(chunk) = stream.next().await { |
| 95 | + let chunk = chunk.map_err(|error| format!("reading title response failed: {error}"))?; |
| 96 | + if bytes.len() + chunk.len() > MAX_TITLE_FETCH_BYTES { |
| 97 | + let remaining = MAX_TITLE_FETCH_BYTES.saturating_sub(bytes.len()); |
| 98 | + bytes.extend_from_slice(&chunk[..remaining]); |
| 99 | + break; |
| 100 | + } |
| 101 | + bytes.extend_from_slice(&chunk); |
| 102 | + } |
| 103 | + |
| 104 | + Ok(String::from_utf8_lossy(&bytes).into_owned()) |
| 105 | +} |
| 106 | + |
| 107 | +fn extract_google_title(html: &str) -> Option<String> { |
| 108 | + extract_meta_title(html) |
| 109 | + .or_else(|| extract_title_tag(html)) |
| 110 | + .and_then(|title| normalize_google_title(&title)) |
| 111 | +} |
| 112 | + |
| 113 | +fn extract_meta_title(html: &str) -> Option<String> { |
| 114 | + let lower = html.to_ascii_lowercase(); |
| 115 | + let mut search_from = 0; |
| 116 | + |
| 117 | + while let Some(relative_start) = lower[search_from..].find("<meta") { |
| 118 | + let start = search_from + relative_start; |
| 119 | + let Some(relative_end) = lower[start..].find('>') else { |
| 120 | + break; |
| 121 | + }; |
| 122 | + let end = start + relative_end + 1; |
| 123 | + let tag = &html[start..end]; |
| 124 | + let lower_tag = &lower[start..end]; |
| 125 | + |
| 126 | + if lower_tag.contains("og:title") || lower_tag.contains("twitter:title") { |
| 127 | + if let Some(content) = attr_value(tag, "content") { |
| 128 | + return Some(content); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + search_from = end; |
| 133 | + } |
| 134 | + |
| 135 | + None |
| 136 | +} |
| 137 | + |
| 138 | +fn extract_title_tag(html: &str) -> Option<String> { |
| 139 | + let lower = html.to_ascii_lowercase(); |
| 140 | + let start = lower.find("<title")?; |
| 141 | + let content_start = start + lower[start..].find('>')? + 1; |
| 142 | + let content_end = content_start + lower[content_start..].find("</title>")?; |
| 143 | + Some(html[content_start..content_end].to_string()) |
| 144 | +} |
| 145 | + |
| 146 | +fn attr_value(tag: &str, attr: &str) -> Option<String> { |
| 147 | + let lower = tag.to_ascii_lowercase(); |
| 148 | + let attr = attr.to_ascii_lowercase(); |
| 149 | + let mut search_from = 0; |
| 150 | + |
| 151 | + while let Some(relative_start) = lower[search_from..].find(&attr) { |
| 152 | + let name_start = search_from + relative_start; |
| 153 | + let name_end = name_start + attr.len(); |
| 154 | + let before = lower[..name_start].chars().last(); |
| 155 | + let after = lower[name_end..].chars().next(); |
| 156 | + let has_name_boundary = !matches!(before, Some(c) if c.is_ascii_alphanumeric() || c == '-' || c == '_') |
| 157 | + && !matches!(after, Some(c) if c.is_ascii_alphanumeric() || c == '-' || c == '_'); |
| 158 | + |
| 159 | + if has_name_boundary { |
| 160 | + let lower_rest = &lower[name_end..]; |
| 161 | + let equals_offset = lower_rest.find('=')?; |
| 162 | + let value_start = name_end + equals_offset + 1; |
| 163 | + let value = tag[value_start..].trim_start(); |
| 164 | + let quote = value.chars().next()?; |
| 165 | + |
| 166 | + if quote == '"' || quote == '\'' { |
| 167 | + let value_body = &value[quote.len_utf8()..]; |
| 168 | + let value_end = value_body.find(quote)?; |
| 169 | + return Some(decode_html_entities(&value_body[..value_end])); |
| 170 | + } |
| 171 | + |
| 172 | + let value_end = value |
| 173 | + .find(|c: char| c.is_ascii_whitespace() || c == '>') |
| 174 | + .unwrap_or(value.len()); |
| 175 | + return Some(decode_html_entities(&value[..value_end])); |
| 176 | + } |
| 177 | + |
| 178 | + search_from = name_end; |
| 179 | + } |
| 180 | + |
| 181 | + None |
| 182 | +} |
| 183 | + |
| 184 | +fn normalize_google_title(raw_title: &str) -> Option<String> { |
| 185 | + let mut title = decode_html_entities(raw_title) |
| 186 | + .split_whitespace() |
| 187 | + .collect::<Vec<_>>() |
| 188 | + .join(" "); |
| 189 | + |
| 190 | + for suffix in [ |
| 191 | + " - Google Docs", |
| 192 | + " - Google Sheets", |
| 193 | + " - Google Slides", |
| 194 | + " - Google Drive", |
| 195 | + ] { |
| 196 | + if let Some(stripped) = title.strip_suffix(suffix) { |
| 197 | + title = stripped.trim().to_string(); |
| 198 | + break; |
| 199 | + } |
| 200 | + } |
| 201 | + |
| 202 | + match title.as_str() { |
| 203 | + "" |
| 204 | + | "Document" |
| 205 | + | "Spreadsheet" |
| 206 | + | "Presentation" |
| 207 | + | "Drive file" |
| 208 | + | "Drive folder" |
| 209 | + | "Google Docs" |
| 210 | + | "Google Sheets" |
| 211 | + | "Google Slides" |
| 212 | + | "Google Drive" |
| 213 | + | "Sign in - Google Accounts" => None, |
| 214 | + _ => Some(title.chars().take(180).collect()), |
| 215 | + } |
| 216 | +} |
| 217 | + |
| 218 | +fn decode_html_entities(value: &str) -> String { |
| 219 | + let mut decoded = value |
| 220 | + .replace("&", "&") |
| 221 | + .replace(""", "\"") |
| 222 | + .replace("'", "'") |
| 223 | + .replace("'", "'") |
| 224 | + .replace("'", "'") |
| 225 | + .replace("<", "<") |
| 226 | + .replace(">", ">"); |
| 227 | + |
| 228 | + while let Some(start) = decoded.find("&#") { |
| 229 | + let Some(relative_end) = decoded[start..].find(';') else { |
| 230 | + break; |
| 231 | + }; |
| 232 | + let end = start + relative_end + 1; |
| 233 | + let entity = &decoded[start + 2..end - 1]; |
| 234 | + let parsed = if let Some(hex) = entity |
| 235 | + .strip_prefix('x') |
| 236 | + .or_else(|| entity.strip_prefix('X')) |
| 237 | + { |
| 238 | + u32::from_str_radix(hex, 16).ok() |
| 239 | + } else { |
| 240 | + entity.parse::<u32>().ok() |
| 241 | + }; |
| 242 | + |
| 243 | + let Some(ch) = parsed.and_then(char::from_u32) else { |
| 244 | + break; |
| 245 | + }; |
| 246 | + decoded.replace_range(start..end, &ch.to_string()); |
| 247 | + } |
| 248 | + |
| 249 | + decoded |
| 250 | +} |
| 251 | + |
| 252 | +#[cfg(test)] |
| 253 | +mod tests { |
| 254 | + use super::{extract_google_title, is_supported_google_link}; |
| 255 | + use url::Url; |
| 256 | + |
| 257 | + #[test] |
| 258 | + fn title_prefers_open_graph_title() { |
| 259 | + let html = r#" |
| 260 | + <html> |
| 261 | + <head> |
| 262 | + <meta property="og:title" content="Composer links & previews - Google Docs"> |
| 263 | + <title>Fallback - Google Docs</title> |
| 264 | + </head> |
| 265 | + </html> |
| 266 | + "#; |
| 267 | + |
| 268 | + assert_eq!( |
| 269 | + extract_google_title(html).as_deref(), |
| 270 | + Some("Composer links & previews") |
| 271 | + ); |
| 272 | + } |
| 273 | + |
| 274 | + #[test] |
| 275 | + fn title_ignores_generic_google_titles() { |
| 276 | + assert_eq!( |
| 277 | + extract_google_title("<title>Sign in - Google Accounts</title>"), |
| 278 | + None |
| 279 | + ); |
| 280 | + assert_eq!(extract_google_title("<title>Google Docs</title>"), None); |
| 281 | + } |
| 282 | + |
| 283 | + #[test] |
| 284 | + fn supported_urls_are_google_file_links_only() { |
| 285 | + assert!(is_supported_google_link( |
| 286 | + &Url::parse("https://docs.google.com/document/d/abc/edit").unwrap() |
| 287 | + )); |
| 288 | + assert!(is_supported_google_link( |
| 289 | + &Url::parse("https://docs.google.com/spreadsheets/d/abc/edit").unwrap() |
| 290 | + )); |
| 291 | + assert!(is_supported_google_link( |
| 292 | + &Url::parse("https://drive.google.com/file/d/abc/view").unwrap() |
| 293 | + )); |
| 294 | + assert!(!is_supported_google_link( |
| 295 | + &Url::parse("https://example.com/document/d/abc/edit").unwrap() |
| 296 | + )); |
| 297 | + assert!(!is_supported_google_link( |
| 298 | + &Url::parse("http://docs.google.com/document/d/abc/edit").unwrap() |
| 299 | + )); |
| 300 | + } |
| 301 | +} |
0 commit comments