From 4b712a3a80f7000b277ada8b917b2a105e21c823 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Mon, 22 May 2023 16:36:19 -0400 Subject: [PATCH] refactor: see if proof exists and only if both the proof and file are not 404, do we abort as already existing --- service/files/files.go | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/service/files/files.go b/service/files/files.go index 30b45ba..100b33a 100644 --- a/service/files/files.go +++ b/service/files/files.go @@ -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)