Commit 57bcbe693c
Unsigned
Layout: unified · split
src/commands/media.rs +29 −1
| @@ -50,7 +50,7 @@ impl MediaCommand { | ||
| 50 | 50 | |
| 51 | 51 | let file = args |
| 52 | 52 | .value_of("file") |
| 53 | .map(|file| PathBuf::from(Weechat::expand_home(file))) | |
| 53 | .map(normalize_download_file) | |
| 54 | 54 | .or_else(|| default_download_file(&uri, &download_prefix)); |
| 55 | 55 | let file = match file { |
| 56 | 56 | Some(file) => file, |
| @@ -96,6 +96,22 @@ impl MediaCommand { | ||
| 96 | 96 | ]; |
| 97 | 97 | } |
| 98 | 98 | |
| 99 | fn normalize_download_file(file: &str) -> PathBuf { | |
| 100 | PathBuf::from(Weechat::expand_home(strip_download_file_quotes(file))) | |
| 101 | } | |
| 102 | ||
| 103 | fn strip_download_file_quotes(file: &str) -> &str { | |
| 104 | let file = file.trim(); | |
| 105 | file | |
| 106 | .strip_prefix('"') | |
| 107 | .and_then(|file| file.strip_suffix('"')) | |
| 108 | .or_else(|| { | |
| 109 | file.strip_prefix('\'') | |
| 110 | .and_then(|file| file.strip_suffix('\'')) | |
| 111 | }) | |
| 112 | .unwrap_or(file) | |
| 113 | } | |
| 114 | ||
| 99 | 115 | fn default_download_file(uri: &MxcUri, prefix: &str) -> Option<PathBuf> { |
| 100 | 116 | uri.as_str() |
| 101 | 117 | .trim_end_matches('/') |
| @@ -182,6 +198,18 @@ mod tests { | ||
| 182 | 198 | ); |
| 183 | 199 | } |
| 184 | 200 | |
| 201 | #[test] | |
| 202 | fn strips_weechat_preserved_quotes_from_download_file() { | |
| 203 | assert_eq!( | |
| 204 | "/tmp/a path/image.png", | |
| 205 | strip_download_file_quotes("\"/tmp/a path/image.png\"") | |
| 206 | ); | |
| 207 | assert_eq!( | |
| 208 | "/tmp/a path/image.png", | |
| 209 | strip_download_file_quotes("'/tmp/a path/image.png'") | |
| 210 | ); | |
| 211 | } | |
| 212 | ||
| 185 | 213 | #[test] |
| 186 | 214 | fn default_download_prefix_can_include_directory() { |
| 187 | 215 | let uri = Box::<MxcUri>::from("mxc://matrix.org/some-media-id"); |
src/render.rs +54 −5
| @@ -1,3 +1,4 @@ | ||
| 1 | use base64::{engine::general_purpose::URL_SAFE_NO_PAD, Engine as _}; | |
| 1 | 2 | use url::Url; |
| 2 | 3 | |
| 3 | 4 | use matrix_sdk::{ |
| @@ -525,10 +526,34 @@ fn media_download_message<C: HasUrlOrFile>(content: &C) -> Option<String> { | ||
| 525 | 526 | }) |
| 526 | 527 | } |
| 527 | 528 | |
| 529 | fn media_tags<C: HasUrlOrFile>(content: &C) -> Vec<String> { | |
| 530 | let mut tags = vec![ | |
| 531 | "matrix_media".to_owned(), | |
| 532 | format!("matrix_media_kind_{}", content.media_kind()), | |
| 533 | format!( | |
| 534 | "matrix_media_name_{}", | |
| 535 | URL_SAFE_NO_PAD.encode(content.body()) | |
| 536 | ), | |
| 537 | ]; | |
| 538 | ||
| 539 | if let MediaSource::Plain(uri) = content.source() { | |
| 540 | tags.push(format!( | |
| 541 | "matrix_media_uri_{}", | |
| 542 | URL_SAFE_NO_PAD.encode(uri.as_str()) | |
| 543 | )); | |
| 544 | } | |
| 545 | ||
| 546 | tags | |
| 547 | } | |
| 548 | ||
| 528 | 549 | impl<C: HasUrlOrFile> Render for C { |
| 529 | 550 | type RenderContext = Url; |
| 530 | 551 | const TAGS: &'static [&'static str] = &["matrix_media"]; |
| 531 | 552 | |
| 553 | fn tags(&self) -> Vec<String> { | |
| 554 | media_tags(self) | |
| 555 | } | |
| 556 | ||
| 532 | 557 | fn render(&self, homeserver: &Self::RenderContext) -> RenderedContent { |
| 533 | 558 | // Convert MXC to HTTP(s) or EMXC, but fallback to MXC if unable to. |
| 534 | 559 | let mxc_url = match self.encrypted_file() { |
| @@ -1317,6 +1342,8 @@ has_formatted_body!(TextMessageEventContent); | ||
| 1317 | 1342 | pub trait HasUrlOrFile { |
| 1318 | 1343 | fn body(&self) -> &str; |
| 1319 | 1344 | |
| 1345 | fn media_kind(&self) -> &'static str; | |
| 1346 | ||
| 1320 | 1347 | #[inline] |
| 1321 | 1348 | fn resolve_url(&self) -> &MxcUri { |
| 1322 | 1349 | match self.source() { |
| @@ -1333,12 +1360,16 @@ pub trait HasUrlOrFile { | ||
| 1333 | 1360 | // Same as above: a simple macro to implement the trait for structs with `url` |
| 1334 | 1361 | // and `file` fields. |
| 1335 | 1362 | macro_rules! has_url_or_file { |
| 1336 | ($content: ident) => { | |
| 1363 | ($content: ident, $kind: literal) => { | |
| 1337 | 1364 | impl HasUrlOrFile for $content { |
| 1338 | 1365 | fn body(&self) -> &str { |
| 1339 | 1366 | &self.body |
| 1340 | 1367 | } |
| 1341 | 1368 | |
| 1369 | fn media_kind(&self) -> &'static str { | |
| 1370 | $kind | |
| 1371 | } | |
| 1372 | ||
| 1342 | 1373 | fn source(&self) -> &MediaSource { |
| 1343 | 1374 | &self.source |
| 1344 | 1375 | } |
| @@ -1353,10 +1384,10 @@ macro_rules! has_url_or_file { | ||
| 1353 | 1384 | }; |
| 1354 | 1385 | } |
| 1355 | 1386 | |
| 1356 | has_url_or_file!(AudioMessageEventContent); | |
| 1357 | has_url_or_file!(FileMessageEventContent); | |
| 1358 | has_url_or_file!(ImageMessageEventContent); | |
| 1359 | has_url_or_file!(VideoMessageEventContent); | |
| 1387 | has_url_or_file!(AudioMessageEventContent, "audio"); | |
| 1388 | has_url_or_file!(FileMessageEventContent, "file"); | |
| 1389 | has_url_or_file!(ImageMessageEventContent, "image"); | |
| 1390 | has_url_or_file!(VideoMessageEventContent, "video"); | |
| 1360 | 1391 | |
| 1361 | 1392 | /// Rendering implementation for membership events (joins, leaves, bans, profile |
| 1362 | 1393 | /// changes, etc). |
| @@ -1597,6 +1628,24 @@ mod tests { | ||
| 1597 | 1628 | ); |
| 1598 | 1629 | } |
| 1599 | 1630 | |
| 1631 | #[test] | |
| 1632 | fn plain_image_exposes_structured_media_tags() { | |
| 1633 | let content = ImageMessageEventContent::plain( | |
| 1634 | "image.png".to_owned(), | |
| 1635 | OwnedMxcUri::from("mxc://matrix.org/some-media-id"), | |
| 1636 | ); | |
| 1637 | ||
| 1638 | assert_eq!( | |
| 1639 | vec![ | |
| 1640 | "matrix_media", | |
| 1641 | "matrix_media_kind_image", | |
| 1642 | "matrix_media_name_aW1hZ2UucG5n", | |
| 1643 | "matrix_media_uri_bXhjOi8vbWF0cml4Lm9yZy9zb21lLW1lZGlhLWlk", | |
| 1644 | ], | |
| 1645 | content.tags() | |
| 1646 | ); | |
| 1647 | } | |
| 1648 | ||
| 1600 | 1649 | #[test] |
| 1601 | 1650 | fn formatted_body_plain_text_decodes_matrix_html() { |
| 1602 | 1651 | let body = "<p>Hello <b>world</b> & \ |