Real attachment deletion

This commit is contained in:
2024-03-24 18:31:36 +08:00
parent bdfd74eaf4
commit 93d959e7a6
4 changed files with 56 additions and 3 deletions

View File

@@ -3,13 +3,26 @@ package services
import (
"mime/multipart"
"net/http"
"os"
"path/filepath"
"strings"
"git.solsynth.dev/hydrogen/interactive/pkg/database"
"git.solsynth.dev/hydrogen/interactive/pkg/models"
"github.com/google/uuid"
"github.com/spf13/viper"
)
func GetAttachmentByUUID(fileId string) (models.Attachment, error) {
var attachment models.Attachment
if err := database.C.Where(models.Attachment{
FileID: fileId,
}).First(&attachment).Error; err != nil {
return attachment, err
}
return attachment, nil
}
func NewAttachment(user models.Account, header *multipart.FileHeader) (models.Attachment, error) {
attachment := models.Attachment{
FileID: uuid.NewString(),
@@ -51,3 +64,16 @@ func NewAttachment(user models.Account, header *multipart.FileHeader) (models.At
return attachment, err
}
func DeleteAttachment(item models.Attachment) error {
if err := database.C.Delete(&item).Error; err != nil {
return err
} else {
basepath := viper.GetString("content")
fullpath := filepath.Join(basepath, item.FileID)
os.Remove(fullpath)
}
return nil
}