Skip to content

Commit

Permalink
Fix possible data loss when uploading large file
Browse files Browse the repository at this point in the history
  • Loading branch information
biluohc committed Feb 13, 2020
1 parent 9cef353 commit 55f196e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
9 changes: 8 additions & 1 deletion src/handlers/file_upload/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,12 @@ pub async fn file_upload_handler<'a>(
Ok(writec) => {
let size = ByteSize::b(writec).to_string_as(true);
warn!(
"addr: {}, part.filename: {}, conetentype: {}, saved to {} ok: {}",
"addr: {}, part.filename: {}, conetentype: {}, saved to {} ok: {} {}",
addr,
part.filename(),
part.contentype(),
file.display(),
writec,
size
);
}
Expand All @@ -76,5 +77,11 @@ pub async fn file_upload_handler<'a>(
}
}

debug!(
"contentlength: {}, offset: {}",
ps.content_lenth().unwrap_or_default(),
ps.offset()
);

f(200, "upload ok")
}
13 changes: 12 additions & 1 deletion src/handlers/file_upload/multipart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ impl MultiPart {
})
}

pub fn content_lenth(&self) -> &Option<u64> {
&self.content_lenth
}

pub fn offset(&self) -> u64 {
self.offset
}

// b"--------------------------7adee9ed033d3a54\r\nContent-Disposition: form-data; name=\"filename\"; filename=\"pkg.jl\"\r\nContent-Type: application/octet-stream\r\n\r\n"

pub async fn next_part<'a>(&'a mut self) -> Option<Result<Part<'a>, Error>> {
Expand Down Expand Up @@ -251,7 +259,7 @@ impl<'a> Part<'a> {
file.write(chunk.as_ref()).map(|wc| (wc, chunk)).map_err(Error::from)
}) {
Ok((wc, chunk)) => {
debug_assert_eq!(wc, chunk.len());
assert_eq!(wc, chunk.len());
bytesc += wc;
}
Err(e) => {
Expand Down Expand Up @@ -306,6 +314,8 @@ impl<'a> Part<'a> {
if self.complete {
return Some(Err(format_err!("unexpected eof")));
} else {
// todo: it maybe slow ?
self.multi.buf = chunk.as_ref().into();
continue;
}
}
Expand All @@ -319,6 +329,7 @@ impl<'a> Part<'a> {
if i == 0 {
None
} else {
assert_eq!(&chunk[chunk.len() - 2..], b"\r\n");
chunk.truncate(chunk.len() - 2);
Some(Ok(chunk))
}
Expand Down

0 comments on commit 55f196e

Please sign in to comment.