diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index fa9a36c..ab50086 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -4,9 +4,9 @@
-
+
-
+
@@ -71,7 +71,8 @@
-
+
+
true
diff --git a/pkg/server/attachments_api.go b/pkg/server/attachments_api.go
index 0821ec7..1777674 100644
--- a/pkg/server/attachments_api.go
+++ b/pkg/server/attachments_api.go
@@ -3,11 +3,12 @@ package server
import (
"context"
"fmt"
- "git.solsynth.dev/hydrogen/paperclip/pkg/grpc"
- "git.solsynth.dev/hydrogen/passport/pkg/grpc/proto"
"net/url"
"path/filepath"
+ "git.solsynth.dev/hydrogen/paperclip/pkg/grpc"
+ "git.solsynth.dev/hydrogen/passport/pkg/grpc/proto"
+
"git.solsynth.dev/hydrogen/paperclip/pkg/database"
"git.solsynth.dev/hydrogen/paperclip/pkg/models"
"git.solsynth.dev/hydrogen/paperclip/pkg/services"
@@ -15,7 +16,6 @@ import (
jsoniter "github.com/json-iterator/go"
"github.com/samber/lo"
"github.com/spf13/viper"
- "gorm.io/datatypes"
)
func openAttachment(c *fiber.Ctx) error {
@@ -101,7 +101,7 @@ func createAttachment(c *fiber.Ctx) error {
)
}
- var usermeta = make(map[string]any)
+ usermeta := make(map[string]any)
_ = jsoniter.UnmarshalFromString(c.FormValue("metadata"), &usermeta)
tx := database.C.Begin()
@@ -110,7 +110,7 @@ func createAttachment(c *fiber.Ctx) error {
HashCode: hash,
Alternative: c.FormValue("alt"),
MimeType: c.FormValue("mimetype"),
- Metadata: datatypes.JSONMap(usermeta),
+ Metadata: usermeta,
IsMature: len(c.FormValue("mature")) > 0,
Destination: destName,
})
@@ -131,6 +131,40 @@ func createAttachment(c *fiber.Ctx) error {
return c.JSON(metadata)
}
+func updateAttachmentMeta(c *fiber.Ctx) error {
+ user := c.Locals("principal").(models.Account)
+
+ var data struct {
+ Alternative string `json:"alt"`
+ Usage string `json:"usage"`
+ Metadata map[string]any `json:"metadata"`
+ IsMature bool `json:"is_mature"`
+ }
+
+ if err := BindAndValidate(c, &data); err != nil {
+ return err
+ }
+
+ var attachment models.Attachment
+ if err := database.C.Where(models.Attachment{
+ Uuid: c.Params("id"),
+ AccountID: user.ID,
+ }).Error; err != nil {
+ return fiber.NewError(fiber.StatusBadRequest, err.Error())
+ }
+
+ attachment.Alternative = data.Alternative
+ attachment.Usage = data.Usage
+ attachment.Metadata = data.Metadata
+ attachment.IsMature = data.IsMature
+
+ if err := database.C.Save(&attachment).Error; err != nil {
+ return fiber.NewError(fiber.StatusBadRequest, err.Error())
+ }
+
+ return c.JSON(attachment)
+}
+
func deleteAttachment(c *fiber.Ctx) error {
id, _ := c.ParamsInt("id", 0)
user := c.Locals("principal").(models.Account)
diff --git a/pkg/server/startup.go b/pkg/server/startup.go
index c3f004b..12ee67f 100644
--- a/pkg/server/startup.go
+++ b/pkg/server/startup.go
@@ -57,6 +57,7 @@ func NewServer() {
api.Get("/attachments/:id/meta", getAttachmentMeta)
api.Get("/attachments/:id", openAttachment)
api.Post("/attachments", authMiddleware, createAttachment)
+ api.Put("/attachments/:id", authMiddleware, updateAttachmentMeta)
api.Delete("/attachments/:id", authMiddleware, deleteAttachment)
}
}