1use serde::{Deserialize, Serialize};
2use std::fmt::Display;
3
4#[derive(Debug, Deserialize, Serialize)]
5pub struct Metadata {
6 #[serde(rename = "uploadedBy")]
7 uploaded_by: String,
8}
9#[derive(Debug, Deserialize, Serialize)]
10pub struct UtRecord {
11 #[serde(rename = "fileKeys")]
12 pub file_keys: Vec<String>,
13 pub metadata: Metadata,
14 #[serde(rename = "callbackUrl")]
15 pub callback_url: String,
16 #[serde(rename = "callbackSlug")]
17 pub callback_slug: String,
18 #[serde(rename = "awaitServerData")]
19 pub await_server_data: bool,
20 #[serde(rename = "isDev")]
21 pub is_dev: bool,
22}
23impl Display for UtRecord {
24 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
25 write!(f, "{}", serde_json::to_string(&self).unwrap())
26 }
27}
28impl Default for UtRecord {
29 fn default() -> Self {
30 Self {
31 file_keys: Vec::new(),
32 metadata: Metadata {
33 uploaded_by: "anonymous".to_string(),
34 },
35 callback_url: std::env::var("UT_CALLBACK_DOMAIN")
36 .expect("UT_CALLBACK_DOMAIN has not been set"),
37 callback_slug: "imageUploader".to_string(),
38 await_server_data: false,
39 is_dev: false,
40 }
41 }
42}
43impl TryFrom<&web_sys::wasm_bindgen::JsValue> for UtRecord {
44 type Error = web_sys::wasm_bindgen::JsValue;
45 fn try_from(value: &web_sys::wasm_bindgen::JsValue) -> Result<Self, Self::Error> {
46 let js_str = web_sys::js_sys::JSON::stringify(value)?;
47 let str = js_str
48 .as_string()
49 .ok_or_else(|| web_sys::wasm_bindgen::JsValue::from_str("Failed to stringify JSON"))?;
50 let presigned_url: Self = serde_json::from_str(&str).map_err(|e| {
51 web_sys::wasm_bindgen::JsValue::from_str(&format!("Failed to parse JSON: {}", e))
52 })?;
53 Ok(presigned_url)
54 }
55}
56impl TryFrom<web_sys::wasm_bindgen::JsValue> for UtRecord {
57 type Error = web_sys::wasm_bindgen::JsValue;
58 fn try_from(value: web_sys::wasm_bindgen::JsValue) -> Result<Self, Self::Error> {
59 Self::try_from(&value)
60 }
61}