2024-06-22 04:29:20 +00:00
|
|
|
package api
|
2024-05-17 07:59:51 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
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-08-18 13:10:10 +00:00
|
|
|
"net/url"
|
|
|
|
"path/filepath"
|
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"
|
|
|
|
jsoniter "github.com/json-iterator/go"
|
|
|
|
"github.com/samber/lo"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
|
|
|
func openAttachment(c *fiber.Ctx) error {
|
2024-08-18 11:53:50 +00:00
|
|
|
id := c.Params("id")
|
2024-05-17 07:59:51 +00:00
|
|
|
|
2024-08-18 13:10:10 +00:00
|
|
|
metadata, err := services.GetAttachmentByRID(id)
|
2024-05-17 07:59:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusNotFound)
|
2024-08-20 09:08:40 +00:00
|
|
|
} else if !metadata.IsUploaded {
|
|
|
|
return fiber.NewError(fiber.StatusNotFound, "file is in uploading progress, please wait until all chunk uploaded")
|
2024-05-17 07:59:51 +00:00
|
|
|
}
|
|
|
|
|
2024-07-28 13:03:56 +00:00
|
|
|
var destMap map[string]any
|
|
|
|
if metadata.Destination == models.AttachmentDstTemporary {
|
|
|
|
destMap = viper.GetStringMap("destinations.temporary")
|
|
|
|
} else {
|
|
|
|
destMap = viper.GetStringMap("destinations.permanent")
|
2024-05-17 07:59:51 +00:00
|
|
|
}
|
|
|
|
|
2024-07-28 13:03:56 +00:00
|
|
|
var dest models.BaseDestination
|
|
|
|
rawDest, _ := jsoniter.Marshal(destMap)
|
|
|
|
_ = jsoniter.Unmarshal(rawDest, &dest)
|
2024-05-17 07:59:51 +00:00
|
|
|
|
2024-07-28 13:03:56 +00:00
|
|
|
switch dest.Type {
|
2024-05-17 07:59:51 +00:00
|
|
|
case models.DestinationTypeLocal:
|
|
|
|
var destConfigured models.LocalDestination
|
|
|
|
_ = jsoniter.Unmarshal(rawDest, &destConfigured)
|
2024-06-01 14:15:41 +00:00
|
|
|
if len(metadata.MimeType) > 0 {
|
|
|
|
c.Set(fiber.HeaderContentType, metadata.MimeType)
|
|
|
|
}
|
|
|
|
return c.SendFile(filepath.Join(destConfigured.Path, metadata.Uuid), false)
|
2024-05-17 07:59:51 +00:00
|
|
|
case models.DestinationTypeS3:
|
|
|
|
var destConfigured models.S3Destination
|
|
|
|
_ = jsoniter.Unmarshal(rawDest, &destConfigured)
|
2024-07-29 05:22:57 +00:00
|
|
|
if len(destConfigured.AccessBaseURL) > 0 {
|
|
|
|
return c.Redirect(fmt.Sprintf(
|
|
|
|
"%s/%s",
|
|
|
|
destConfigured.AccessBaseURL,
|
|
|
|
url.QueryEscape(filepath.Join(destConfigured.Path, metadata.Uuid)),
|
|
|
|
), fiber.StatusMovedPermanently)
|
|
|
|
} else {
|
|
|
|
protocol := lo.Ternary(destConfigured.EnableSSL, "https", "http")
|
|
|
|
return c.Redirect(fmt.Sprintf(
|
|
|
|
"%s://%s.%s/%s",
|
|
|
|
protocol,
|
|
|
|
destConfigured.Bucket,
|
|
|
|
destConfigured.Endpoint,
|
|
|
|
url.QueryEscape(filepath.Join(destConfigured.Path, metadata.Uuid)),
|
|
|
|
), fiber.StatusMovedPermanently)
|
|
|
|
}
|
2024-05-17 07:59:51 +00:00
|
|
|
default:
|
2024-07-28 13:03:56 +00:00
|
|
|
return fmt.Errorf("invalid destination: unsupported protocol %s", dest.Type)
|
2024-05-17 07:59:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getAttachmentMeta(c *fiber.Ctx) error {
|
2024-08-18 13:10:10 +00:00
|
|
|
id := c.Params("id")
|
2024-05-17 07:59:51 +00:00
|
|
|
|
2024-08-18 13:10:10 +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-10-27 05:13:40 +00:00
|
|
|
user := c.Locals("nex_user").(sec.UserInfo)
|
2024-05-20 12:02:44 +00:00
|
|
|
|
|
|
|
var data struct {
|
2024-09-10 13:31:51 +00:00
|
|
|
Alternative string `json:"alt"`
|
|
|
|
Metadata map[string]any `json:"metadata"`
|
|
|
|
IsMature bool `json:"is_mature"`
|
2024-05-20 12:02:44 +00:00
|
|
|
}
|
|
|
|
|
2024-06-22 04:29:20 +00:00
|
|
|
if err := exts.BindAndValidate(c, &data); err != nil {
|
2024-05-20 12:02:44 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var attachment models.Attachment
|
2024-07-25 14:15:06 +00:00
|
|
|
if err := database.C.Where("id = ? AND account_id = ?", id, user.ID).First(&attachment).Error; err != nil {
|
2024-05-20 12:02:44 +00:00
|
|
|
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
attachment.Alternative = data.Alternative
|
2024-09-10 13:31:51 +00:00
|
|
|
attachment.Metadata = data.Metadata
|
2024-05-20 12:02:44 +00:00
|
|
|
attachment.IsMature = data.IsMature
|
|
|
|
|
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-10-27 05:13:40 +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)
|
|
|
|
}
|
|
|
|
}
|