From 633e44298edce245d088e08cb28f48adb060eecf Mon Sep 17 00:00:00 2001 From: Ben Sarmiento Date: Mon, 6 Nov 2023 02:26:46 +0000 Subject: [PATCH] Fix download issues --- pkg/chunk/download.go | 10 ++++------ pkg/chunk/manager.go | 9 +++++++-- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/pkg/chunk/download.go b/pkg/chunk/download.go index b56b053..a9cd6ec 100644 --- a/pkg/chunk/download.go +++ b/pkg/chunk/download.go @@ -114,16 +114,14 @@ func (d *Downloader) downloadFromAPI(request *Request, buffer []byte, delay int6 downloadURL := resp.Download req, err := http.NewRequest("GET", downloadURL, nil) if nil != err { - d.log.Debugf("%v", err) + d.log.Debugf("request init error: %v", err) return fmt.Errorf("could not create request object %s %s from API", request.file.Path, request.file.Link) } req.Header.Add("Range", fmt.Sprintf("bytes=%v-%v", request.offsetStart, request.offsetEnd-1)) - d.log.Debugw("Sending HTTP Request %v", req) - res, err := http.DefaultClient.Do(req) if nil != err { - d.log.Debugf("%v", err) + d.log.Debugf("request error: %v", err) return fmt.Errorf("could not request object %s %s from API", request.file.Path, request.file.Link) } defer res.Body.Close() @@ -139,8 +137,8 @@ func (d *Downloader) downloadFromAPI(request *Request, buffer []byte, delay int6 } n, err := io.ReadFull(reader, buffer[:res.ContentLength:cap(buffer)]) - if nil != err { - d.log.Debugf("%v", err) + if nil != err && err != io.ErrUnexpectedEOF { + d.log.Debugf("response read error: %v", err) return fmt.Errorf("could not read objects %s %s API response", request.file.Path, request.file.Link) } d.log.Debugf("Downloaded %v bytes of %s %s", n, request.file.Path, request.file.Link) diff --git a/pkg/chunk/manager.go b/pkg/chunk/manager.go index 8e397e2..68fd023 100644 --- a/pkg/chunk/manager.go +++ b/pkg/chunk/manager.go @@ -1,8 +1,8 @@ package chunk import ( + "crypto/sha256" "encoding/binary" - "encoding/hex" "fmt" "os" @@ -144,7 +144,12 @@ func (m *Manager) GetChunk(object *torrent.File, offset, size int64) ([]byte, er } func buildRequestID(object *torrent.File, offset int64) (id RequestID) { - hex.Decode(id[:], []byte(object.Link)) + fileID := object.Link + if fileID == "" { + fileID = object.Path + } + hash := sha256.Sum256([]byte(fileID)) + copy(id[:16], hash[:16]) binary.BigEndian.PutUint64(id[16:], uint64(offset)) return }