♻️ Updated way to setting thumbnail & compressed
This commit is contained in:
@ -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
|
||||
}
|
Reference in New Issue
Block a user