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,24 +831,24 @@ func (s *S5API) accountPin(jc jape.Context) {
return
}
next, err := dlUriProvider.Next()
locations, err := dlUriProvider.All()
if err != nil {
s.sendErrorResponse(jc, NewS5Error(ErrKeyResourceNotFound, err))
return
}
r := rq.Get(next.Location().BytesURL())
locations = lo.FilterMap(locations, func(location storage2.SignedStorageLocation, index int) (storage2.SignedStorageLocation, bool) {
r := rq.Get(location.Location().BytesURL())
httpReq, err := r.ParseRequest()
if err != nil {
s.sendErrorResponse(jc, NewS5Error(ErrKeyInternalError, err))
return
return nil, false
}
res, err := http.DefaultClient.Do(httpReq)
if err != nil {
s.sendErrorResponse(jc, NewS5Error(ErrKeyFileDownloadFailed, err))
return nil, false
}
defer func(Body io.ReadCloser) {
err := Body.Close()
@ -859,21 +859,28 @@ func (s *S5API) accountPin(jc jape.Context) {
contentLengthStr := res.Header.Get("Content-Length")
if contentLengthStr == "" {
s.sendErrorResponse(jc, NewS5Error(ErrKeyFileDownloadFailed, errors.New("content-length header is missing")))
return
return nil, false
}
contentLength, err := strconv.ParseInt(contentLengthStr, 10, 64)
if err != nil {
s.sendErrorResponse(jc, NewS5Error(ErrKeyFileDownloadFailed, err))
return
return nil, false
}
if uint64(contentLength) != decodedCid.Size {
s.sendErrorResponse(jc, NewS5Error(ErrKeyFileDownloadFailed, errors.New("file size does not match CID expected 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
}
location := locations[0]
cid64, err := decodedCid.ToBase64Url()
if err != nil {
s.sendErrorResponse(jc, NewS5Error(ErrKeyInternalError, err))
@ -888,7 +895,7 @@ func (s *S5API) accountPin(jc jape.Context) {
Name: jobName,
Tags: nil,
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,
Limit: 10,
After: nil,