✨ Real attachment deletion
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
Reference in New Issue
Block a user