firebase_wasm/storage/
bindings.rs1use serde::{Deserialize, Serialize};
2use std::collections::HashMap;
3use wasm_bindgen::prelude::*;
4
5#[derive(Debug, Clone, TypedBuilder, Serialize)]
6#[serde(rename_all = "camelCase")]
7#[builder(field_defaults(default, setter(strip_option, into)))]
8pub struct UploadMetadataOptions {
9 pub cache_control: Option<String>,
10 pub content_disposition: Option<String>,
11 pub content_encoding: Option<String>,
12 pub content_language: Option<String>,
13 pub content_type: Option<String>,
14 #[builder(default, setter(skip))]
15 custom_metadata: HashMap<String, String>,
16 pub md5_hash: Option<String>,
17}
18
19impl UploadMetadataOptions {
20 pub fn add_custom_metadata(mut self, key: impl ToString, value: impl ToString) -> Self {
21 self.custom_metadata
22 .insert(key.to_string(), value.to_string());
23
24 self
25 }
26}
27
28impl UploadMetadata {
29 pub fn get_custom_metadata<T>(&self) -> Result<Option<T>, serde_wasm_bindgen::Error>
30 where
31 T: for<'de> Deserialize<'de>,
32 {
33 self.custom_metadata()
34 .map(|o| serde_wasm_bindgen::from_value::<T>(o.into()))
35 .transpose()
36 }
37}
38
39pub fn upload_bytes(
40 ref_: Ref,
41 data: &web_sys::Blob,
42 metadata: Option<UploadMetadataOptions>,
43) -> Result<UploadTask, JsValue> {
44 let serializer = serde_wasm_bindgen::Serializer::new().serialize_maps_as_objects(true);
45
46 let metadata = metadata.serialize(&serializer).unwrap();
47
48 upload_bytes_(ref_, data, metadata)
49}
50
51#[wasm_bindgen(module = "firebase/storage")]
52extern "C" {
53 pub type Storage;
54 pub type Ref;
55 pub type UploadTask;
56 pub type UploadTaskSnapshot;
57 pub type SettableMetadata;
58 #[wasm_bindgen(extends = SettableMetadata)]
59 pub type UploadMetadata;
60 #[wasm_bindgen(extends = UploadMetadata)]
61 pub type FullMetadata;
62 pub type TaskState;
63
64 #[wasm_bindgen(js_name = getStorage)]
65 pub fn get_storage() -> Storage;
66
67 #[wasm_bindgen(js_name = ref)]
68 pub fn ref_(storage: Storage, path: &str) -> Ref;
69
70 #[wasm_bindgen(js_name = uploadBytesResumable, catch)]
71 fn upload_bytes_(
72 ref_: Ref,
73 data: &web_sys::Blob,
74 metadata: JsValue,
75 ) -> Result<UploadTask, JsValue>;
76
77 #[wasm_bindgen(js_name = getDownloadURL, catch)]
78 pub async fn get_download_url(ref_: Ref) -> Result<JsValue, JsValue>;
79
80 #[wasm_bindgen(js_name = deleteObject, catch)]
81 pub async fn delete_object(ref_: Ref) -> Result<(), JsValue>;
82
83 #[wasm_bindgen(js_name = getMetadata, catch)]
84 pub async fn get_metadata(ref_: Ref) -> Result<JsValue, JsValue>;
85
86 #[wasm_bindgen(method)]
91 pub fn on(
92 this: &UploadTask,
93 event: &str,
94 on_snapshot: &Closure<dyn FnMut(UploadTaskSnapshot)>,
95 on_error: Option<&Closure<dyn FnMut(JsValue)>>,
96 on_complete: Option<&Closure<dyn FnMut()>>,
97 ) -> js_sys::Function;
98
99 #[wasm_bindgen(method)]
100 pub fn cancel(this: &UploadTask) -> bool;
101
102 #[wasm_bindgen(method)]
103 pub fn pause(this: &UploadTask) -> bool;
104
105 #[wasm_bindgen(method)]
106 pub fn resume(this: &UploadTask) -> bool;
107
108 #[wasm_bindgen(method, getter)]
109 pub fn snapshot(this: &UploadTask) -> UploadTaskSnapshot;
110
111 #[wasm_bindgen(method, getter, js_name = bytesTransferred)]
116 pub fn bytes_transferred(this: &UploadTaskSnapshot) -> usize;
117
118 #[wasm_bindgen(method, getter, js_name = totalBytes)]
119 pub fn total_bytes(this: &UploadTaskSnapshot) -> usize;
120
121 #[wasm_bindgen(method, getter, js_name = ref)]
122 pub fn ref_(this: &UploadTaskSnapshot) -> Ref;
123
124 #[wasm_bindgen(method, getter)]
125 pub fn metadata(this: &UploadTaskSnapshot) -> FullMetadata;
126
127 #[wasm_bindgen(method, getter)]
128 pub fn state(this: &UploadTaskSnapshot) -> String;
129
130 #[wasm_bindgen(method, getter)]
131 pub fn task(this: &UploadTaskSnapshot) -> UploadTask;
132
133 #[wasm_bindgen(method, getter, js_name = cacheControl)]
138 pub fn cache_control(this: &SettableMetadata) -> Option<String>;
139
140 #[wasm_bindgen(method, getter, js_name = contentDisposition)]
141 pub fn content_disposition(this: &SettableMetadata) -> Option<String>;
142
143 #[wasm_bindgen(method, getter, js_name = contentEncoding)]
144 pub fn content_encoding(this: &SettableMetadata) -> Option<String>;
145
146 #[wasm_bindgen(method, getter, js_name = contentLanguage)]
147 pub fn content_language(this: &SettableMetadata) -> Option<String>;
148
149 #[wasm_bindgen(method, getter, js_name = contentType)]
150 pub fn content_type(this: &SettableMetadata) -> Option<String>;
151
152 #[wasm_bindgen(method, getter, js_name = customMetadata)]
153 pub fn custom_metadata(this: &SettableMetadata) -> Option<js_sys::Object>;
154
155 #[wasm_bindgen(method, getter, js_name = md5Hash)]
160 pub fn md5_hash(this: &UploadMetadata) -> Option<String>;
161
162 #[wasm_bindgen(method, getter)]
167 pub fn bucket(this: &FullMetadata) -> String;
168
169 #[wasm_bindgen(method, getter, js_name = fullPath)]
170 pub fn full_path(this: &FullMetadata) -> String;
171
172 #[wasm_bindgen(method, getter)]
173 pub fn generation(this: &FullMetadata) -> String;
174
175 #[wasm_bindgen(method, getter)]
176 pub fn metageneration(this: &FullMetadata) -> String;
177
178 #[wasm_bindgen(method, getter)]
179 pub fn name(this: &FullMetadata) -> String;
180
181 #[wasm_bindgen(method, getter)]
182 pub fn size(this: &FullMetadata) -> u64;
183
184 #[wasm_bindgen(method, getter, js_name = timeCreated)]
185 pub fn time_created(this: &FullMetadata) -> String;
186
187 #[wasm_bindgen(method, getter)]
188 pub fn updated(this: &FullMetadata) -> String;
189
190 #[wasm_bindgen(method, getter, js_name = downloadTokens)]
191 pub fn download_tokens(this: &FullMetadata) -> Vec<js_sys::JsString>;
192
193 #[wasm_bindgen(method, getter, js_name = ref)]
194 pub fn ref_(this: &FullMetadata) -> Option<Ref>;
195}