Instant upload

This commit is contained in:
2024-03-24 19:01:18 +08:00
parent 93d959e7a6
commit fb0c7860e0
6 changed files with 110 additions and 37 deletions

View File

@ -18,12 +18,16 @@ func readAttachment(c *fiber.Ctx) error {
func uploadAttachment(c *fiber.Ctx) error {
user := c.Locals("principal").(models.Account)
hashcode := c.FormValue("hashcode")
if len(hashcode) != 64 {
return fiber.NewError(fiber.StatusBadRequest, "please provide a SHA256 hashcode, length should be 64 characters")
}
file, err := c.FormFile("attachment")
if err != nil {
return err
}
attachment, err := services.NewAttachment(user, file)
attachment, err := services.NewAttachment(user, file, hashcode)
if err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
}
@ -39,10 +43,10 @@ func uploadAttachment(c *fiber.Ctx) error {
}
func deleteAttachment(c *fiber.Ctx) error {
id := c.Params("fileId")
id, _ := c.ParamsInt("id", 0)
user := c.Locals("principal").(models.Account)
attachment, err := services.GetAttachmentByUUID(id)
attachment, err := services.GetAttachmentByID(uint(id))
if err != nil {
return fiber.NewError(fiber.StatusNotFound, err.Error())
} else if attachment.AuthorID != user.ID {

View File

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