From 99dd7f55e0115e5c0d6155ec286287e3429a390b Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Sat, 28 Dec 2024 13:56:25 +0800 Subject: [PATCH] :sparkles: Fragment uploading continue --- pkg/internal/models/attachments.go | 2 ++ pkg/internal/server/api/up_multipart_api.go | 2 ++ pkg/internal/services/analyzer.go | 2 +- pkg/internal/services/fragments.go | 10 ++++++++++ 4 files changed, 15 insertions(+), 1 deletion(-) diff --git a/pkg/internal/models/attachments.go b/pkg/internal/models/attachments.go index 0df79c4..0c69af2 100644 --- a/pkg/internal/models/attachments.go +++ b/pkg/internal/models/attachments.go @@ -92,6 +92,8 @@ type AttachmentFragment struct { PoolID *uint `json:"pool_id"` AccountID uint `json:"account_id"` + + FileChunksMissing []string `json:"file_chunks_missing" gorm:"-"` // This field use to prompt client which chunks is pending upload, do not store it } func (v AttachmentFragment) ToAttachment() Attachment { diff --git a/pkg/internal/server/api/up_multipart_api.go b/pkg/internal/server/api/up_multipart_api.go index 7e17547..69b0f98 100644 --- a/pkg/internal/server/api/up_multipart_api.go +++ b/pkg/internal/server/api/up_multipart_api.go @@ -58,6 +58,8 @@ func createAttachmentFragment(c *fiber.Ctx) error { }) if err != nil { return fiber.NewError(fiber.StatusBadRequest, err.Error()) + } else { + metadata.FileChunksMissing = services.FindFragmentMissingChunks(metadata) } return c.JSON(fiber.Map{ diff --git a/pkg/internal/services/analyzer.go b/pkg/internal/services/analyzer.go index 7358896..67de4d7 100644 --- a/pkg/internal/services/analyzer.go +++ b/pkg/internal/services/analyzer.go @@ -315,7 +315,7 @@ func HashAttachment(file models.Attachment) (hash string, err error) { } // Hash with the file metadata - hasher.Write([]byte(fmt.Sprintf("%d", file.Size))) + fmt.Fprintf(hasher, "%d", file.Size) // Return the combined hash hash = hex.EncodeToString(hasher.Sum(nil)) diff --git a/pkg/internal/services/fragments.go b/pkg/internal/services/fragments.go index b943b34..2cf5489 100644 --- a/pkg/internal/services/fragments.go +++ b/pkg/internal/services/fragments.go @@ -151,3 +151,13 @@ func CheckFragmentChunkExists(meta models.AttachmentFragment, cid string) bool { return true } } + +func FindFragmentMissingChunks(meta models.AttachmentFragment) []string { + var missing []string + for cid := range meta.FileChunks { + if !CheckFragmentChunkExists(meta, cid) { + missing = append(missing, cid) + } + } + return missing +}