-
Notifications
You must be signed in to change notification settings - Fork 82
Expand file tree
/
Copy pathfiles.rs
More file actions
175 lines (147 loc) · 5 KB
/
Copy pathfiles.rs
File metadata and controls
175 lines (147 loc) · 5 KB
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
use std::{fmt, fs, io::Write, path::PathBuf, time::Duration};
use actix_multipart::Field;
use futures::StreamExt;
use opendal::{services::S3, Operator};
use crate::{appstate::AppState, config::Config, errors::AtomicServerResult};
#[derive(Clone, Debug, PartialEq)]
pub enum FileStore {
S3(S3Config),
FS(FSConfig),
}
#[derive(Clone, Debug, PartialEq)]
pub struct S3Config {
pub bucket: String,
pub path: String,
pub endpoint: Option<String>,
pub region: Option<String>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct FSConfig {
pub path: PathBuf,
}
impl FileStore {
const S3_PREFIX: &'static str = "s3:";
const FS_PREFIX: &'static str = "fs:";
pub fn init_fs_from_config(config: &Config) -> FileStore {
FileStore::FS(FSConfig {
path: config.uploads_path.clone(),
})
}
pub fn init_from_config(config: &Config, fs_file_store: FileStore) -> FileStore {
let opts = &config.opts;
if let Some(bucket) = &opts.s3_bucket {
let config = S3Config {
bucket: bucket.clone(),
endpoint: opts.s3_endpoint.clone(),
region: opts.s3_region.clone(),
path: opts.s3_path.clone().unwrap_or("uploads".to_string()),
};
FileStore::S3(config)
} else {
fs_file_store
}
}
pub fn get_subject_file_store<'a>(appstate: &'a AppState, subject: &str) -> &'a FileStore {
if subject.contains(Self::S3_PREFIX) {
&appstate.file_store
} else {
&appstate.fs_file_store
}
}
pub fn get_fs_file_path(&self, file_id: &str) -> AtomicServerResult<PathBuf> {
tracing::info!("fs_file_path: {}", file_id);
if let FileStore::FS(config) = self {
let fs_file_id = file_id.strip_prefix(Self::FS_PREFIX).unwrap_or(file_id);
let mut file_path = config.path.clone();
file_path.push(fs_file_id);
Ok(file_path)
} else {
Err("Wrong FileStore passed to get_fs_file_path".into())
}
}
pub fn prefix(&self) -> &str {
match self {
Self::S3(_) => Self::S3_PREFIX,
Self::FS(_) => Self::FS_PREFIX,
}
}
pub fn encoded(&self) -> String {
urlencoding::encode(self.prefix()).into_owned()
}
pub async fn upload_file(&self, file_id: &str, field: Field) -> AtomicServerResult<i64> {
match self {
FileStore::S3(_) => s3_upload(self, file_id, field).await,
FileStore::FS(config) => fs_upload(self, config, file_id, field).await,
}
}
}
impl fmt::Display for FileStore {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.prefix())
}
}
async fn fs_upload(
file_store: &FileStore,
config: &FSConfig,
file_id: &str,
mut field: Field,
) -> AtomicServerResult<i64> {
std::fs::create_dir_all(config.path.clone())?;
let mut file = fs::File::create(file_store.get_fs_file_path(file_id)?)?;
let byte_count: i64 = file
.metadata()?
.len()
.try_into()
.map_err(|_e| "Too large")?;
// Field in turn is stream of *Bytes* object
while let Some(chunk) = field.next().await {
let data = chunk.map_err(|e| format!("Error while reading multipart data. {}", e))?;
// TODO: Update a SHA256 hash here for checksum
file.write_all(&data)?;
}
Ok(byte_count)
}
async fn s3_upload(
file_store: &FileStore,
file_id: &str,
mut field: Field,
) -> AtomicServerResult<i64> {
let mut builder = S3::default();
if let FileStore::S3(config) = file_store {
builder.bucket(&config.bucket);
builder.root(&config.path);
config.region.as_ref().map(|r| builder.region(r));
config.endpoint.as_ref().map(|e| builder.endpoint(e));
} else {
return Err("Uploading to S3 but no S3 config provided".into());
}
let op: Operator = Operator::new(builder)?.finish();
let mut w = op.writer(file_id).await?;
let mut len = 0;
while let Some(chunk) = field.next().await {
let data = chunk.map_err(|e| format!("Error while reading multipart data. {}", e))?;
len += data.len();
w.write(data).await?;
}
let byte_length: i64 = len.try_into().map_err(|_e| "Too large")?;
w.close().await?;
Ok(byte_length)
}
pub async fn get_s3_signed_url(
file_store: &FileStore,
duration: Duration,
file_id: &str,
) -> AtomicServerResult<String> {
let mut builder = S3::default();
if let FileStore::S3(config) = file_store {
builder.bucket(&config.bucket);
builder.root(&config.path);
config.region.as_ref().map(|r| builder.region(r));
config.endpoint.as_ref().map(|e| builder.endpoint(e));
} else {
return Err("Downloading from S3 but no S3 config provided".into());
}
let op: Operator = Operator::new(builder)?.finish();
let uri = op.presign_read(file_id, duration).await?.uri().to_string();
Ok(uri)
}