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 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) {
r := rq.Get(location.Location().BytesURL())
httpReq, err := r.ParseRequest() httpReq, err := r.ParseRequest()
if err != nil { if err != nil {
s.sendErrorResponse(jc, NewS5Error(ErrKeyInternalError, err)) return nil, false
return
} }
res, err := http.DefaultClient.Do(httpReq) res, err := http.DefaultClient.Do(httpReq)
if err != nil { if err != nil {
s.sendErrorResponse(jc, NewS5Error(ErrKeyFileDownloadFailed, err)) return nil, false
} }
defer func(Body io.ReadCloser) { defer func(Body io.ReadCloser) {
err := Body.Close() err := Body.Close()
@ -859,21 +859,28 @@ func (s *S5API) accountPin(jc jape.Context) {
contentLengthStr := res.Header.Get("Content-Length") contentLengthStr := res.Header.Get("Content-Length")
if contentLengthStr == "" { if contentLengthStr == "" {
s.sendErrorResponse(jc, NewS5Error(ErrKeyFileDownloadFailed, errors.New("content-length header is missing"))) return nil, false
return
} }
contentLength, err := strconv.ParseInt(contentLengthStr, 10, 64) contentLength, err := strconv.ParseInt(contentLengthStr, 10, 64)
if err != nil { if err != nil {
s.sendErrorResponse(jc, NewS5Error(ErrKeyFileDownloadFailed, err)) return nil, false
return
} }
if uint64(contentLength) != decodedCid.Size { 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 return
} }
location := locations[0]
cid64, err := decodedCid.ToBase64Url() cid64, err := decodedCid.ToBase64Url()
if err != nil { if err != nil {
s.sendErrorResponse(jc, NewS5Error(ErrKeyInternalError, err)) s.sendErrorResponse(jc, NewS5Error(ErrKeyInternalError, err))
@ -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,