✨ Referenced external attachment
This commit is contained in:
@@ -2,8 +2,8 @@ package api
|
||||
|
||||
import (
|
||||
"git.solsynth.dev/hypernet/nexus/pkg/nex/sec"
|
||||
"git.solsynth.dev/hypernet/paperclip/pkg/internal/database"
|
||||
"git.solsynth.dev/hypernet/paperclip/pkg/filekit/models"
|
||||
"git.solsynth.dev/hypernet/paperclip/pkg/internal/database"
|
||||
"git.solsynth.dev/hypernet/paperclip/pkg/internal/server/exts"
|
||||
"git.solsynth.dev/hypernet/paperclip/pkg/internal/services"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
|
@@ -37,6 +37,7 @@ func MapAPIs(app *fiber.App, baseURL string) {
|
||||
attachments.Get("/:id/meta", getAttachmentMeta)
|
||||
attachments.Get("/:id", openAttachment)
|
||||
attachments.Post("/", sec.ValidatorMiddleware, createAttachmentDirectly)
|
||||
attachments.Post("/referenced", sec.ValidatorMiddleware, createAttachmentWithURL)
|
||||
attachments.Put("/:id", sec.ValidatorMiddleware, updateAttachmentMeta)
|
||||
attachments.Delete("/:id", sec.ValidatorMiddleware, deleteAttachment)
|
||||
}
|
||||
|
@@ -2,9 +2,9 @@ package api
|
||||
|
||||
import (
|
||||
"git.solsynth.dev/hypernet/nexus/pkg/nex/sec"
|
||||
"git.solsynth.dev/hypernet/paperclip/pkg/filekit/models"
|
||||
"git.solsynth.dev/hypernet/paperclip/pkg/internal/database"
|
||||
"git.solsynth.dev/hypernet/paperclip/pkg/internal/gap"
|
||||
"git.solsynth.dev/hypernet/paperclip/pkg/filekit/models"
|
||||
"git.solsynth.dev/hypernet/paperclip/pkg/internal/server/exts"
|
||||
"git.solsynth.dev/hypernet/paperclip/pkg/internal/services"
|
||||
"git.solsynth.dev/hypernet/passport/pkg/authkit"
|
||||
|
@@ -2,10 +2,11 @@ package api
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"git.solsynth.dev/hypernet/paperclip/pkg/internal/server/exts"
|
||||
|
||||
"git.solsynth.dev/hypernet/nexus/pkg/nex/sec"
|
||||
"git.solsynth.dev/hypernet/paperclip/pkg/internal/database"
|
||||
"git.solsynth.dev/hypernet/paperclip/pkg/filekit/models"
|
||||
"git.solsynth.dev/hypernet/paperclip/pkg/internal/database"
|
||||
"git.solsynth.dev/hypernet/paperclip/pkg/internal/services"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
@@ -81,3 +82,50 @@ func createAttachmentDirectly(c *fiber.Ctx) error {
|
||||
|
||||
return c.JSON(metadata)
|
||||
}
|
||||
|
||||
func createAttachmentWithURL(c *fiber.Ctx) error {
|
||||
user := c.Locals("nex_user").(*sec.UserInfo)
|
||||
|
||||
poolAlias := c.FormValue("pool")
|
||||
aliasingMap := viper.GetStringMapString("pools.aliases")
|
||||
if val, ok := aliasingMap[poolAlias]; ok {
|
||||
poolAlias = val
|
||||
}
|
||||
|
||||
var data struct {
|
||||
URL string `json:"url"`
|
||||
Metadata map[string]any `json:"metadata"`
|
||||
Mimetype string `json:"mimetype"`
|
||||
Name string `json:"filename"`
|
||||
Alternative string `json:"alt"`
|
||||
}
|
||||
if err := exts.BindAndValidate(c, &data); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if !user.HasPermNode("CreateReferencedAttachments", true) {
|
||||
return fiber.NewError(fiber.StatusForbidden, "you are not permitted to create attachments with URL")
|
||||
}
|
||||
|
||||
pool, err := services.GetAttachmentPoolByAlias(poolAlias)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("unable to get attachment pool info: %v", err))
|
||||
}
|
||||
|
||||
attachment := models.Attachment{
|
||||
Name: data.Name,
|
||||
Alternative: data.Alternative,
|
||||
MimeType: c.FormValue("mimetype"),
|
||||
Usermeta: data.Metadata,
|
||||
IsAnalyzed: true,
|
||||
Destination: models.AttachmentDstExternal,
|
||||
Pool: &pool,
|
||||
PoolID: &pool.ID,
|
||||
}
|
||||
|
||||
if attachment, err = services.NewRefURLAttachment(database.C, user, attachment); err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
}
|
||||
|
||||
return c.JSON(attachment)
|
||||
}
|
||||
|
@@ -5,9 +5,9 @@ import (
|
||||
"fmt"
|
||||
|
||||
"git.solsynth.dev/hypernet/nexus/pkg/nex/sec"
|
||||
"git.solsynth.dev/hypernet/paperclip/pkg/filekit/models"
|
||||
"git.solsynth.dev/hypernet/paperclip/pkg/internal/database"
|
||||
"git.solsynth.dev/hypernet/paperclip/pkg/internal/fs"
|
||||
"git.solsynth.dev/hypernet/paperclip/pkg/filekit/models"
|
||||
"git.solsynth.dev/hypernet/paperclip/pkg/internal/server/exts"
|
||||
"git.solsynth.dev/hypernet/paperclip/pkg/internal/services"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
|
Reference in New Issue
Block a user