Attachment pool basis

This commit is contained in:
LittleSheep 2024-08-18 12:01:41 +08:00
parent de6215cffe
commit dd0f7399a6
10 changed files with 226 additions and 18 deletions

View File

@ -4,9 +4,17 @@
<option name="autoReloadType" value="ALL" />
</component>
<component name="ChangeListManager">
<list default="true" id="18dd0d68-b4b8-40db-9734-9119b5c848bd" name="更改" comment=":sparkles: Self reference detection">
<list default="true" id="18dd0d68-b4b8-40db-9734-9119b5c848bd" name="更改" comment=":sparkles: List attachment original filter">
<change afterPath="$PROJECT_DIR$/pkg/internal/models/pools.go" afterDir="false" />
<change afterPath="$PROJECT_DIR$/pkg/internal/server/api/pools_api.go" afterDir="false" />
<change afterPath="$PROJECT_DIR$/pkg/internal/services/pools.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/internal/server/api/attachment_dir_api.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/server/api/attachment_dir_api.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/internal/database/migrator.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/database/migrator.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/internal/models/accounts.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/models/accounts.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/internal/models/attachments.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/models/attachments.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/internal/models/metadata.go" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/internal/server/api/attachments_api.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/server/api/attachments_api.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/internal/server/api/index.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/server/api/index.go" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -118,7 +126,8 @@
<MESSAGE value=":zap: Add cache into metadata fetching" />
<MESSAGE value=":recycle: Moved onto dealer" />
<MESSAGE value=":sparkles: Self reference detection" />
<option name="LAST_COMMIT_MESSAGE" value=":sparkles: Self reference detection" />
<MESSAGE value=":sparkles: List attachment original filter" />
<option name="LAST_COMMIT_MESSAGE" value=":sparkles: List attachment original filter" />
</component>
<component name="VgoProject">
<settings-migrated>true</settings-migrated>

View File

@ -8,6 +8,7 @@ import (
var AutoMaintainRange = []any{
&models.Account{},
&models.Attachment{},
&models.AttachmentPool{},
&models.StickerPack{},
&models.Sticker{},
}

View File

@ -13,6 +13,8 @@ type Account struct {
Description string `json:"description"`
EmailAddress string `json:"email_address"`
PowerLevel int `json:"power_level"`
Attachments []Attachment `json:"attachments"`
ExternalID uint `json:"external_id"`
Attachments []Attachment `json:"attachments"`
Pools []AttachmentPool `json:"pools"`
}

View File

@ -1,6 +1,9 @@
package models
import "gorm.io/datatypes"
import (
"gorm.io/datatypes"
"time"
)
type AttachmentDst = int8
@ -22,6 +25,8 @@ type Attachment struct {
Destination AttachmentDst `json:"destination"`
RefCount int `json:"ref_count"`
CleanedAt *time.Time `json:"cleaned_at"`
Metadata datatypes.JSONMap `json:"metadata"`
IsMature bool `json:"is_mature"`
IsAnalyzed bool `json:"is_analyzed"`
@ -30,6 +35,9 @@ type Attachment struct {
Ref *Attachment `json:"ref"`
RefID *uint `json:"ref_id"`
Pool *AttachmentPool `json:"pool"`
PoolID *uint `json:"pool_id"`
Account Account `json:"account"`
AccountID uint `json:"account_id"`
}

View File

@ -1,4 +0,0 @@
package models
type MediaMetadata struct {
}

View File

@ -0,0 +1,22 @@
package models
import "gorm.io/datatypes"
type AttachmentPool struct {
BaseModel
Alias string `json:"alias"`
Name string `json:"name"`
Description string `json:"description"`
Config datatypes.JSONType[AttachmentPoolConfig] `json:"config"`
Attachments []Attachment `json:"attachments"`
Account *Account `json:"account"`
AccountID *uint `json:"account_id"`
}
type AttachmentPoolConfig struct {
ExistLifecycle *int `json:"exist_lifecycle"`
IsPublicAccessible bool `json:"is_public_accessible"`
}

View File

@ -95,7 +95,7 @@ func createAttachment(c *fiber.Ctx) error {
return err
}
if err := gap.H.EnsureGrantedPerm(c, "CreateAttachments", file.Size); err != nil {
if err = gap.H.EnsureGrantedPerm(c, "CreateAttachments", file.Size); err != nil {
return err
}

View File

@ -7,6 +7,12 @@ func MapAPIs(app *fiber.App, baseURL string) {
api := app.Group(baseURL).Name("API")
{
api.Get("/pools", listPost)
api.Get("/pools/:id", getPool)
api.Post("/pools", createPool)
api.Put("/pools/:id", updatePool)
api.Delete("/pools/:id", deletePool)
api.Get("/attachments", listAttachment)
api.Get("/attachments/:id/meta", getAttachmentMeta)
api.Get("/attachments/:id", openAttachment)

View File

@ -0,0 +1,113 @@
package api
import (
"git.solsynth.dev/hydrogen/paperclip/pkg/internal/gap"
"git.solsynth.dev/hydrogen/paperclip/pkg/internal/models"
"git.solsynth.dev/hydrogen/paperclip/pkg/internal/server/exts"
"git.solsynth.dev/hydrogen/paperclip/pkg/internal/services"
"github.com/gofiber/fiber/v2"
"gorm.io/datatypes"
)
func listPost(c *fiber.Ctx) error {
pools, err := services.ListAttachmentPool()
if err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
}
return c.JSON(pools)
}
func getPool(c *fiber.Ctx) error {
id, _ := c.ParamsInt("id")
pool, err := services.GetAttachmentPool(uint(id))
if err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
}
return c.JSON(pool)
}
func createPool(c *fiber.Ctx) error {
if err := gap.H.EnsureGrantedPerm(c, "CreateAttachmentPools", true); err != nil {
return err
}
user := c.Locals("user").(models.Account)
var data struct {
Alias string `json:"alias" validate:"required"`
Name string `json:"name" validate:"required"`
Description string `json:"description"`
Config models.AttachmentPoolConfig `json:"config"`
}
if err := exts.BindAndValidate(c, &data); err != nil {
return err
}
pool := models.AttachmentPool{
Alias: data.Alias,
Name: data.Name,
Description: data.Description,
Config: datatypes.NewJSONType(data.Config),
AccountID: &user.ID,
}
if pool, err := services.NewAttachmentPool(pool); err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
} else {
return c.JSON(pool)
}
}
func updatePool(c *fiber.Ctx) error {
if err := gap.H.EnsureAuthenticated(c); err != nil {
return err
}
user := c.Locals("user").(models.Account)
var data struct {
Alias string `json:"alias" validate:"required"`
Name string `json:"name" validate:"required"`
Description string `json:"description"`
Config models.AttachmentPoolConfig `json:"config"`
}
if err := exts.BindAndValidate(c, &data); err != nil {
return err
}
id, _ := c.ParamsInt("id")
pool, err := services.GetAttachmentPoolWithUser(uint(id), user.ID)
if err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
}
pool.Alias = data.Alias
pool.Name = data.Name
pool.Description = data.Description
pool.Config = datatypes.NewJSONType(data.Config)
if pool, err := services.UpdateAttachmentPool(pool); err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
} else {
return c.JSON(pool)
}
}
func deletePool(c *fiber.Ctx) error {
if err := gap.H.EnsureAuthenticated(c); err != nil {
return err
}
user := c.Locals("user").(models.Account)
id, _ := c.ParamsInt("id")
pool, err := services.GetAttachmentPoolWithUser(uint(id), user.ID)
if err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
}
if pool, err := services.DeleteAttachmentPool(pool); err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
} else {
return c.JSON(pool)
}
}

View File

@ -0,0 +1,51 @@
package services
import (
"git.solsynth.dev/hydrogen/paperclip/pkg/internal/database"
"git.solsynth.dev/hydrogen/paperclip/pkg/internal/models"
)
func ListAttachmentPool() ([]models.AttachmentPool, error) {
var pools []models.AttachmentPool
if err := database.C.Find(&pools).Error; err != nil {
return pools, err
}
return pools, nil
}
func GetAttachmentPool(id uint) (models.AttachmentPool, error) {
var pool models.AttachmentPool
if err := database.C.Where("id = ?", id).First(&pool).Error; err != nil {
return pool, err
}
return pool, nil
}
func GetAttachmentPoolWithUser(id uint, userId uint) (models.AttachmentPool, error) {
var pool models.AttachmentPool
if err := database.C.Where("id = ? AND account_id = ?", id, userId).First(&pool).Error; err != nil {
return pool, err
}
return pool, nil
}
func NewAttachmentPool(pool models.AttachmentPool) (models.AttachmentPool, error) {
if err := database.C.Save(&pool).Error; err != nil {
return pool, err
}
return pool, nil
}
func UpdateAttachmentPool(pool models.AttachmentPool) (models.AttachmentPool, error) {
if err := database.C.Save(&pool).Error; err != nil {
return pool, err
}
return pool, nil
}
func DeleteAttachmentPool(pool models.AttachmentPool) (models.AttachmentPool, error) {
if err := database.C.Delete(&pool).Error; err != nil {
return pool, err
}
return pool, nil
}