This repository has been archived by the owner on Oct 23, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 166
/
Copy pathadd.rs
417 lines (349 loc) · 15.7 KB
/
add.rs
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
use super::AddArgs;
use crate::v0::support::StringError;
use bytes::{Buf, BufMut, Bytes, BytesMut};
use cid::Cid;
use futures::stream::{Stream, StreamExt, TryStreamExt};
use ipfs::unixfs::ll::{
dir::builder::{
BufferingTreeBuilder, TreeBuildingFailed, TreeConstructionFailed, TreeNode, TreeOptions,
},
file::adder::FileAdder,
};
use ipfs::{Block, Ipfs, IpfsTypes};
use mime::Mime;
use mpart_async::server::{MultipartError, MultipartStream};
use serde::Serialize;
use std::borrow::Cow;
use std::fmt;
use warp::{Rejection, Reply};
pub(super) async fn add_inner<T: IpfsTypes>(
ipfs: Ipfs<T>,
opts: AddArgs,
content_type: Mime,
body: impl Stream<Item = Result<impl Buf, warp::Error>> + Send + Unpin + 'static,
) -> Result<impl Reply, Rejection> {
let boundary = content_type
.get_param("boundary")
.map(|v| v.to_string())
.ok_or_else(|| StringError::from("missing 'boundary' on content-type"))?;
let st = MultipartStream::new(
Bytes::from(boundary),
body.map_ok(|mut buf| buf.copy_to_bytes(buf.remaining())),
);
let st = add_stream(ipfs, st, opts);
// map the errors into json objects; as we can't return them as trailers yet
let st = st.map(|res| match res {
passthrough @ Ok(_) | passthrough @ Err(AddError::ResponseSerialization(_)) => {
// there is nothing we should do or could do for these; the assumption is that hyper
// will send the bytes and stop on serialization error and log it. the response
// *should* be closed on the error.
passthrough
}
Err(something_else) => {
let msg = crate::v0::support::MessageResponseBuilder::default()
.with_message(something_else.to_string());
let bytes: Bytes = serde_json::to_vec(&msg)
.expect("serializing here should not have failed")
.into();
let crlf = Bytes::from(&b"\r\n"[..]);
// note that here we are assuming that the stream ends on error
let mut chained = bytes.chain(crlf);
Ok(chained.copy_to_bytes(chained.remaining()))
}
});
let body = crate::v0::support::StreamResponse(st);
Ok(body)
}
#[derive(Debug)]
enum AddError {
Parsing(MultipartError),
Header(MultipartError),
InvalidFilename(std::str::Utf8Error),
UnsupportedField(String),
UnsupportedContentType(String),
ResponseSerialization(serde_json::Error),
Persisting(ipfs::Error),
TreeGathering(TreeBuildingFailed),
TreeBuilding(TreeConstructionFailed),
}
impl From<MultipartError> for AddError {
fn from(e: MultipartError) -> AddError {
AddError::Parsing(e)
}
}
impl fmt::Display for AddError {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
use AddError::*;
match self {
Parsing(me) => write!(fmt, "invalid request body: {}", me),
Header(me) => write!(fmt, "invalid multipart header(s): {}", me),
InvalidFilename(e) => write!(fmt, "invalid multipart filename: {:?}", e),
UnsupportedField(name) => write!(fmt, "unsupported field name: {:?}", name),
UnsupportedContentType(t) => write!(fmt, "unsupported content-type: {:?} (supported: application/{{octet-stream,x-directory}})", t),
ResponseSerialization(e) => write!(fmt, "progress serialization failed: {}", e),
Persisting(e) => write!(fmt, "put_block failed: {}", e),
TreeGathering(g) => write!(fmt, "invalid directory tree: {}", g),
TreeBuilding(b) => write!(fmt, "constructed invalid directory tree: {}", b),
}
}
}
impl std::error::Error for AddError {}
fn add_stream<St, E>(
ipfs: Ipfs<impl IpfsTypes>,
mut fields: MultipartStream<St, E>,
opts: AddArgs,
) -> impl Stream<Item = Result<Bytes, AddError>> + Send + 'static
where
St: Stream<Item = Result<Bytes, E>> + Send + Unpin + 'static,
E: Into<anyhow::Error> + Send + Sync + 'static + std::error::Error,
{
async_stream::try_stream! {
let mut tree_opts = TreeOptions::default();
if opts.wrap_with_directory {
tree_opts.wrap_with_directory();
}
let mut tree = BufferingTreeBuilder::new(tree_opts);
let mut buffer = BytesMut::new();
while let Some(mut field) = fields
.try_next()
.await?
{
let field_name = field.name().map_err(AddError::Header)?;
let filename = field.filename().map_err(AddError::Header)?;
let filename = percent_encoding::percent_decode_str(filename)
.decode_utf8()
.map_err(AddError::InvalidFilename)?;
let filename = if let Some(relative) = filename.strip_prefix('/') {
// normalize single first slash; seems similar to what js-ipfs does: filesystem
// test cases post with paths '/some-directory/...' and others post with
// 'some-directory/...'.
// since slash is a single code point, we can just do
relative.to_owned()
} else {
filename.into_owned()
};
let content_type = field.content_type().map_err(AddError::Header)?;
let next = match content_type {
"application/octet-stream" => {
// files are of the form "file-{1,2,3,..}"
let _ = if field_name != "file" && !field_name.starts_with("file-") {
Err(AddError::UnsupportedField(field_name.to_string()))
} else {
Ok(())
}?;
let mut adder = FileAdder::default();
// how many bytes we have stored as blocks
let mut total_written = 0u64;
// how many bytes of input we have read
let mut total_read = 0u64;
loop {
let next = field
.try_next()
.await
.map_err(AddError::Parsing)?;
match next {
Some(next) => {
let (read, saved_any, written) = push_all(&ipfs, &mut adder, next).await?;
total_written += written;
total_read += read;
if saved_any && opts.progress {
// technically we could just send messages but that'd
// require us to stop using Cow's and use Arc<String> or
// similar. not especially fond of either.
serde_json::to_writer((&mut buffer).writer(), &Response::Progress {
name: Cow::Borrowed(&filename),
bytes: total_read,
}).map_err(AddError::ResponseSerialization)?;
buffer.put(&b"\r\n"[..]);
yield buffer.split().freeze();
}
}
None => break,
}
}
if opts.progress {
// in the interface-http-core tests the subtotal is expected to be full
// size, ordering w.r.t. to the "added" is not specified
serde_json::to_writer((&mut buffer).writer(), &Response::Progress {
name: Cow::Borrowed(&filename),
bytes: total_read,
}).map_err(AddError::ResponseSerialization)?;
buffer.put(&b"\r\n"[..]);
// it is not required to yield here so perhaps we just accumulate the next
// response in as well
}
let (root, subtotal) = import_all(&ipfs, adder.finish())
.await
.map_err(AddError::Persisting)?
// there was a bug in ipfs-unixfs however in general the "push" operation
// should flush so that the final finish would still have work to do.
.expect("there should always be something from finish");
total_written += subtotal;
tracing::trace!("completed processing file of {} bytes: {:?}", total_read, filename);
// using the filename as the path since we can tolerate a single empty named file
// however the second one will cause issues
tree.put_link(&filename, root.clone(), total_written)
.map_err(AddError::TreeGathering)?;
let filename: Cow<'_, str> = if filename.is_empty() {
// cid needs to be repeated if no filename was given; in which case there
// should not be anything to build as tree either. note that intentionally
// no such Cid repeating happens when building the tree and a new wrapping
// root will have empty filename in the progress report.
Cow::Owned(root.to_string())
} else {
Cow::Owned(filename)
};
serde_json::to_writer((&mut buffer).writer(), &Response::Added {
name: filename,
hash: Quoted(&root),
size: Quoted(total_written),
}).map_err(AddError::ResponseSerialization)?;
buffer.put(&b"\r\n"[..]);
Ok(buffer.split().freeze())
},
"application/x-directory" => {
// dirs are of the form "dir-{1,2,3,..}"
let _ = if field_name != "dir" && !field_name.starts_with("dir-") {
Err(AddError::UnsupportedField(field_name.to_string()))
} else {
Ok(())
}?;
// we need to fully consume this part, even though there shouldn't be anything
// except for the already parsed *but* ignored headers
while field.try_next().await.map_err(AddError::Parsing)?.is_some() {}
// while at the moment we don't parse the mtime, mtime-nsec headers and mode
// those should be reflected in the metadata. this will still add an empty
// directory which is a good thing.
tree.set_metadata(&filename, ipfs::unixfs::ll::Metadata::default())
.map_err(AddError::TreeGathering)?;
continue;
}
unsupported => {
Err(AddError::UnsupportedContentType(unsupported.to_string()))
}
}?;
yield next;
}
let mut iter = tree.build();
while let Some(res) = iter.next_borrowed() {
let TreeNode { path, cid, total_size, block } = res.map_err(AddError::TreeBuilding)?;
// shame we need to allocate once again here..
ipfs.put_block(Block { cid: cid.to_owned(), data: block.into() }).await.map_err(AddError::Persisting)?;
serde_json::to_writer((&mut buffer).writer(), &Response::Added {
name: Cow::Borrowed(path),
hash: Quoted(cid),
size: Quoted(total_size),
}).map_err(AddError::ResponseSerialization)?;
buffer.put(&b"\r\n"[..]);
yield buffer.split().freeze();
}
}
}
async fn push_all(
ipfs: &Ipfs<impl IpfsTypes>,
adder: &mut FileAdder,
next: Bytes,
) -> Result<(u64, bool, u64), AddError> {
let mut read = 0usize;
let mut saved_any = false;
let mut total_written = 0;
while read < next.len() {
let (iter, used) = adder.push(&next.slice(read..));
read += used;
let maybe_tuple = import_all(ipfs, iter).await.map_err(AddError::Persisting)?;
let subtotal = maybe_tuple.map(|t| t.1);
total_written += subtotal.unwrap_or(0);
saved_any |= subtotal.is_some();
}
Ok((read as u64, saved_any, total_written))
}
async fn import_all(
ipfs: &Ipfs<impl IpfsTypes>,
iter: impl Iterator<Item = (Cid, Vec<u8>)>,
) -> Result<Option<(Cid, u64)>, ipfs::Error> {
// TODO: use FuturesUnordered
let mut last: Option<Cid> = None;
let mut total = 0u64;
for (cid, data) in iter {
total += data.len() as u64;
let block = Block {
cid,
data: data.into_boxed_slice(),
};
let cid = ipfs.put_block(block).await?;
last = Some(cid);
}
Ok(last.map(|cid| (cid, total)))
}
/// The possible response messages from /add.
#[derive(Debug, Serialize)]
#[serde(untagged)] // rename_all="..." doesn't seem to work at this level
enum Response<'a> {
/// When progress=true query parameter has been given, this will be output every N bytes, or
/// perhaps every chunk.
Progress {
/// Probably the name of the file being added or empty if none was provided.
name: Cow<'a, str>,
/// Bytes processed since last progress; for a file, all progress reports must add up to
/// the total file size. Interestingly this should not be stringified with `Quoted`,
/// whereas the `Added::size` needs to be `Quoted`.
bytes: u64,
},
/// Output for every input item.
#[serde(rename_all = "PascalCase")]
Added {
/// The resulting Cid as a string.
hash: Quoted<&'a Cid>,
/// Name of the file added from filename or the resulting Cid.
name: Cow<'a, str>,
/// Stringified version of the total cumulative size in bytes.
size: Quoted<u64>,
},
}
#[derive(Debug)]
struct Quoted<D>(pub D);
impl<D: fmt::Display> serde::Serialize for Quoted<D> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serializer.collect_str(&self.0)
}
}
#[cfg(test)]
mod tests {
use crate::v0::root_files::add;
#[tokio::test]
async fn add_single_block_file() {
let ipfs = tokio_ipfs().await;
// this is from interface-ipfs-core, pretty much simplest add a buffer test case
// but the body content is from the pubsub test case I copied this from
let response = warp::test::request()
.path("/add")
.header(
"content-type",
"multipart/form-data; boundary=-----------------------------Z0oYi6XyTm7_x2L4ty8JL",
)
.body(
&b"-------------------------------Z0oYi6XyTm7_x2L4ty8JL\r\n\
Content-Disposition: form-data; name=\"file\"; filename=\"testfile.txt\"\r\n\
Content-Type: application/octet-stream\r\n\
\r\n\
Plz add me!\n\
\r\n-------------------------------Z0oYi6XyTm7_x2L4ty8JL--\r\n"[..],
)
.reply(&add(&ipfs))
.await;
let body = std::str::from_utf8(response.body()).unwrap();
assert_eq!(
body,
"{\"Hash\":\"Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP\",\"Name\":\"testfile.txt\",\"Size\":\"20\"}\r\n"
);
}
async fn tokio_ipfs() -> ipfs::Ipfs<ipfs::TestTypes> {
let options = ipfs::IpfsOptions::inmemory_with_generated_keys();
let (ipfs, fut) = ipfs::UninitializedIpfs::new(options).start().await.unwrap();
tokio::spawn(fut);
ipfs
}
}