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