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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.

use std::fmt::Debug;
use std::fmt::Formatter;

use bytes::Buf;
use bytes::Bytes;
use http::header;
use http::request;
use http::Request;
use http::Response;
use http::StatusCode;
use serde::Deserialize;
use serde::Serialize;
use serde_json::json;

use self::constants::*;
use super::error::parse_error;
use crate::raw::*;
use crate::*;

pub(super) mod constants {
    // https://github.com/vercel/storage/blob/main/packages/blob/src/put.ts#L16
    // x-content-type specifies the MIME type of the file being uploaded.
    pub const X_VERCEL_BLOB_CONTENT_TYPE: &str = "x-content-type";
    // x-add-random-suffix specifying whether to  add a random suffix to the pathname
    // Default value is 1, which means to add a random suffix.
    // Set it to 0 to disable the random suffix.
    pub const X_VERCEL_BLOB_ADD_RANDOM_SUFFIX: &str = "x-add-random-suffix";
    // https://github.com/vercel/storage/blob/main/packages/blob/src/put-multipart.ts#L84
    // x-mpu-action specifies the action to perform on the MPU.
    // Possible values are:
    // - create: create a new MPU.
    // - upload: upload a part to an existing MPU.
    // - complete: complete an existing MPU.
    pub const X_VERCEL_BLOB_MPU_ACTION: &str = "x-mpu-action";
    pub const X_VERCEL_BLOB_MPU_KEY: &str = "x-mpu-key";
    pub const X_VERCEL_BLOB_MPU_PART_NUMBER: &str = "x-mpu-part-number";
    pub const X_VERCEL_BLOB_MPU_UPLOAD_ID: &str = "x-mpu-upload-id";
}

#[derive(Clone)]
pub struct VercelBlobCore {
    /// The root of this core.
    pub root: String,
    /// Vercel Blob token.
    pub token: String,

    pub client: HttpClient,
}

impl Debug for VercelBlobCore {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Backend")
            .field("root", &self.root)
            .finish_non_exhaustive()
    }
}

impl VercelBlobCore {
    #[inline]
    pub async fn send(&self, req: Request<Buffer>) -> Result<Response<Buffer>> {
        self.client.send(req).await
    }

    pub fn sign(&self, req: request::Builder) -> request::Builder {
        req.header(header::AUTHORIZATION, format!("Bearer {}", self.token))
    }
}

impl VercelBlobCore {
    pub async fn download(
        &self,
        path: &str,
        range: BytesRange,
        _: &OpRead,
    ) -> Result<Response<HttpBody>> {
        let p = build_abs_path(&self.root, path);
        // Vercel blob use an unguessable random id url to download the file
        // So we use list to get the url of the file and then use it to download the file
        let resp = self.list(&p, Some(1)).await?;

        // Use the mtach url to download the file
        let url = resolve_blob(resp.blobs, p);

        if url.is_empty() {
            return Err(Error::new(ErrorKind::NotFound, "Blob not found"));
        }

        let mut req = Request::get(url);

        if !range.is_full() {
            req = req.header(header::RANGE, range.to_header());
        }

        // Set body
        let req = req.body(Buffer::new()).map_err(new_request_build_error)?;

        self.client.fetch(req).await
    }

    pub fn get_put_request(
        &self,
        path: &str,
        size: Option<u64>,
        args: &OpWrite,
        body: Buffer,
    ) -> Result<Request<Buffer>> {
        let p = build_abs_path(&self.root, path);

        let url = format!(
            "https://blob.vercel-storage.com/{}",
            percent_encode_path(&p)
        );

        let mut req = Request::put(&url);

        req = req.header(X_VERCEL_BLOB_ADD_RANDOM_SUFFIX, "0");

        if let Some(size) = size {
            req = req.header(header::CONTENT_LENGTH, size.to_string())
        }

        if let Some(mime) = args.content_type() {
            req = req.header(X_VERCEL_BLOB_CONTENT_TYPE, mime)
        }

        let req = self.sign(req);

        // Set body
        let req = req.body(body).map_err(new_request_build_error)?;

        Ok(req)
    }

    pub async fn head(&self, path: &str) -> Result<Response<Buffer>> {
        let p = build_abs_path(&self.root, path);

        let resp = self.list(&p, Some(1)).await?;

        let url = resolve_blob(resp.blobs, p);

        if url.is_empty() {
            return Err(Error::new(ErrorKind::NotFound, "Blob not found"));
        }

        let req = Request::get(format!(
            "https://blob.vercel-storage.com?url={}",
            percent_encode_path(&url)
        ));

        let req = self.sign(req);

        // Set body
        let req = req.body(Buffer::new()).map_err(new_request_build_error)?;

        self.send(req).await
    }

    pub async fn copy(&self, from: &str, to: &str) -> Result<Response<Buffer>> {
        let from = build_abs_path(&self.root, from);

        let resp = self.list(&from, Some(1)).await?;

        let from_url = resolve_blob(resp.blobs, from);

        if from_url.is_empty() {
            return Err(Error::new(ErrorKind::NotFound, "Blob not found"));
        }

        let to = build_abs_path(&self.root, to);

        let to_url = format!(
            "https://blob.vercel-storage.com/{}?fromUrl={}",
            percent_encode_path(&to),
            percent_encode_path(&from_url),
        );

        let req = Request::put(&to_url);

        let req = self.sign(req);

        // Set body
        let req = req.body(Buffer::new()).map_err(new_request_build_error)?;

        self.send(req).await
    }

    pub async fn list(&self, prefix: &str, limit: Option<usize>) -> Result<ListResponse> {
        let prefix = if prefix == "/" { "" } else { prefix };

        let mut url = format!(
            "https://blob.vercel-storage.com?prefix={}",
            percent_encode_path(prefix)
        );

        if let Some(limit) = limit {
            url.push_str(&format!("&limit={}", limit))
        }

        let req = Request::get(&url);

        let req = self.sign(req);

        // Set body
        let req = req.body(Buffer::new()).map_err(new_request_build_error)?;

        let resp = self.send(req).await?;

        let status = resp.status();

        if status != StatusCode::OK {
            return Err(parse_error(resp));
        }

        let body = resp.into_body();

        let resp: ListResponse =
            serde_json::from_reader(body.reader()).map_err(new_json_deserialize_error)?;

        Ok(resp)
    }

    pub async fn initiate_multipart_upload(
        &self,
        path: &str,
        args: &OpWrite,
    ) -> Result<Response<Buffer>> {
        let p = build_abs_path(&self.root, path);

        let url = format!(
            "https://blob.vercel-storage.com/mpu/{}",
            percent_encode_path(&p)
        );

        let req = Request::post(&url);

        let mut req = self.sign(req);

        req = req.header(X_VERCEL_BLOB_MPU_ACTION, "create");
        req = req.header(X_VERCEL_BLOB_ADD_RANDOM_SUFFIX, "0");

        if let Some(mime) = args.content_type() {
            req = req.header(X_VERCEL_BLOB_CONTENT_TYPE, mime);
        };

        // Set body
        let req = req.body(Buffer::new()).map_err(new_request_build_error)?;

        self.send(req).await
    }

    pub async fn upload_part(
        &self,
        path: &str,
        upload_id: &str,
        part_number: usize,
        size: u64,
        body: Buffer,
    ) -> Result<Response<Buffer>> {
        let p = build_abs_path(&self.root, path);

        let url = format!(
            "https://blob.vercel-storage.com/mpu/{}",
            percent_encode_path(&p)
        );

        let mut req = Request::post(&url);

        req = req.header(header::CONTENT_LENGTH, size);
        req = req.header(X_VERCEL_BLOB_MPU_ACTION, "upload");
        req = req.header(X_VERCEL_BLOB_MPU_KEY, p);
        req = req.header(X_VERCEL_BLOB_MPU_UPLOAD_ID, upload_id);
        req = req.header(X_VERCEL_BLOB_MPU_PART_NUMBER, part_number);

        let req = self.sign(req);

        // Set body
        let req = req.body(body).map_err(new_request_build_error)?;

        self.send(req).await
    }

    pub async fn complete_multipart_upload(
        &self,
        path: &str,
        upload_id: &str,
        parts: Vec<Part>,
    ) -> Result<Response<Buffer>> {
        let p = build_abs_path(&self.root, path);

        let url = format!(
            "https://blob.vercel-storage.com/mpu/{}",
            percent_encode_path(&p)
        );

        let mut req = Request::post(&url);

        req = req.header(X_VERCEL_BLOB_MPU_ACTION, "complete");
        req = req.header(X_VERCEL_BLOB_MPU_KEY, p);
        req = req.header(X_VERCEL_BLOB_MPU_UPLOAD_ID, upload_id);

        let req = self.sign(req);

        let parts_json = json!(parts);

        let req = req
            .header(header::CONTENT_TYPE, "application/json")
            .body(Buffer::from(Bytes::from(parts_json.to_string())))
            .map_err(new_request_build_error)?;

        self.send(req).await
    }
}

pub fn parse_blob(blob: &Blob) -> Result<Metadata> {
    let mode = if blob.pathname.ends_with('/') {
        EntryMode::DIR
    } else {
        EntryMode::FILE
    };
    let mut md = Metadata::new(mode);
    if let Some(content_type) = blob.content_type.clone() {
        md.set_content_type(&content_type);
    }
    md.set_content_length(blob.size);
    md.set_last_modified(parse_datetime_from_rfc3339(&blob.uploaded_at)?);
    md.set_content_disposition(&blob.content_disposition);
    Ok(md)
}

fn resolve_blob(blobs: Vec<Blob>, path: String) -> String {
    for blob in blobs {
        if blob.pathname == path {
            return blob.url;
        }
    }
    "".to_string()
}

#[derive(Default, Debug, Clone, PartialEq, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ListResponse {
    pub cursor: Option<String>,
    pub has_more: bool,
    pub blobs: Vec<Blob>,
}

#[derive(Default, Debug, Clone, PartialEq, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Blob {
    pub url: String,
    pub pathname: String,
    pub size: u64,
    pub uploaded_at: String,
    pub content_disposition: String,
    pub content_type: Option<String>,
}

#[derive(Default, Debug, Clone, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Part {
    pub part_number: usize,
    pub etag: String,
}

#[derive(Default, Debug, Clone, PartialEq, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct InitiateMultipartUploadResponse {
    pub upload_id: String,
    pub key: String,
}

#[derive(Default, Debug, Clone, PartialEq, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct UploadPartResponse {
    pub etag: String,
}