From 3a77af23cbfb1f741f029f5061175c3df67f68b5 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Thu, 26 Dec 2024 21:53:09 +0800 Subject: [PATCH] :recycle: Usermeta, is indexable, thumbnail and more fields on attachments --- pkg/internal/models/attachments.go | 16 +++++++++----- pkg/internal/server/api/attachments_api.go | 23 ++++++++++++++------- pkg/internal/server/api/index_api.go | 7 +++++-- pkg/internal/server/api/up_direct_api.go | 4 ++-- pkg/internal/server/api/up_multipart_api.go | 5 ++--- 5 files changed, 35 insertions(+), 20 deletions(-) diff --git a/pkg/internal/models/attachments.go b/pkg/internal/models/attachments.go index 3d4bb6e..7e15b9b 100644 --- a/pkg/internal/models/attachments.go +++ b/pkg/internal/models/attachments.go @@ -32,11 +32,17 @@ type Attachment struct { CleanedAt *time.Time `json:"cleaned_at"` - Metadata datatypes.JSONMap `json:"metadata"` - IsMature bool `json:"is_mature"` - IsAnalyzed bool `json:"is_analyzed"` - IsUploaded bool `json:"is_uploaded"` - IsSelfRef bool `json:"is_self_ref"` + Metadata datatypes.JSONMap `json:"metadata"` // This field is analyzer auto generated metadata + Usermeta datatypes.JSONMap `json:"usermeta"` // This field is user set metadata + + Thumbnail string `json:"thumbnail"` // The cover image of audio / video attachment + ContentRating int `json:"content_rating"` // This field use to filter mature content or not + QualityRating int `json:"quality_rating"` // This field use to filter good content or not + + IsAnalyzed bool `json:"is_analyzed"` + IsUploaded bool `json:"is_uploaded"` + IsSelfRef bool `json:"is_self_ref"` + IsIndexable bool `json:"is_indexable"` // Show this attachment in the public directory api or not Ref *Attachment `json:"ref"` RefID *uint `json:"ref_id"` diff --git a/pkg/internal/server/api/attachments_api.go b/pkg/internal/server/api/attachments_api.go index 10697ae..f12a960 100644 --- a/pkg/internal/server/api/attachments_api.go +++ b/pkg/internal/server/api/attachments_api.go @@ -2,11 +2,12 @@ package api import ( "fmt" + "net/url" + "path/filepath" + "git.solsynth.dev/hypernet/nexus/pkg/nex/sec" "git.solsynth.dev/hypernet/paperclip/pkg/internal/database" "git.solsynth.dev/hypernet/paperclip/pkg/internal/server/exts" - "net/url" - "path/filepath" "git.solsynth.dev/hypernet/paperclip/pkg/internal/models" "git.solsynth.dev/hypernet/paperclip/pkg/internal/services" @@ -89,9 +90,9 @@ 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"` - IsMature bool `json:"is_mature"` + Alternative *string `json:"alt"` + Metadata *map[string]any `json:"metadata"` + IsIndexable *bool `json:"is_indexable"` } if err := exts.BindAndValidate(c, &data); err != nil { @@ -103,9 +104,15 @@ func updateAttachmentMeta(c *fiber.Ctx) error { return fiber.NewError(fiber.StatusBadRequest, err.Error()) } - attachment.Alternative = data.Alternative - attachment.Metadata = data.Metadata - attachment.IsMature = data.IsMature + if data.Alternative != nil { + attachment.Alternative = *data.Alternative + } + if data.Metadata != nil { + attachment.Usermeta = *data.Metadata + } + if data.IsIndexable != nil { + attachment.IsIndexable = *data.IsIndexable + } if attachment, err := services.UpdateAttachment(attachment); err != nil { return fiber.NewError(fiber.StatusBadRequest, err.Error()) diff --git a/pkg/internal/server/api/index_api.go b/pkg/internal/server/api/index_api.go index 3200d91..063c792 100644 --- a/pkg/internal/server/api/index_api.go +++ b/pkg/internal/server/api/index_api.go @@ -2,11 +2,12 @@ package api import ( "fmt" + "strings" + "git.solsynth.dev/hypernet/paperclip/pkg/internal/gap" "git.solsynth.dev/hypernet/passport/pkg/authkit" "github.com/spf13/viper" "gorm.io/datatypes" - "strings" "git.solsynth.dev/hypernet/paperclip/pkg/internal/database" "git.solsynth.dev/hypernet/paperclip/pkg/internal/models" @@ -26,7 +27,7 @@ func listAttachment(c *fiber.Ctx) error { needQuery := true - var result = make([]models.Attachment, take) + result := make([]models.Attachment, take) var idxList []string if len(c.Query("id")) > 0 { @@ -73,6 +74,8 @@ func listAttachment(c *fiber.Ctx) error { tx = tx.Where("ref_id IS NULL") } + tx = tx.Where("is_indexable != ?", false) + var count int64 countTx := tx if err := countTx.Model(&models.Attachment{}).Count(&count).Error; err != nil { diff --git a/pkg/internal/server/api/up_direct_api.go b/pkg/internal/server/api/up_direct_api.go index 1c84a1a..698f065 100644 --- a/pkg/internal/server/api/up_direct_api.go +++ b/pkg/internal/server/api/up_direct_api.go @@ -2,6 +2,7 @@ package api import ( "fmt" + "git.solsynth.dev/hypernet/nexus/pkg/nex/sec" "git.solsynth.dev/hypernet/paperclip/pkg/internal/database" "git.solsynth.dev/hypernet/paperclip/pkg/internal/models" @@ -45,8 +46,7 @@ func createAttachmentDirectly(c *fiber.Ctx) error { metadata, err := services.NewAttachmentMetadata(tx, user, file, models.Attachment{ Alternative: c.FormValue("alt"), MimeType: c.FormValue("mimetype"), - Metadata: usermeta, - IsMature: len(c.FormValue("mature")) > 0, + Usermeta: usermeta, IsAnalyzed: false, IsUploaded: true, Destination: models.AttachmentDstTemporary, diff --git a/pkg/internal/server/api/up_multipart_api.go b/pkg/internal/server/api/up_multipart_api.go index ac008f9..32d2e95 100644 --- a/pkg/internal/server/api/up_multipart_api.go +++ b/pkg/internal/server/api/up_multipart_api.go @@ -3,6 +3,7 @@ package api import ( "encoding/json" "fmt" + "git.solsynth.dev/hypernet/nexus/pkg/nex/sec" "git.solsynth.dev/hypernet/paperclip/pkg/internal/database" "git.solsynth.dev/hypernet/paperclip/pkg/internal/models" @@ -22,7 +23,6 @@ func createAttachmentMultipartPlaceholder(c *fiber.Ctx) error { Alternative string `json:"alt"` MimeType string `json:"mimetype"` Metadata map[string]any `json:"metadata"` - IsMature bool `json:"is_mature"` } if err := exts.BindAndValidate(c, &data); err != nil { @@ -50,8 +50,7 @@ func createAttachmentMultipartPlaceholder(c *fiber.Ctx) error { Size: data.Size, Alternative: data.Alternative, MimeType: data.MimeType, - Metadata: data.Metadata, - IsMature: data.IsMature, + Usermeta: data.Metadata, IsAnalyzed: false, Destination: models.AttachmentDstTemporary, Pool: &pool,