Real attachment deletion

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

View File

@ -37,3 +37,21 @@ func uploadAttachment(c *fiber.Ctx) error {
"url": attachment.GetAccessPath(), "url": attachment.GetAccessPath(),
}) })
} }
func deleteAttachment(c *fiber.Ctx) error {
id := c.Params("fileId")
user := c.Locals("principal").(models.Account)
attachment, err := services.GetAttachmentByUUID(id)
if err != nil {
return fiber.NewError(fiber.StatusNotFound, err.Error())
} else if attachment.AuthorID != user.ID {
return fiber.NewError(fiber.StatusNotFound, "record not created by you")
}
if err := services.DeleteAttachment(attachment); err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
} else {
return c.SendStatus(fiber.StatusOK)
}
}

View File

@ -68,6 +68,7 @@ func NewServer() {
CacheControl: true, CacheControl: true,
}), readAttachment) }), readAttachment)
api.Post("/attachments", authMiddleware, uploadAttachment) api.Post("/attachments", authMiddleware, uploadAttachment)
api.Delete("/attachments/:fileId", authMiddleware, deleteAttachment)
api.Get("/feed", listFeed) api.Get("/feed", listFeed)

View File

@ -3,13 +3,26 @@ package services
import ( import (
"mime/multipart" "mime/multipart"
"net/http" "net/http"
"os"
"path/filepath"
"strings" "strings"
"git.solsynth.dev/hydrogen/interactive/pkg/database" "git.solsynth.dev/hydrogen/interactive/pkg/database"
"git.solsynth.dev/hydrogen/interactive/pkg/models" "git.solsynth.dev/hydrogen/interactive/pkg/models"
"github.com/google/uuid" "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) { func NewAttachment(user models.Account, header *multipart.FileHeader) (models.Attachment, error) {
attachment := models.Attachment{ attachment := models.Attachment{
FileID: uuid.NewString(), FileID: uuid.NewString(),
@ -51,3 +64,16 @@ func NewAttachment(user models.Account, header *multipart.FileHeader) (models.At
return attachment, err 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
}

View File

@ -24,7 +24,7 @@
<v-list-item v-for="(item, idx) in props.value" :title="getFileName(item)"> <v-list-item v-for="(item, idx) in props.value" :title="getFileName(item)">
<template #subtitle> {{ getFileType(item) }} · {{ formatBytes(item.filesize) }} </template> <template #subtitle> {{ getFileType(item) }} · {{ formatBytes(item.filesize) }} </template>
<template #append> <template #append>
<v-btn icon="mdi-delete" size="small" variant="text" color="error" @click="detach(idx)" /> <v-btn icon="mdi-delete" size="small" variant="text" color="error" @click="dispose(idx)" />
</template> </template>
</v-list-item> </v-list-item>
</v-list> </v-list>
@ -78,10 +78,18 @@ async function upload(file?: any) {
return meta return meta
} }
function detach(idx: number) { async function dispose(idx: number) {
const media = JSON.parse(JSON.stringify(props.value)) const media = JSON.parse(JSON.stringify(props.value))
media.splice(idx) const item = media.splice(idx)[0]
emits("update:value", media) emits("update:value", media)
const res = await request(`/api/attachments/${item.file_id}`, {
method: "DELETE",
headers: { Authorization: `Bearer ${getAtk()}` },
})
if (res.status !== 200) {
error.value = await res.text()
}
} }
defineExpose({ upload }) defineExpose({ upload })