♻️ Updated way to setting thumbnail & compressed
This commit is contained in:
parent
dda85eae98
commit
845894f12c
@ -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,
|
||||
|
@ -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,19 +100,49 @@ 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 data.Metadata != nil {
|
||||
attachment.Usermeta = *data.Metadata
|
||||
if attachment.Thumbnail != nil {
|
||||
services.UnsetAttachmentAsThumbnail(*attachment.Thumbnail)
|
||||
}
|
||||
if data.IsIndexable != nil {
|
||||
attachment.IsIndexable = *data.IsIndexable
|
||||
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.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())
|
||||
|
@ -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)
|
||||
|
53
pkg/internal/services/related.go
Normal file
53
pkg/internal/services/related.go
Normal file
@ -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
|
||||
}
|
Loading…
Reference in New Issue
Block a user