From 0fc3fa9a4555dce0a140680804189c706dc4e7d1 Mon Sep 17 00:00:00 2001 From: mutantmonkey Date: Thu, 24 Jan 2019 19:25:05 -0800 Subject: [PATCH] Use ReadFull when reading chunks for torrent When generating a torrent, we need to get the SHA1 hash of each chunk of the file. Because we stream the data for S3, Read doesn't always fill the chunk buffer, so to fix this, we can use ReadFull. --- torrent.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/torrent.go b/torrent.go index f6a5505..c5e7a58 100644 --- a/torrent.go +++ b/torrent.go @@ -28,10 +28,10 @@ func createTorrent(fileName string, f io.Reader, r *http.Request) ([]byte, error } for { - n, err := f.Read(chunk) + n, err := io.ReadFull(f, chunk) if err == io.EOF { break - } else if err != nil { + } else if err != nil && err != io.ErrUnexpectedEOF { return []byte{}, err } @@ -58,7 +58,8 @@ func fileTorrentHandler(c web.C, w http.ResponseWriter, r *http.Request) { oopsHandler(c, w, r, RespAUTO, "Corrupt metadata.") return } else if err != nil { - oopsHandler(c, w, r, RespAUTO, "Could not create torrent.") + oopsHandler(c, w, r, RespAUTO, err.Error()) + return } defer f.Close()