From b46e12b972e2e9aa4dd716dc7c66cb872bcecb5c Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Fri, 1 Mar 2024 05:09:57 -0500 Subject: [PATCH] fix: if for some edge case reason an upload id no longer exists, start over, but use db Save so we can update the record if we have an ID --- storage/storage.go | 48 ++++++++++++++++++++++++---------------------- 1 file changed, 25 insertions(+), 23 deletions(-) diff --git a/storage/storage.go b/storage/storage.go index e9270d6..def6bcd 100644 --- a/storage/storage.go +++ b/storage/storage.go @@ -370,6 +370,30 @@ func (s StorageServiceDefault) S3MultipartUpload(ctx context.Context, data io.Re uploadId = s3Upload.UploadID } + if len(uploadId) > 0 { + parts, err := client.ListParts(ctx, &s3.ListPartsInput{ + Bucket: aws.String(bucket), + Key: aws.String(key), + UploadId: aws.String(uploadId), + }) + + if err != nil { + uploadId = "" + } else { + for _, part := range parts.Parts { + if uint64(*part.Size) == partSize { + if *part.PartNumber > lastPartNumber { + lastPartNumber = *part.PartNumber + completedParts = append(completedParts, types.CompletedPart{ + ETag: part.ETag, + PartNumber: part.PartNumber, + }) + } + } + } + } + } + if uploadId == "" { mu, err := client.CreateMultipartUpload(ctx, &s3.CreateMultipartUploadInput{ Bucket: aws.String(bucket), @@ -382,32 +406,10 @@ func (s StorageServiceDefault) S3MultipartUpload(ctx context.Context, data io.Re uploadId = *mu.UploadId s3Upload.UploadID = uploadId - ret = s.db.Create(&s3Upload) + ret = s.db.Save(&s3Upload) if ret.Error != nil { return ret.Error } - } else { - parts, err := client.ListParts(ctx, &s3.ListPartsInput{ - Bucket: aws.String(bucket), - Key: aws.String(key), - UploadId: aws.String(uploadId), - }) - - if err != nil { - return err - } - - for _, part := range parts.Parts { - if uint64(*part.Size) == partSize { - if *part.PartNumber > lastPartNumber { - lastPartNumber = *part.PartNumber - completedParts = append(completedParts, types.CompletedPart{ - ETag: part.ETag, - PartNumber: part.PartNumber, - }) - } - } - } } for partNum := 1; partNum <= totalParts; partNum++ {