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

use super::StorageZone;
use crate::client::Client;
use crate::error::{APIResult, error_from_response};
use crate::files::{file_content, checksum, Error as FileError, urlencode_path};
use std::path::Path;
use reqwest::Method;

/// Errors that may occur when uploading files
#[derive(Debug, thiserror::Error)]
pub enum Error {
    /// Error while processing a file.
    #[error(transparent)]
    File(#[from] FileError),
}

fn get_url(storage_zone: StorageZone, zone_name: &str, dest: &str) -> String {
    let dest = dest.strip_prefix('/').unwrap_or(dest);
    let dest = urlencode_path(dest);
    format!("https://{}/{}/{}", storage_zone.url(), zone_name, dest)
}

async fn upload_data(client: &Client, url: String, content: Vec<u8>) -> APIResult<(), Error> {
    let hash = checksum(&content);
    let request = client
        .put(&url)
        .header("Checksum", hash)
        .header("content-type", "application/octet-stream")
        .body(content);

    let resp = request.send()
        .await
        .map_err(crate::Error::map_request_err(
            Method::PUT,
            url.clone(),
        ))?;

    error_from_response(resp)
        .await
        .map(|_| ())
        .map_err(crate::Error::map_response_err(Method::PUT, url))
}

/// Upload a file to the given Storage Zone by local path.
///
/// # Errors
///
/// - I/O errors while reading the file
/// - See also: [`upload_file_data`]
#[allow(clippy::module_name_repetitions)]
pub async fn upload_file_by_path(client: &Client, storage_zone: StorageZone, zone_name: &str, dest: &str, file: &Path) -> APIResult<(), Error> {
    let url = get_url(storage_zone, zone_name, dest);
    let content = file_content(file).await
        .map_err(Error::from)
        .map_err(crate::Error::map_err(
            Method::PUT,
            url.clone(),
        ))?;

    upload_data(client, url, content).await
}

/// Upload bytes to the given Storage Zone.
///
/// # Errors
///
/// - Failure to send the request
/// - Error response from the server
#[allow(clippy::module_name_repetitions)]
pub async fn upload_file_data(client: &Client, storage_zone: StorageZone, zone_name: &str, dest: &str, content: Vec<u8>) -> APIResult<(), Error> {
    let url = get_url(storage_zone, zone_name, dest);
    upload_data(client, url, content).await
}