Paperclip/pkg/internal/server/api/attachments_api.go

141 lines
4.1 KiB
Go
Raw Normal View History

package api
2024-05-17 07:59:51 +00:00
import (
"fmt"
"strings"
2024-10-27 05:13:40 +00:00
"git.solsynth.dev/hypernet/nexus/pkg/nex/sec"
2024-11-02 02:41:31 +00:00
"git.solsynth.dev/hypernet/paperclip/pkg/internal/database"
"git.solsynth.dev/hypernet/paperclip/pkg/internal/server/exts"
2024-05-17 07:59:51 +00:00
2024-11-02 02:41:31 +00:00
"git.solsynth.dev/hypernet/paperclip/pkg/internal/models"
"git.solsynth.dev/hypernet/paperclip/pkg/internal/services"
2024-05-17 07:59:51 +00:00
"github.com/gofiber/fiber/v2"
)
func openAttachment(c *fiber.Ctx) error {
2024-08-18 11:53:50 +00:00
id := c.Params("id")
region := c.Query("region")
var err error
var url, mimetype string
if len(region) > 0 {
url, mimetype, err = services.OpenAttachmentByRID(id, region)
} else {
url, mimetype, err = services.OpenAttachmentByRID(id)
}
2024-05-17 07:59:51 +00:00
if err != nil {
return fiber.NewError(fiber.StatusNotFound, err.Error())
2024-05-17 07:59:51 +00:00
}
c.Set(fiber.HeaderContentType, mimetype)
2024-05-17 07:59:51 +00:00
if strings.HasPrefix(url, "file://") {
fp := strings.Replace(url, "file://", "", 1)
return c.SendFile(fp)
2024-05-17 07:59:51 +00:00
}
return c.Redirect(url, fiber.StatusFound)
2024-05-17 07:59:51 +00:00
}
func getAttachmentMeta(c *fiber.Ctx) error {
id := c.Params("id")
2024-05-17 07:59:51 +00:00
metadata, err := services.GetAttachmentByRID(id)
2024-05-17 07:59:51 +00:00
if err != nil {
return fiber.NewError(fiber.StatusNotFound)
}
return c.JSON(metadata)
}
2024-05-20 12:02:44 +00:00
func updateAttachmentMeta(c *fiber.Ctx) error {
2024-05-20 14:31:55 +00:00
id, _ := c.ParamsInt("id", 0)
2024-11-02 18:14:56 +00:00
user := c.Locals("nex_user").(*sec.UserInfo)
2024-05-20 12:02:44 +00:00
var data struct {
Thumbnail *uint `json:"thumbnail"`
Compressed *uint `json:"compressed"`
Alternative string `json:"alt"`
Metadata map[string]any `json:"metadata"`
IsIndexable bool `json:"is_indexable"`
2024-05-20 12:02:44 +00:00
}
if err := exts.BindAndValidate(c, &data); err != nil {
2024-05-20 12:02:44 +00:00
return err
}
var attachment models.Attachment
if err := database.C.
Where("id = ? AND account_id = ?", id, user.ID).
Preload("Thumbnail").
Preload("Compressed").
First(&attachment).Error; err != nil {
2024-05-20 12:02:44 +00:00
return fiber.NewError(fiber.StatusBadRequest, err.Error())
}
if data.Thumbnail != nil && attachment.ThumbnailID != data.Thumbnail {
var thumbnail models.Attachment
if err := database.C.
Where("id = ? AND account_id = ?", data.Thumbnail, user.ID).
First(&thumbnail).Error; err != nil {
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("unable find thumbnail: %v", err))
}
if attachment.Thumbnail != nil {
services.UnsetAttachmentAsThumbnail(*attachment.Thumbnail)
}
thumbnail, err := services.SetAttachmentAsThumbnail(thumbnail)
if err != nil {
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("unable set thumbnail: %v", err))
}
attachment.Thumbnail = &thumbnail
attachment.ThumbnailID = &thumbnail.ID
}
if data.Compressed != nil && attachment.CompressedID != data.Compressed {
var compressed models.Attachment
if err := database.C.
Where("id = ? AND account_id = ?", data.Compressed, user.ID).
First(&compressed).Error; err != nil {
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("unable find compressed: %v", err))
}
2024-12-28 11:26:46 +00:00
if attachment.Compressed != nil {
services.UnsetAttachmentAsCompressed(*attachment.Compressed)
}
compressed, err := services.SetAttachmentAsCompressed(compressed)
if err != nil {
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("unable set compressed: %v", err))
}
attachment.Compressed = &compressed
attachment.CompressedID = &compressed.ID
}
2024-05-20 12:02:44 +00:00
attachment.Alternative = data.Alternative
attachment.Usermeta = data.Metadata
attachment.IsIndexable = data.IsIndexable
2024-06-29 13:16:11 +00:00
if attachment, err := services.UpdateAttachment(attachment); err != nil {
2024-05-20 12:02:44 +00:00
return fiber.NewError(fiber.StatusBadRequest, err.Error())
2024-06-29 13:16:11 +00:00
} else {
return c.JSON(attachment)
2024-05-20 12:02:44 +00:00
}
}
2024-05-17 07:59:51 +00:00
func deleteAttachment(c *fiber.Ctx) error {
id, _ := c.ParamsInt("id", 0)
2024-11-02 18:14:56 +00:00
user := c.Locals("nex_user").(*sec.UserInfo)
2024-05-17 07:59:51 +00:00
attachment, err := services.GetAttachmentByID(uint(id))
if err != nil {
return fiber.NewError(fiber.StatusNotFound, err.Error())
2024-07-28 13:03:56 +00:00
} else if attachment.AccountID != user.ID {
2024-05-17 07:59:51 +00:00
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)
}
}