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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
use blobd_token::AuthToken;
use blobd_token::AuthTokenAction;
use blobd_token::BlobdTokens;
use bytes::Bytes;
use futures::channel::mpsc::UnboundedSender;
use futures::stream::iter;
use futures::Stream;
use futures::StreamExt;
use futures::TryStreamExt;
use off64::create_u16_be;
use off64::create_u40_be;
use percent_encoding::utf8_percent_encode;
use percent_encoding::CONTROLS;
use reqwest::header::CONTENT_LENGTH;
use reqwest::header::RANGE;
use reqwest::Body;
use serde::Deserialize;
use serde::Serialize;
use std::error::Error;
use std::fmt::Write;
use std::time::SystemTime;
use std::time::UNIX_EPOCH;

// This client has internal state that is not very cheap to clone, nor memory efficient when cloned lots. Therefore, we don't derive Clone for it; wrap it in an Arc for cheap sharing.
pub struct BlobdClient {
  client: reqwest::Client,
  url_prefix: String,
  tokens: BlobdTokens,
}

fn now() -> u64 {
  SystemTime::now()
    .duration_since(UNIX_EPOCH)
    .expect("system clock is before 1970")
    .as_secs()
}

type BoxErr = Box<dyn Error + Send + Sync>;

pub struct BatchCreateObjectEntry<DS: Stream<Item = Result<Bytes, BoxErr>>> {
  pub size: u64,
  pub data_stream: DS,
  pub key: Vec<u8>,
}

pub struct BatchCreatedObjects {
  pub successful_count: u64,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, Hash)]
pub struct CreatedObject {
  pub object_id: u64,
  pub upload_id: String,
}

#[derive(Serialize, Deserialize, Clone, Copy, Debug, PartialEq, Eq)]
pub struct InspectedObject {
  pub object_id: u64,
  pub content_length: u64,
}

impl BlobdClient {
  pub fn new(url_prefix: String, token_secret: [u8; 32]) -> BlobdClient {
    BlobdClient {
      client: reqwest::Client::new(),
      tokens: BlobdTokens::new(token_secret),
      url_prefix,
    }
  }

  fn build_url(&self, key: &str) -> String {
    let mut url = self.url_prefix.clone();
    for (i, p) in key.split('/').enumerate() {
      if i > 0 {
        url.push('/');
      };
      url.extend(utf8_percent_encode(p, CONTROLS));
    }
    url
  }

  pub fn generate_presigned_url(
    &self,
    key: &str,
    action: AuthTokenAction,
    expires_in_seconds: u64,
  ) -> String {
    let mut url = self.build_url(key);
    let t = AuthToken::new(&self.tokens, action, now() + expires_in_seconds);
    write!(url, "?t={}", utf8_percent_encode(&t, CONTROLS)).unwrap();
    url
  }

  /// Parts to this method:
  /// - The list of objects. It's an infallible stream to allow for scenarios where the list is not known ahead of time or is buffered asynchronously.
  /// - Each object's data. It's a fallable stream of chunks of bytes, using the bytes::Bytes type as that's what reqwest requires.
  /// Approaches to the list of objects:
  /// - If it's all in memory, use `futures::stream::iter(my_list_of_objects)`.
  /// - If it can be derived from an existing stream, use the `StreamExt` methods to transform items into `BatchCreateObjectEntry`.
  /// - If it is dynamically built from another thread/async function, use `futures::channel::mpsc::unbounded`; the receiver already implements `Stream` and can be provided as the list.
  /// Approaches to object data stream:
  /// - If it's all in memory, use `futures::stream::Once(Ok(bytes::Bytes::from(my_object_data)))`.
  /// - If it's a synchronous Read, wrap in a Stream that reads chunks into a `Bytes`, preferably on a different thread (e.g. `spawn_blocking`).
  /// - If it's an AsyncRead, read in chunks and wrap each chunk with `Bytes::from`.
  /// Approaches to error handling:
  /// - The blobd server will never return a bad status, but will bail as soon as an error occurs.
  /// - However, reqwest will bail if the request body fails, which can occur if a `data_stream` of a `BatchCreateObjectEntry` yields an `Err` chunk.
  /// - If reqwest bails, the response will be unavailable and the amount of successfully committed objects will not be returned.
  /// - Therefore, there are some optional approaches worth considering:
  ///   - Filter out `BatchCreateObjectEntry` items that have a `data_stream` that will definitely fail. Once an entry starts being transferred, there's no way to skip over it midway.
  ///   - When a `data_stream` chunk is about to yield `Err`, return an `Ok` instead with empty data and stop the object list stream (as the current data transferred is midway in an object and there's no way to skip over it). The server will notice that the object was not completely uploaded, decide to bail, and return the successful count.
  pub async fn batch_create_objects<DS, Objects>(
    &self,
    objects: Objects,
    transfer_byte_counter: UnboundedSender<usize>,
  ) -> reqwest::Result<BatchCreatedObjects>
  where
    DS: 'static + Stream<Item = Result<Bytes, BoxErr>> + Send + Sync,
    Objects: 'static + Stream<Item = BatchCreateObjectEntry<DS>> + Send + Sync,
  {
    let body_stream = objects.flat_map(move |e| {
      let transfer_byte_counter = transfer_byte_counter.clone();
      iter(vec![
        Ok(Bytes::from(
          create_u16_be(e.key.len().try_into().unwrap()).to_vec(),
        )),
        Ok(Bytes::from(e.key)),
        Ok(Bytes::from(create_u40_be(e.size).to_vec())),
      ])
      .chain(
        e.data_stream
          .inspect_ok(move |chunk| transfer_byte_counter.unbounded_send(chunk.len()).unwrap()),
      )
    });
    let body = Body::wrap_stream(body_stream);
    let t = AuthToken::new(
      &self.tokens,
      AuthTokenAction::BatchCreateObjects {},
      now() + 300,
    );
    let res = self
      .client
      .post(self.url_prefix.clone())
      .query(&[("t", t)])
      .body(body)
      .send()
      .await?
      .error_for_status()?;
    Ok(BatchCreatedObjects {
      successful_count: res
        .headers()
        .get("x-blobd-objects-created-count")
        .unwrap()
        .to_str()
        .unwrap()
        .parse()
        .unwrap(),
    })
  }

  pub async fn create_object(&self, key: &str, size: u64) -> reqwest::Result<CreatedObject> {
    let t = AuthToken::new(
      &self.tokens,
      AuthTokenAction::CreateObject {
        key: key.as_bytes().to_vec(),
        size,
      },
      now() + 300,
    );
    let url = self.build_url(key);
    let res = self
      .client
      .post(url)
      .query(&[("size", size.to_string()), ("t", t)])
      .send()
      .await?
      .error_for_status()?;
    Ok(CreatedObject {
      object_id: res
        .headers()
        .get("x-blobd-object-id")
        .unwrap()
        .to_str()
        .unwrap()
        .parse()
        .unwrap(),
      upload_id: res
        .headers()
        .get("x-blobd-upload-id")
        .unwrap()
        .to_str()
        .unwrap()
        .parse()
        .unwrap(),
    })
  }

  pub async fn commit_object(&self, key: &str, creation: CreatedObject) -> reqwest::Result<()> {
    let t = AuthToken::new(
      &self.tokens,
      AuthTokenAction::CommitObject {
        object_id: creation.object_id,
      },
      now() + 300,
    );
    let url = self.build_url(key);
    self
      .client
      .put(url)
      .query(&[
        ("object_id", creation.object_id.to_string()),
        ("upload_id", creation.upload_id.to_string()),
        ("t", t),
      ])
      .send()
      .await?
      .error_for_status()?;
    Ok(())
  }

  pub async fn delete_object(&self, key: &str) -> reqwest::Result<()> {
    let t = AuthToken::new(
      &self.tokens,
      AuthTokenAction::DeleteObject {
        key: key.as_bytes().to_vec(),
      },
      now() + 300,
    );
    let url = self.build_url(key);
    self
      .client
      .delete(url)
      .query(&[("t", t)])
      .send()
      .await?
      .error_for_status()?;
    Ok(())
  }

  pub async fn inspect_object(&self, key: &str) -> reqwest::Result<InspectedObject> {
    let t = AuthToken::new(
      &self.tokens,
      AuthTokenAction::InspectObject {
        key: key.as_bytes().to_vec(),
      },
      now() + 300,
    );
    let url = self.build_url(key);
    let res = self
      .client
      .head(url)
      .query(&[("t", t)])
      .send()
      .await?
      .error_for_status()?;
    Ok(InspectedObject {
      object_id: res
        .headers()
        .get("x-blobd-object-id")
        .unwrap()
        .to_str()
        .unwrap()
        .parse()
        .unwrap(),
      content_length: res
        .headers()
        .get(CONTENT_LENGTH)
        .unwrap()
        .to_str()
        .unwrap()
        .parse()
        .unwrap(),
    })
  }

  pub async fn read_object(
    &self,
    key: &str,
    start: u64,
    end: Option<u64>,
  ) -> reqwest::Result<impl Stream<Item = reqwest::Result<Bytes>>> {
    let t = AuthToken::new(
      &self.tokens,
      AuthTokenAction::ReadObject {
        key: key.as_bytes().to_vec(),
      },
      now() + 300,
    );
    let url = self.build_url(key);
    let res = self
      .client
      .get(url)
      .query(&[("t", t)])
      .header(
        RANGE,
        format!(
          "bytes={}-{}",
          start,
          end.map(|e| e.to_string()).unwrap_or_default()
        ),
      )
      .send()
      .await?
      .error_for_status()?;
    Ok(res.bytes_stream())
  }

  pub async fn write_object(
    &self,
    key: &str,
    creation: CreatedObject,
    offset: u64,
    data: impl Into<Body>,
  ) -> reqwest::Result<()> {
    let t = AuthToken::new(
      &self.tokens,
      AuthTokenAction::WriteObject {
        object_id: creation.object_id,
      },
      now() + 300,
    );
    let url = self.build_url(key);
    self
      .client
      .patch(url)
      .query(&[
        ("offset", offset.to_string()),
        ("object_id", creation.object_id.to_string()),
        ("upload_id", creation.upload_id.to_string()),
        ("t", t),
      ])
      .body(data)
      .send()
      .await?
      .error_for_status()?;
    Ok(())
  }
}