Attachment has pool

This commit is contained in:
2024-08-18 14:09:52 +08:00
parent dd0f7399a6
commit a82fb3a49c
11 changed files with 102 additions and 32 deletions

View File

@ -15,11 +15,14 @@ const (
type Attachment struct {
BaseModel
Uuid string `json:"uuid"`
// Random ID is for accessing (appear in URL)
Rid string `json:"rid" gorm:"uniqueIndex"`
// Unique ID is for storing (appear in local file name or object name)
Uuid string `json:"uuid"`
Size int64 `json:"size"`
Name string `json:"name"`
Alternative string `json:"alt"`
Usage string `json:"usage"`
MimeType string `json:"mimetype"`
HashCode string `json:"hash"`
Destination AttachmentDst `json:"destination"`

View File

@ -17,6 +17,8 @@ type AttachmentPool struct {
}
type AttachmentPoolConfig struct {
ExistLifecycle *int `json:"exist_lifecycle"`
IsPublicAccessible bool `json:"is_public_accessible"`
MaxFileSize *int64 `json:"max_file_size"`
ExistLifecycle *int64 `json:"exist_lifecycle"`
AllowCrossPoolIngress bool `json:"allow_cross_pool_ingress"`
AllowCrossPoolEgress bool `json:"allow_cross_pool_egress"`
}

View File

@ -85,9 +85,18 @@ func createAttachment(c *fiber.Ctx) error {
}
user := c.Locals("user").(models.Account)
usage := c.FormValue("usage")
if !lo.Contains(viper.GetStringSlice("accepts_usage"), usage) {
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("disallowed usage: %s", usage))
poolAlias := c.FormValue("pool")
if len(poolAlias) == 0 {
poolAlias = c.FormValue("usage")
}
aliasingMap := viper.GetStringMapString("pools.aliases")
if val, ok := aliasingMap[poolAlias]; ok {
poolAlias = val
}
pool, err := services.GetAttachmentPoolByAlias(poolAlias)
if err != nil {
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("unable to get attachment pool info: %v", err))
}
file, err := c.FormFile("file")
@ -97,6 +106,8 @@ func createAttachment(c *fiber.Ctx) error {
if err = gap.H.EnsureGrantedPerm(c, "CreateAttachments", file.Size); err != nil {
return err
} else if pool.Config.Data().MaxFileSize != nil && file.Size > *pool.Config.Data().MaxFileSize {
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("attachment pool %s doesn't allow file larger than %d", pool.Alias, *pool.Config.Data().MaxFileSize))
}
usermeta := make(map[string]any)
@ -105,13 +116,14 @@ func createAttachment(c *fiber.Ctx) error {
tx := database.C.Begin()
metadata, err := services.NewAttachmentMetadata(tx, user, file, models.Attachment{
Usage: usage,
Alternative: c.FormValue("alt"),
MimeType: c.FormValue("mimetype"),
Metadata: usermeta,
IsMature: len(c.FormValue("mature")) > 0,
IsAnalyzed: false,
Destination: models.AttachmentDstTemporary,
Pool: &pool,
PoolID: &pool.ID,
})
if err != nil {
tx.Rollback()
@ -126,6 +138,7 @@ func createAttachment(c *fiber.Ctx) error {
tx.Commit()
metadata.Account = user
metadata.Pool = &pool
services.PublishAnalyzeTask(metadata)
return c.JSON(metadata)
@ -141,7 +154,6 @@ func updateAttachmentMeta(c *fiber.Ctx) error {
var data struct {
Alternative string `json:"alt"`
Usage string `json:"usage"`
IsMature bool `json:"is_mature"`
}
@ -155,7 +167,6 @@ func updateAttachmentMeta(c *fiber.Ctx) error {
}
attachment.Alternative = data.Alternative
attachment.Usage = data.Usage
attachment.IsMature = data.IsMature
if attachment, err := services.UpdateAttachment(attachment); err != nil {

View File

@ -99,7 +99,7 @@ func AnalyzeAttachment(file models.Attachment) error {
var start time.Time
// Do analyze job
// Do analyze jobs
if !file.IsAnalyzed || len(file.HashCode) == 0 {
destMap := viper.GetStringMap("destinations.temporary")

View File

@ -28,7 +28,7 @@ func GetAttachmentByID(id uint) (models.Attachment, error) {
var attachment models.Attachment
if err := database.C.Where(models.Attachment{
BaseModel: models.BaseModel{ID: id},
}).Preload("Account").First(&attachment).Error; err != nil {
}).Preload("Pool").Preload("Account").First(&attachment).Error; err != nil {
return attachment, err
} else {
MaintainAttachmentCache()
@ -42,7 +42,7 @@ func GetAttachmentByHash(hash string) (models.Attachment, error) {
var attachment models.Attachment
if err := database.C.Where(models.Attachment{
HashCode: hash,
}).First(&attachment).Error; err != nil {
}).Preload("Pool").First(&attachment).Error; err != nil {
return attachment, err
}
return attachment, nil
@ -60,6 +60,7 @@ func CacheAttachment(id uint, item models.Attachment) {
}
func NewAttachmentMetadata(tx *gorm.DB, user models.Account, file *multipart.FileHeader, attachment models.Attachment) (models.Attachment, error) {
attachment.Rid = RandString(16)
attachment.Uuid = uuid.NewString()
attachment.Size = file.Size
attachment.Name = file.Filename
@ -104,6 +105,13 @@ func TryLinkAttachment(tx *gorm.DB, og models.Attachment, hash string) (bool, er
return false, err
}
if prev.PoolID != nil && og.PoolID != nil && prev.PoolID != og.PoolID {
if !prev.Pool.Config.Data().AllowCrossPoolEgress || !og.Pool.Config.Data().AllowCrossPoolIngress {
// Pool config doesn't allow reference
return false, nil
}
}
prev.RefCount++
og.RefID = &prev.ID
og.Uuid = prev.Uuid

View File

@ -21,6 +21,14 @@ func GetAttachmentPool(id uint) (models.AttachmentPool, error) {
return pool, nil
}
func GetAttachmentPoolByAlias(alias string) (models.AttachmentPool, error) {
var pool models.AttachmentPool
if err := database.C.Where("alias = ?", alias).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 {

View File

@ -0,0 +1,31 @@
package services
import (
"math/rand"
"strings"
)
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
const (
letterIdxBits = 6
letterIdxMask = 1<<letterIdxBits - 1
letterIdxMax = 63 / letterIdxBits
)
func RandString(length int) string {
builder := strings.Builder{}
builder.Grow(length)
for idx, cache, remain := length-1, rand.Int63(), letterIdxMax; idx >= 0; {
if remain == 0 {
cache, remain = rand.Int63(), letterIdxMax
}
if idx := int(cache & letterIdxMask); idx < len(letterBytes) {
builder.WriteByte(letterBytes[idx])
idx--
}
cache >>= letterIdxBits
remain--
}
return builder.String()
}