:sparklesS: Updatable attachment info

This commit is contained in:
LittleSheep 2024-05-20 20:02:44 +08:00
parent a5a6503af9
commit ff450ddd9d
3 changed files with 44 additions and 8 deletions

View File

@ -4,9 +4,9 @@
<option name="autoReloadType" value="ALL" />
</component>
<component name="ChangeListManager">
<list default="true" id="18dd0d68-b4b8-40db-9734-9119b5c848bd" name="更改" comment=":truck: Update url mapping">
<list default="true" id="18dd0d68-b4b8-40db-9734-9119b5c848bd" name="更改" comment=":bug: Fix uuid duplicate when link exists">
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/services/attachments.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/services/attachments.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/server/attachments_api.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/server/attachments_api.go" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -71,7 +71,8 @@
<MESSAGE value=":sparkles: Upload attachment requires permission check" />
<MESSAGE value=":sparkles: Provide a faster check attachment exists grpc method" />
<MESSAGE value=":truck: Update url mapping" />
<option name="LAST_COMMIT_MESSAGE" value=":truck: Update url mapping" />
<MESSAGE value=":bug: Fix uuid duplicate when link exists" />
<option name="LAST_COMMIT_MESSAGE" value=":bug: Fix uuid duplicate when link exists" />
</component>
<component name="VgoProject">
<settings-migrated>true</settings-migrated>

View File

@ -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)

View File

@ -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)
}
}