✨ Real attachment deletion
This commit is contained in:
parent
bdfd74eaf4
commit
93d959e7a6
@ -37,3 +37,21 @@ func uploadAttachment(c *fiber.Ctx) error {
|
||||
"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)
|
||||
}
|
||||
}
|
||||
|
@ -68,6 +68,7 @@ func NewServer() {
|
||||
CacheControl: true,
|
||||
}), readAttachment)
|
||||
api.Post("/attachments", authMiddleware, uploadAttachment)
|
||||
api.Delete("/attachments/:fileId", authMiddleware, deleteAttachment)
|
||||
|
||||
api.Get("/feed", listFeed)
|
||||
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -24,7 +24,7 @@
|
||||
<v-list-item v-for="(item, idx) in props.value" :title="getFileName(item)">
|
||||
<template #subtitle> {{ getFileType(item) }} · {{ formatBytes(item.filesize) }} </template>
|
||||
<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>
|
||||
</v-list-item>
|
||||
</v-list>
|
||||
@ -78,10 +78,18 @@ async function upload(file?: any) {
|
||||
return meta
|
||||
}
|
||||
|
||||
function detach(idx: number) {
|
||||
async function dispose(idx: number) {
|
||||
const media = JSON.parse(JSON.stringify(props.value))
|
||||
media.splice(idx)
|
||||
const item = media.splice(idx)[0]
|
||||
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 })
|
||||
|
Loading…
Reference in New Issue
Block a user