| 1 | use base64::Engine as _; |
| 2 | use base64::engine::general_purpose::STANDARD; |
| 3 | use serde_json::{Value, json}; |
| 4 | use session::{App, Scope, Session}; |
| 5 | |
| 6 | use crate::error::{Error, Result}; |
| 7 | use crate::links::{LinkImage, decoded_image, trusted_ams_image_url}; |
| 8 | use crate::messages::ensure_success; |
| 9 | use crate::pins::transport::{CSA_RESOURCE, CSA_SCOPE, skype_fetch_answer}; |
| 10 | |
| 11 | const AMS_BASE: &str = "https://de-prod.asyncgw.teams.microsoft.com/"; |
| 12 | const UPLOAD_BODY: &str = concat!( |
| 13 | include_str!("../assets/skype_token.js"), |
| 14 | include_str!("../assets/ams_upload.js") |
| 15 | ); |
| 16 | const DOWNLOAD_BODY: &str = concat!( |
| 17 | include_str!("../assets/skype_token.js"), |
| 18 | include_str!("../assets/ams_download.js") |
| 19 | ); |
| 20 | const FULL_SIZE_VIEW: &str = "/views/imgpsh_fullsize"; |
| 21 | |
| 22 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 23 | pub struct AmsImage { |
| 24 | pub object_id: String, |
| 25 | pub url: String, |
| 26 | } |
| 27 | |
| 28 | pub fn full_size_url(object_id: &str) -> String { |
| 29 | format!("{AMS_BASE}v1/objects/{object_id}{FULL_SIZE_VIEW}") |
| 30 | } |
| 31 | |
| 32 | fn uploaded_image(body: &Value) -> Result<AmsImage> { |
| 33 | let object_id = body |
| 34 | .get("id") |
| 35 | .and_then(Value::as_str) |
| 36 | .filter(|id| { |
| 37 | !id.is_empty() |
| 38 | && id |
| 39 | .chars() |
| 40 | .all(|character| character.is_ascii_alphanumeric() || character == '-') |
| 41 | }) |
| 42 | .ok_or_else(|| Error::UnexpectedAnswer("ams upload has no object id".into()))?; |
| 43 | Ok(AmsImage { |
| 44 | object_id: object_id.to_owned(), |
| 45 | url: full_size_url(object_id), |
| 46 | }) |
| 47 | } |
| 48 | |
| 49 | pub struct Ams { |
| 50 | session: Session, |
| 51 | } |
| 52 | |
| 53 | impl Ams { |
| 54 | pub fn new(session: &Session) -> Self { |
| 55 | Ams { |
| 56 | session: session.clone(), |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | /// The image is readable by members of `conversation_id` only. |
| 61 | pub async fn upload_image( |
| 62 | &self, |
| 63 | conversation_id: &str, |
| 64 | bytes: &[u8], |
| 65 | filename: &str, |
| 66 | ) -> Result<AmsImage> { |
| 67 | let arguments = json!({ |
| 68 | "base": AMS_BASE, |
| 69 | "conversationId": conversation_id, |
| 70 | "filename": filename, |
| 71 | "base64": STANDARD.encode(bytes), |
| 72 | }); |
| 73 | let answer = self |
| 74 | .session |
| 75 | .run_with_token( |
| 76 | App::Teams, |
| 77 | &Scope::new(CSA_RESOURCE, CSA_SCOPE), |
| 78 | UPLOAD_BODY, |
| 79 | &arguments, |
| 80 | false, |
| 81 | ) |
| 82 | .await?; |
| 83 | let answer = skype_fetch_answer(answer); |
| 84 | ensure_success(&answer)?; |
| 85 | uploaded_image(&answer.body) |
| 86 | } |
| 87 | |
| 88 | /// Reads an AMS image view such as `.../v1/objects/{id}/views/imgo` with the Skype token. |
| 89 | pub async fn download_image(&self, url: &str) -> Result<LinkImage> { |
| 90 | let Some(url) = trusted_ams_image_url(url) else { |
| 91 | return Err(Error::UnexpectedAnswer(format!( |
| 92 | "not an AMS image url: {url}" |
| 93 | ))); |
| 94 | }; |
| 95 | let answer = self |
| 96 | .session |
| 97 | .run_with_token( |
| 98 | App::Teams, |
| 99 | &Scope::new(CSA_RESOURCE, CSA_SCOPE), |
| 100 | DOWNLOAD_BODY, |
| 101 | &json!({ "url": url.as_str() }), |
| 102 | false, |
| 103 | ) |
| 104 | .await?; |
| 105 | let answer = skype_fetch_answer(answer); |
| 106 | ensure_success(&answer)?; |
| 107 | decoded_image(&answer.body) |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | #[cfg(test)] |
| 112 | mod tests { |
| 113 | use super::*; |
| 114 | |
| 115 | #[test] |
| 116 | fn an_uploaded_object_becomes_its_full_size_view() { |
| 117 | let image = uploaded_image(&json!({"id": "0-weu-d4-abc123"})).unwrap(); |
| 118 | assert_eq!(image.object_id, "0-weu-d4-abc123"); |
| 119 | assert_eq!( |
| 120 | image.url, |
| 121 | "https://de-prod.asyncgw.teams.microsoft.com/v1/objects/0-weu-d4-abc123/views/imgpsh_fullsize" |
| 122 | ); |
| 123 | assert!(uploaded_image(&json!({"id": "../x"})).is_err()); |
| 124 | assert!(uploaded_image(&json!({})).is_err()); |
| 125 | } |
| 126 | } |