refactor: see if proof exists and only if both the proof and file are not 404, do we abort as already existing

This commit is contained in:
Derrick Hammer 2023-05-22 16:36:19 -04:00
parent 7fe05862b1
commit 4b712a3a80
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 23 additions and 4 deletions

View File

@ -72,19 +72,38 @@ func Upload(r io.ReadSeeker, size int64, hash []byte) (model.Upload, error) {
return upload, err
}
statusCode := objectExistsResult.StatusCode()
objectStatusCode := objectExistsResult.StatusCode()
if statusCode == 500 {
if objectStatusCode == 500 {
bodyErr := objectExistsResult.String()
if !strings.Contains(bodyErr, "no slabs found") {
shared.GetLogger().Error("Failed fetching object", zap.String("error", objectExistsResult.String()))
return upload, errors.New(fmt.Sprintf("error fetching file: %s", objectExistsResult.String()))
}
statusCode = 404
objectStatusCode = 404
}
if statusCode != 404 {
proofExistsResult, err := client.R().Get(fmt.Sprintf("/worker/objects/%s.obao", hashHex))
if err != nil {
shared.GetLogger().Error("Failed query object proof", zap.Error(err))
return upload, err
}
proofStatusCode := proofExistsResult.StatusCode()
if proofStatusCode == 500 {
bodyErr := proofExistsResult.String()
if !strings.Contains(bodyErr, "no slabs found") {
shared.GetLogger().Error("Failed fetching object proof", zap.String("error", proofExistsResult.String()))
return upload, errors.New(fmt.Sprintf("error fetching file proof: %s", proofExistsResult.String()))
}
objectStatusCode = 404
}
if objectStatusCode != 404 && proofStatusCode != 404 {
msg := "file already exists in network, but missing in database"
shared.GetLogger().Error(msg)
return upload, errors.New(msg)