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
/// The types a bucket can have
///
/// Note that 'Snapshot' cannot be created via b2_create_bucket or b2_update_bucketuse serde::{Deserialize, Serialize};
use serde::{Deserialize, Serialize};

#[derive(Deserialize, Serialize, Debug, Clone, Eq, PartialEq, Ord, PartialOrd)]
#[serde(rename_all = "camelCase")]
pub enum B2BucketType {
    AllPublic,
    AllPrivate,
    Snapshot,
}

/// Represents a 'Bucket' on B2
///
/// API response from 'b2_create_bucket', 'b2_update_bucket', 'b2_delete_bucket' and 'b2_list_buckets'
#[derive(Deserialize, Serialize, Debug, Clone, Eq, PartialEq, Ord, PartialOrd)]
#[serde(rename_all = "camelCase")]
pub struct BucketResult {
    pub account_id: String,
    pub bucket_id: String,
    pub bucket_name: String,
    pub bucket_type: B2BucketType,
}

/// Represents a file on B2
///
/// API response from 'b2_upload_file' and 'b2_hide_file', 'b2_list_file_names' and 'b2_list_file_versions'
#[derive(Deserialize, Serialize, Debug, Clone, Eq)]
#[serde(rename_all = "camelCase")]
pub struct B2FileInfo {
    pub account_id: String,
    pub action: String,
    pub bucket_id: String,
    pub content_length: u64,
    pub content_sha1: Option<String>,
    pub content_type: Option<String>,
    pub file_id: Option<String>,
    pub file_info: Option<HashMap<String, String>>,
    pub file_name: String,
    pub upload_timestamp: u64,
}

/// Compares by the file_name value
impl Ord for B2FileInfo {
    fn cmp(&self, other: &B2FileInfo) -> Ordering {
        self.file_name.cmp(&other.file_name)
    }
}

impl PartialOrd for B2FileInfo {
    fn partial_cmp(&self, other: &B2FileInfo) -> Option<Ordering> {
        Some(self.cmp(&other))
    }
}

/// Compares by the file_name value
impl PartialEq for B2FileInfo {
    fn eq(&self, other: &B2FileInfo) -> bool {
        self.file_name == other.file_name
    }
}

impl B2FileInfo {
    /// Returns the modified timestamp of the file
    /// If it wasn't supplied during upload, this will return 0
    pub fn modified(&self) -> u64 {
        match &self.file_info {
            Some(fi) => match fi.get("src_last_modified_millis") {
                Some(s) => s.parse::<u64>().unwrap_or(0),
                None => 0,
            },
            None => 0,
        }
    }
}

// Export API calls
mod b2_authorize_account;
pub use self::b2_authorize_account::*;

mod b2_create_bucket;
pub use self::b2_create_bucket::*;
mod b2_update_bucket;
pub use self::b2_update_bucket::*;
mod b2_delete_bucket;
pub use self::b2_delete_bucket::*;
mod b2_list_buckets;
pub use self::b2_list_buckets::*;

mod b2_list_file_names;
pub use self::b2_list_file_names::*;
mod b2_get_file_info;
pub use self::b2_get_file_info::*;

mod b2_get_upload_url;
pub use self::b2_get_upload_url::*;
mod b2_upload_file;
pub use self::b2_upload_file::*;
mod b2_delete_file_version;
pub use self::b2_delete_file_version::*;
mod b2_hide_file;
pub use self::b2_hide_file::*;
use std::cmp::Ordering;

mod b2_get_download_authorization;
pub use self::b2_get_download_authorization::*;
mod b2_download_file_by_name;
pub use self::b2_download_file_by_name::*;
use std::collections::HashMap;