Paperclip/pkg/internal/services/related.go

54 lines
1.2 KiB
Go

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
}