-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfilesystem_api.rs
More file actions
229 lines (217 loc) · 7.88 KB
/
filesystem_api.rs
File metadata and controls
229 lines (217 loc) · 7.88 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
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
use std::path::PathBuf;
use eyre::{Result, eyre};
use crate::{
client::FirecrestClient,
types::{
DownloadFileResponse, DownloadFileResponseTransferDirectives, GetDirectoryLsResponse, GetFileStatResponse,
GetFileTailResponse, PostFileDownloadRequest, PostFileDownloadRequestTransferDirectives, PostFileUploadRequest,
PostMakeDirRequest, PostMkdirResponse, PutFileChmodRequest, PutFileChmodResponse, S3TransferRequest,
S3TransferResponse, UploadFileResponse,
},
};
pub async fn get_filesystem_ops_ls(
client: &FirecrestClient,
system_name: &str,
path: PathBuf,
) -> Result<GetDirectoryLsResponse> {
let path = path.as_os_str().to_str().ok_or(eyre!("couldn't cast path to string"))?;
let response = client
.get(
format!("filesystem/{system_name}/ops/ls").as_str(),
Some(vec![("path", path)]),
)
.await?;
let model: GetDirectoryLsResponse = serde_json::from_str(response.as_str())?;
Ok(model)
}
pub async fn get_filesystem_ops_stat(
client: &FirecrestClient,
system_name: &str,
path: PathBuf,
) -> Result<GetFileStatResponse> {
let path = path.as_os_str().to_str().ok_or(eyre!("couldn't cast path to string"))?;
let response = client
.get(
format!("filesystem/{system_name}/ops/stat").as_str(),
Some(vec![("path", path)]),
)
.await?;
let model: GetFileStatResponse = serde_json::from_str(response.as_str())?;
Ok(model)
}
pub async fn post_filesystem_ops_mkdir(
client: &FirecrestClient,
system_name: &str,
path: PathBuf,
) -> Result<PostMkdirResponse> {
let path = path
.into_os_string()
.into_string()
.map_err(|_| eyre!("couldn't convert path"))?;
let body = PostMakeDirRequest {
parent: Some(true),
source_path: Some(path),
};
let body_json = serde_json::to_string(&body)?;
let response = client
.post(
format!("filesystem/{system_name}/ops/mkdir").as_str(),
body_json,
None,
None,
)
.await?;
let model: PostMkdirResponse = serde_json::from_str(response.as_str())?;
Ok(model)
}
pub async fn put_filesystem_ops_chmod(
client: &FirecrestClient,
system_name: &str,
path: PathBuf,
mode: &str,
) -> Result<PutFileChmodResponse> {
let body = PutFileChmodRequest {
source_path: Some(
path.into_os_string()
.into_string()
.map_err(|_| eyre!("couldn't convert path"))?,
),
mode: mode.to_owned(),
};
let body_json = serde_json::to_string(&body)?;
let response = client
.put(format!("filesystem/{system_name}/ops/chmod").as_str(), body_json, None)
.await?;
let model: PutFileChmodResponse = serde_json::from_str(response.as_str())?;
Ok(model)
}
pub async fn get_filesystem_ops_tail(
client: &FirecrestClient,
system_name: &str,
path: PathBuf,
lines: usize,
) -> Result<GetFileTailResponse> {
let path = path.as_os_str().to_str().ok_or(eyre!("couldn't cast path to string"))?;
let response = client
.get(
format!("filesystem/{system_name}/ops/tail").as_str(),
Some(vec![("path", path), ("lines", &lines.to_string())]),
)
.await?;
let model: GetFileTailResponse = serde_json::from_str(response.as_str())?;
Ok(model)
}
pub async fn post_filesystem_ops_upload(
client: &FirecrestClient,
system_name: &str,
path: PathBuf,
file: Vec<u8>,
) -> Result<()> {
let folder = path
.parent()
.ok_or(eyre!("couldn't get parent folder"))?
.as_os_str()
.to_str()
.ok_or(eyre!("couldn't cast parent folder to string"))?;
let filename = path
.file_name()
.ok_or(eyre!("couldn't get file name"))?
.to_str()
.ok_or(eyre!("couldn't cast file name to string"))?;
let _ = client
.post(
format!("filesystem/{system_name}/ops/upload").as_str(),
"".to_owned(),
Some(vec![("path", folder)]),
Some(("file", (filename, file))),
)
.await?;
Ok(())
}
pub async fn post_filesystem_transfer_upload(
client: &FirecrestClient,
system_name: &str,
account: Option<String>,
path: PathBuf,
size: i64,
) -> Result<UploadFileResponse> {
let file_path = path.as_os_str().to_str().ok_or(eyre!("couldn't cast path to string"))?;
let body = PostFileUploadRequest {
source_path: Some(file_path.to_owned()),
transfer_directives: PostFileDownloadRequestTransferDirectives::S3(S3TransferRequest {
file_size: Some(size),
..Default::default()
}),
account,
};
let body_json = serde_json::to_string(&body)?;
let response = client
.post(
format!("filesystem/{system_name}/transfer/upload").as_str(),
body_json,
None,
None,
)
.await?;
let json_data: serde_json::Value = serde_json::from_str(response.as_str())?;
let mut model: UploadFileResponse = serde_json::from_str(response.as_str())?;
// deserializing of contained enum does not work properly as the fields are wrong, we do it here manually
let transfer_json = json_data["transferDirectives"].clone();
let transfer_dir = DownloadFileResponseTransferDirectives::S3(S3TransferResponse {
complete_upload_url: transfer_json["complete_upload_url"].as_str().map(|s| s.to_owned()),
download_url: transfer_json["download_url"].as_str().map(|s| s.to_owned()),
max_part_size: transfer_json["max_part_size"].as_i64(),
parts_upload_urls: transfer_json["parts_upload_urls"]
.as_array()
.map(|v| v.iter().flat_map(|u| u.as_str().map(|s| s.to_owned())).collect()),
transfer_method: "s3".to_owned(),
});
model.transfer_directives = transfer_dir;
Ok(model)
}
pub async fn get_filesystem_ops_download(client: &FirecrestClient, system_name: &str, path: PathBuf) -> Result<String> {
let file_path = path.as_os_str().to_str().ok_or(eyre!("couldn't cast path to string"))?;
let response = client
.get(
format!("filesystem/{system_name}/ops/download").as_str(),
Some(vec![("path", file_path)]),
)
.await?;
Ok(response.as_str().to_string())
}
pub async fn post_filesystem_transfer_download(
client: &FirecrestClient,
system_name: &str,
account: Option<String>,
path: PathBuf,
) -> Result<DownloadFileResponse> {
let file_path = path.as_os_str().to_str().ok_or(eyre!("couldn't cast path to string"))?;
let body = PostFileDownloadRequest {
source_path: Some(file_path.to_owned()),
transfer_directives: PostFileDownloadRequestTransferDirectives::S3(S3TransferRequest::default()),
account,
};
let body_json = serde_json::to_string(&body)?;
let response = client
.post(
format!("filesystem/{system_name}/transfer/download").as_str(),
body_json,
None,
None,
)
.await?;
let mut model: DownloadFileResponse = serde_json::from_str(response.as_str())?;
let json_data: serde_json::Value = serde_json::from_str(response.as_str())?;
let transfer_json = json_data["transferDirectives"].clone();
let transfer_dir = DownloadFileResponseTransferDirectives::S3(S3TransferResponse {
complete_upload_url: transfer_json["complete_upload_url"].as_str().map(|s| s.to_owned()),
download_url: transfer_json["download_url"].as_str().map(|s| s.to_owned()),
max_part_size: transfer_json["max_part_size"].as_i64(),
parts_upload_urls: transfer_json["parts_upload_urls"]
.as_array()
.map(|v| v.iter().flat_map(|u| u.as_str().map(|s| s.to_owned())).collect()),
transfer_method: "s3".to_owned(),
});
model.transfer_directives = transfer_dir;
Ok(model)
}