Boost CRUD API

This commit is contained in:
2024-12-28 21:41:13 +08:00
parent edbe412f97
commit d59966a03e
11 changed files with 299 additions and 22 deletions

View File

@ -241,7 +241,7 @@ func AnalyzeAttachment(file models.Attachment) error {
if !linked {
go func() {
start = time.Now()
if err := ReUploadFileToPermanent(file, 1); err != nil {
if err := ReUploadFile(file, 1); err != nil {
log.Warn().Any("file", file).Err(err).Msg("Unable to move file to permanet storage...")
} else {
// Recycle the temporary file

View File

@ -35,6 +35,7 @@ func GetAttachmentByID(id uint) (models.Attachment, error) {
Preload("Pool").
Preload("Thumbnail").
Preload("Compressed").
Preload("Boosts").
First(&attachment).Error; err != nil {
return attachment, err
} else {
@ -64,6 +65,7 @@ func GetAttachmentByRID(rid string) (models.Attachment, error) {
Preload("Pool").
Preload("Thumbnail").
Preload("Compressed").
Preload("Boosts").
First(&attachment).Error; err != nil {
return attachment, err
} else {

View File

@ -0,0 +1,96 @@
package services
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/fs"
"git.solsynth.dev/hypernet/paperclip/pkg/internal/models"
jsoniter "github.com/json-iterator/go"
"github.com/rs/zerolog/log"
"github.com/spf13/cast"
"github.com/spf13/viper"
)
func GetBoostByID(id uint) (models.AttachmentBoost, error) {
var boost models.AttachmentBoost
if err := database.C.
Where("id = ?", id).
Preload("Attachment").
First(&boost).Error; err != nil {
return boost, err
}
return boost, nil
}
func CreateBoost(user *sec.UserInfo, source models.Attachment, destination int) (models.AttachmentBoost, error) {
boost := models.AttachmentBoost{
Status: models.BoostStatusPending,
Destination: destination,
AttachmentID: source.ID,
Attachment: source,
AccountID: user.ID,
}
dests := cast.ToSlice(viper.Get("destinations"))
if destination >= len(dests) {
return boost, fmt.Errorf("invalid destination: %d", destination)
}
if err := database.C.Create(&boost).Error; err != nil {
return boost, err
}
boost.Attachment = source
go ActivateBoost(boost)
return boost, nil
}
func ActivateBoost(boost models.AttachmentBoost) {
dests := cast.ToSlice(viper.Get("destinations"))
if boost.Destination >= len(dests) {
log.Warn().Any("boost", boost).Msg("Unable to activate boost, invalid destination...")
database.C.Model(&boost).Update("status", models.BoostStatusError)
return
}
if err := ReUploadFile(boost.Attachment, boost.Destination); err != nil {
log.Warn().Any("boost", boost).Err(err).Msg("Unable to activate boost...")
database.C.Model(&boost).Update("status", models.BoostStatusError)
return
}
log.Info().Any("boost", boost).Msg("Boost was activated successfully.")
database.C.Model(&boost).Update("status", models.BoostStatusActive)
}
func UpdateBoostStatus(boost models.AttachmentBoost, status int) (models.AttachmentBoost, error) {
if status != models.BoostStatusActive && status != models.BoostStatusSuspended {
return boost, fmt.Errorf("invalid status: %d", status)
}
err := database.C.Save(&boost).Error
return boost, err
}
func DeleteBoost(boost models.AttachmentBoost) error {
destMap := viper.GetStringMap(fmt.Sprintf("destinations.%d", boost.Destination))
var dest models.BaseDestination
rawDest, _ := jsoniter.Marshal(destMap)
_ = jsoniter.Unmarshal(rawDest, &dest)
switch dest.Type {
case models.DestinationTypeLocal:
var destConfigured models.LocalDestination
_ = jsoniter.Unmarshal(rawDest, &destConfigured)
return fs.DeleteFileFromLocal(destConfigured, boost.Attachment.Uuid)
case models.DestinationTypeS3:
var destConfigured models.S3Destination
_ = jsoniter.Unmarshal(rawDest, &destConfigured)
return fs.DeleteFileFromS3(destConfigured, boost.Attachment.Uuid)
default:
return fmt.Errorf("invalid destination: unsupported protocol %s", dest.Type)
}
}

View File

@ -9,6 +9,7 @@ import (
"path/filepath"
"git.solsynth.dev/hypernet/paperclip/pkg/internal/database"
"git.solsynth.dev/hypernet/paperclip/pkg/internal/fs"
"git.solsynth.dev/hypernet/paperclip/pkg/internal/models"
"github.com/gofiber/fiber/v2"
jsoniter "github.com/json-iterator/go"
@ -34,7 +35,7 @@ func UploadFileToTemporary(ctx *fiber.Ctx, file *multipart.FileHeader, meta mode
}
}
func ReUploadFileToPermanent(meta models.Attachment, dst int) error {
func ReUploadFile(meta models.Attachment, dst int) error {
if dst == models.AttachmentDstTemporary || meta.Destination == dst {
return fmt.Errorf("destnation cannot be reversed temporary or the same as the original")
}
@ -42,6 +43,19 @@ func ReUploadFileToPermanent(meta models.Attachment, dst int) error {
return fmt.Errorf("attachment isn't in temporary storage, unable to process")
}
prevDst := meta.Destination
inDst, err := fs.DownloadFileToLocal(meta, prevDst)
if err != nil {
return fmt.Errorf("unable to retrieve file content: %v", err)
}
cleanupDst := func() {
if prevDst == models.AttachmentDstTemporary {
return
}
os.Remove(inDst)
}
meta.Destination = dst
destMap := viper.GetStringMap(fmt.Sprintf("destinations.%d", dst))
@ -49,16 +63,6 @@ func ReUploadFileToPermanent(meta models.Attachment, dst int) error {
rawDest, _ := jsoniter.Marshal(destMap)
_ = jsoniter.Unmarshal(rawDest, &dest)
prevDestMap := viper.GetStringMap("destinations.0")
// Currently, the temporary destination only supports the local.
// So we can do this.
var prevDest models.LocalDestination
prevRawDest, _ := jsoniter.Marshal(prevDestMap)
_ = jsoniter.Unmarshal(prevRawDest, &prevDest)
inDst := filepath.Join(prevDest.Path, meta.Uuid)
switch dest.Type {
case models.DestinationTypeLocal:
var destConfigured models.LocalDestination
@ -83,6 +87,7 @@ func ReUploadFileToPermanent(meta models.Attachment, dst int) error {
database.C.Save(&meta)
CacheAttachment(meta)
cleanupDst()
return nil
case models.DestinationTypeS3:
var destConfigured models.S3Destination
@ -107,6 +112,7 @@ func ReUploadFileToPermanent(meta models.Attachment, dst int) error {
database.C.Save(&meta)
CacheAttachment(meta)
cleanupDst()
return nil
default:
return fmt.Errorf("invalid destination: unsupported protocol %s", dest.Type)