refactor: switch to using All api then filter to find valid locations

This commit is contained in:
Derrick Hammer 2024-02-27 10:58:15 -05:00
parent cdb23540ca
commit 097e29aa94
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 38 additions and 31 deletions

View File

@ -831,48 +831,55 @@ func (s *S5API) accountPin(jc jape.Context) {
return return
} }
next, err := dlUriProvider.Next() locations, err := dlUriProvider.All()
if err != nil { if err != nil {
s.sendErrorResponse(jc, NewS5Error(ErrKeyResourceNotFound, err)) s.sendErrorResponse(jc, NewS5Error(ErrKeyResourceNotFound, err))
return return
} }
r := rq.Get(next.Location().BytesURL()) locations = lo.FilterMap(locations, func(location storage2.SignedStorageLocation, index int) (storage2.SignedStorageLocation, bool) {
httpReq, err := r.ParseRequest() r := rq.Get(location.Location().BytesURL())
httpReq, err := r.ParseRequest()
if err != nil {
s.sendErrorResponse(jc, NewS5Error(ErrKeyInternalError, err))
return
}
res, err := http.DefaultClient.Do(httpReq)
if err != nil {
s.sendErrorResponse(jc, NewS5Error(ErrKeyFileDownloadFailed, err))
}
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil { if err != nil {
s.logger.Error("Error closing response body", zap.Error(err)) return nil, false
} }
}(res.Body)
contentLengthStr := res.Header.Get("Content-Length") res, err := http.DefaultClient.Do(httpReq)
if contentLengthStr == "" {
s.sendErrorResponse(jc, NewS5Error(ErrKeyFileDownloadFailed, errors.New("content-length header is missing"))) if err != nil {
return nil, false
}
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
s.logger.Error("Error closing response body", zap.Error(err))
}
}(res.Body)
contentLengthStr := res.Header.Get("Content-Length")
if contentLengthStr == "" {
return nil, false
}
contentLength, err := strconv.ParseInt(contentLengthStr, 10, 64)
if err != nil {
return nil, false
}
if uint64(contentLength) != decodedCid.Size {
return nil, false
}
return location, true
})
if len(locations) == 0 {
s.sendErrorResponse(jc, NewS5Error(ErrKeyResourceNotFound, fmt.Errorf("cid could not be found on the network")))
return return
} }
contentLength, err := strconv.ParseInt(contentLengthStr, 10, 64) location := locations[0]
if err != nil {
s.sendErrorResponse(jc, NewS5Error(ErrKeyFileDownloadFailed, err))
return
}
if uint64(contentLength) != decodedCid.Size {
s.sendErrorResponse(jc, NewS5Error(ErrKeyFileDownloadFailed, errors.New("file size does not match CID expected size")))
return
}
cid64, err := decodedCid.ToBase64Url() cid64, err := decodedCid.ToBase64Url()
if err != nil { if err != nil {
@ -888,7 +895,7 @@ func (s *S5API) accountPin(jc jape.Context) {
Name: jobName, Name: jobName,
Tags: nil, Tags: nil,
Function: s.pinImportCronJob, Function: s.pinImportCronJob,
Args: []interface{}{cid64, next.Location().BytesURL(), next.Location().OutboardBytesURL(), userID}, Args: []interface{}{cid64, location.Location().BytesURL(), location.Location().OutboardBytesURL(), userID},
Attempt: 0, Attempt: 0,
Limit: 10, Limit: 10,
After: nil, After: nil,