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
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 futures::Stream;
use serde::{Deserialize, Serialize};
use warp::hyper::body::Bytes;
#[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)?,
})
}
}
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
#[serde(deny_unknown_fields)]
pub struct BlobMeta {
pub name: String,
pub blob_type: Type,
pub metadata: HashMap<String, String>,
pub tags: Vec<String>,
pub parents: Vec<String>,
pub size: u64,
}
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,
}
}
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 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: BlobInfo,
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: BlobInfo) -> 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<()>;
}