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
use std::collections::HashMap;
use std::fs;
use std::io;
use std::ops::Bound;
use std::path::Path;

use anyhow::Result;

use async_trait::async_trait;

use chrono::{DateTime, Utc};

use futures::Stream;

use serde::{Deserialize, Serialize};

use warp::hyper::body::Bytes;

/// The type of a blob in a menmos cluster (file or directory).
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
pub enum Type {
    File,
    Directory,
}

fn file_to_base64<P: AsRef<Path>>(path: P) -> Result<String> {
    Ok(base64::encode(fs::read(path.as_ref())?))
}

#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
#[serde(deny_unknown_fields)]
pub struct CertificateInfo {
    pub certificate_b64: String,
    pub private_key_b64: String,
}

impl CertificateInfo {
    pub fn from_path<P: AsRef<Path>, Q: AsRef<Path>>(
        certificate_path: P,
        private_key_path: Q,
    ) -> Result<CertificateInfo> {
        Ok(Self {
            certificate_b64: file_to_base64(certificate_path)?,
            private_key_b64: file_to_base64(private_key_path)?,
        })
    }
}

/// Metadata accepted when indexing a blob.
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
#[serde(deny_unknown_fields)]
pub struct BlobMetaRequest {
    /// The name of this blob. Does not need to be unique.
    pub name: String,

    /// The type of this blob.
    pub blob_type: Type,

    /// The key/value pairs for this blob.
    pub metadata: HashMap<String, String>,

    /// The tags for this blob.
    pub tags: Vec<String>,

    /// This blob's parent IDs.
    pub parents: Vec<String>,

    /// This blob's size, in bytes.
    pub size: u64,
}

impl BlobMetaRequest {
    pub fn new<S: Into<String>>(name: S, blob_type: Type) -> Self {
        Self {
            name: name.into(),
            blob_type,
            metadata: Default::default(),
            tags: Default::default(),
            parents: Default::default(),
            size: 0,
        }
    }

    pub fn file<S: Into<String>>(name: S) -> Self {
        Self::new(name, Type::File)
    }

    pub fn directory<S: Into<String>>(name: S) -> Self {
        Self::new(name, Type::Directory)
    }

    pub fn with_meta<S: Into<String>, T: Into<String>>(mut self, key: S, value: T) -> Self {
        self.metadata.insert(key.into(), value.into());
        self
    }

    pub fn with_tag<S: Into<String>>(mut self, s: S) -> Self {
        self.tags.push(s.into());
        self
    }

    pub fn with_parent<S: Into<String>>(mut self, s: S) -> Self {
        self.parents.push(s.into());
        self
    }

    pub fn with_size(mut self, size: u64) -> Self {
        self.size = size;
        self
    }

    pub fn into_meta(self, created_at: DateTime<Utc>, modified_at: DateTime<Utc>) -> BlobMeta {
        BlobMeta {
            name: self.name,
            blob_type: self.blob_type,
            metadata: self.metadata,
            tags: self.tags,
            parents: self.parents,
            size: self.size,
            created_at,
            modified_at,
        }
    }
}

/// Metadata associated with a blob.
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
#[serde(deny_unknown_fields)]
pub struct BlobMeta {
    /// The name of this blob. Does not need to be unique.
    pub name: String,

    /// The type of this blob.
    pub blob_type: Type,

    /// The key/value pairs for this blob.
    pub metadata: HashMap<String, String>,

    /// The tags for this blob.
    pub tags: Vec<String>,

    /// This blob's parent IDs.
    pub parents: Vec<String>,

    /// This blob's size, in bytes.
    pub size: u64,

    /// This blob's creation time.
    pub created_at: DateTime<Utc>,

    /// This blob's last modified time.
    pub modified_at: DateTime<Utc>,
}

impl From<BlobMeta> for BlobMetaRequest {
    fn from(m: BlobMeta) -> Self {
        Self {
            name: m.name,
            blob_type: m.blob_type,
            metadata: m.metadata,
            tags: m.tags,
            parents: m.parents,
            size: m.size,
        }
    }
}

impl BlobMeta {
    pub fn new<S: Into<String>>(name: S, blob_type: Type) -> Self {
        Self {
            name: name.into(),
            blob_type,
            metadata: Default::default(),
            tags: Default::default(),
            parents: Default::default(),
            size: 0,
            created_at: Utc::now(),
            modified_at: Utc::now(),
        }
    }

    pub fn file<S: Into<String>>(name: S) -> Self {
        BlobMeta::new(name, Type::File)
    }

    pub fn directory<S: Into<String>>(name: S) -> Self {
        BlobMeta::new(name, Type::Directory)
    }

    pub fn with_meta<S: Into<String>, T: Into<String>>(mut self, key: S, value: T) -> Self {
        self.metadata.insert(key.into(), value.into());
        self
    }

    pub fn with_tag<S: Into<String>>(mut self, s: S) -> Self {
        self.tags.push(s.into());
        self
    }

    pub fn with_parent<S: Into<String>>(mut self, s: S) -> Self {
        self.parents.push(s.into());
        self
    }

    pub fn with_size(mut self, size: u64) -> Self {
        self.size = size;
        self
    }
}

#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
#[serde(deny_unknown_fields)]
pub struct BlobInfoRequest {
    pub meta_request: BlobMetaRequest,
    pub owner: String,
}

impl BlobInfoRequest {
    pub fn into_blob_info(self, created_at: DateTime<Utc>, modified_at: DateTime<Utc>) -> BlobInfo {
        BlobInfo {
            meta: self.meta_request.into_meta(created_at, modified_at),
            owner: self.owner,
        }
    }
}

#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
#[serde(deny_unknown_fields)]
pub struct BlobInfo {
    pub meta: BlobMeta,
    pub owner: String,
}

pub struct Blob {
    pub stream: Box<dyn Stream<Item = Result<Bytes, io::Error>> + Send + Sync>,
    pub current_chunk_size: u64,
    pub total_blob_size: u64,
    pub info: BlobInfo,
}

#[async_trait]
pub trait StorageNode {
    async fn put(
        &self,
        id: String,
        info: BlobInfoRequest,
        stream: Option<Box<dyn Stream<Item = Result<Bytes, io::Error>> + Send + Sync + Unpin>>,
    ) -> Result<()>;

    async fn write(
        &self,
        id: String,
        range: (Bound<u64>, Bound<u64>),
        bytes: Bytes,
        username: &str,
    ) -> Result<()>;

    async fn get(&self, blob_id: String, range: Option<(Bound<u64>, Bound<u64>)>) -> Result<Blob>;

    async fn update_meta(&self, blob_id: String, info: BlobInfoRequest) -> Result<()>;

    async fn delete(&self, blob_id: String, username: &str) -> Result<()>;

    async fn get_certificates(&self) -> Option<CertificateInfo>;

    async fn fsync(&self, blob_id: String, username: &str) -> Result<()>;

    async fn flush(&self) -> Result<()>;
}