From 845894f12cd28a74a322d2780a99b2fe9ce795b1 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Sat, 28 Dec 2024 15:42:19 +0800 Subject: [PATCH] :recycle: Updated way to setting thumbnail & compressed --- pkg/internal/models/attachments.go | 2 + pkg/internal/server/api/attachments_api.go | 54 +++++++++++++++++----- pkg/internal/services/attachments.go | 2 +- pkg/internal/services/related.go | 53 +++++++++++++++++++++ 4 files changed, 99 insertions(+), 12 deletions(-) create mode 100644 pkg/internal/services/related.go diff --git a/pkg/internal/models/attachments.go b/pkg/internal/models/attachments.go index 0c69af2..82108e1 100644 --- a/pkg/internal/models/attachments.go +++ b/pkg/internal/models/attachments.go @@ -33,6 +33,7 @@ type Attachment struct { HashCode string `json:"hash"` Destination int `json:"destination"` RefCount int `json:"ref_count"` + Type uint `json:"type"` FileChunks datatypes.JSONMap `json:"file_chunks"` @@ -108,6 +109,7 @@ func (v AttachmentFragment) ToAttachment() Attachment { Metadata: v.Metadata, Usermeta: v.Usermeta, Destination: AttachmentDstTemporary, + Type: AttachmentTypeNormal, Pool: v.Pool, PoolID: v.PoolID, AccountID: v.AccountID, diff --git a/pkg/internal/server/api/attachments_api.go b/pkg/internal/server/api/attachments_api.go index 5beadba..9837a86 100644 --- a/pkg/internal/server/api/attachments_api.go +++ b/pkg/internal/server/api/attachments_api.go @@ -88,9 +88,11 @@ func updateAttachmentMeta(c *fiber.Ctx) error { user := c.Locals("nex_user").(*sec.UserInfo) var data struct { - Alternative *string `json:"alt"` - Metadata *map[string]any `json:"metadata"` - IsIndexable *bool `json:"is_indexable"` + Thumbnail *uint `json:"thumbnail"` + Compressed *uint `json:"compressed"` + Alternative string `json:"alt"` + Metadata map[string]any `json:"metadata"` + IsIndexable bool `json:"is_indexable"` } if err := exts.BindAndValidate(c, &data); err != nil { @@ -98,20 +100,50 @@ func updateAttachmentMeta(c *fiber.Ctx) error { } var attachment models.Attachment - if err := database.C.Where("id = ? AND account_id = ?", id, user.ID).First(&attachment).Error; err != nil { + if err := database.C. + Where("id = ? AND account_id = ?", id, user.ID). + Preload("Thumbnail"). + Preload("Compressed"). + First(&attachment).Error; err != nil { return fiber.NewError(fiber.StatusBadRequest, err.Error()) } - if data.Alternative != nil { - attachment.Alternative = *data.Alternative + if data.Thumbnail != nil && attachment.ThumbnailID != data.Thumbnail { + var thumbnail models.Attachment + if err := database.C. + Where("id = ? AND account_id = ?", data.Thumbnail, user.ID). + First(&thumbnail).Error; err != nil { + return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("unable find thumbnail: %v", err)) + } + if attachment.Thumbnail != nil { + services.UnsetAttachmentAsThumbnail(*attachment.Thumbnail) + } + thumbnail, err := services.SetAttachmentAsThumbnail(thumbnail) + if err != nil { + return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("unable set thumbnail: %v", err)) + } + attachment.Thumbnail = &thumbnail + attachment.ThumbnailID = &thumbnail.ID } - if data.Metadata != nil { - attachment.Usermeta = *data.Metadata - } - if data.IsIndexable != nil { - attachment.IsIndexable = *data.IsIndexable + if data.Compressed != nil && attachment.CompressedID != data.Compressed { + var compressed models.Attachment + if err := database.C. + Where("id = ? AND account_id = ?", data.Compressed, user.ID). + First(&compressed).Error; err != nil { + return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("unable find compressed: %v", err)) + } + compressed, err := services.SetAttachmentAsCompressed(compressed) + if err != nil { + return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("unable set compressed: %v", err)) + } + attachment.Compressed = &compressed + attachment.CompressedID = &compressed.ID } + attachment.Alternative = data.Alternative + attachment.Usermeta = data.Metadata + attachment.IsIndexable = data.IsIndexable + if attachment, err := services.UpdateAttachment(attachment); err != nil { return fiber.NewError(fiber.StatusBadRequest, err.Error()) } else { diff --git a/pkg/internal/services/attachments.go b/pkg/internal/services/attachments.go index b8f5a62..fe1e859 100644 --- a/pkg/internal/services/attachments.go +++ b/pkg/internal/services/attachments.go @@ -182,7 +182,7 @@ func TryLinkAttachment(tx *gorm.DB, og models.Attachment, hash string) (bool, er } func UpdateAttachment(item models.Attachment) (models.Attachment, error) { - if err := database.C.Updates(&item).Error; err != nil { + if err := database.C.Model(&item).Updates(&item).Error; err != nil { return item, err } else { CacheAttachment(item) diff --git a/pkg/internal/services/related.go b/pkg/internal/services/related.go new file mode 100644 index 0000000..e32d1cf --- /dev/null +++ b/pkg/internal/services/related.go @@ -0,0 +1,53 @@ +package services + +import ( + "fmt" + "strings" + + "git.solsynth.dev/hypernet/paperclip/pkg/internal/database" + "git.solsynth.dev/hypernet/paperclip/pkg/internal/models" +) + +func SetAttachmentAsThumbnail(item models.Attachment) (models.Attachment, error) { + if !strings.HasPrefix(item.MimeType, "image") { + return item, fmt.Errorf("thumbnail must be an image") + } + + item.Type = models.AttachmentTypeThumbnail + item.UsedCount++ + if err := database.C.Save(&item).Error; err != nil { + return item, err + } + + return item, nil +} + +func SetAttachmentAsCompressed(item models.Attachment) (models.Attachment, error) { + item.Type = models.AttachmentTypeCompressed + item.UsedCount++ + if err := database.C.Save(&item).Error; err != nil { + return item, err + } + + return item, nil +} + +func UnsetAttachmentAsThumbnail(item models.Attachment) (models.Attachment, error) { + item.Type = models.AttachmentTypeNormal + item.UsedCount-- + if err := database.C.Save(&item).Error; err != nil { + return item, err + } + + return item, nil +} + +func UnsetAttachmentAsCompressed(item models.Attachment) (models.Attachment, error) { + item.Type = models.AttachmentTypeNormal + item.UsedCount-- + if err := database.C.Save(&item).Error; err != nil { + return item, err + } + + return item, nil +}