From 8bcf02fa5ec49a463bab9f45f43eedfffc0a96f6 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Mon, 6 Jan 2025 21:51:26 +0800 Subject: [PATCH] :sparkles: Able to directly open (get the image) of a sticker --- pkg/internal/server/api/index.go | 3 ++- pkg/internal/server/api/stickers_api.go | 35 +++++++++++++++++++++++-- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/pkg/internal/server/api/index.go b/pkg/internal/server/api/index.go index fa1f960..b256fea 100644 --- a/pkg/internal/server/api/index.go +++ b/pkg/internal/server/api/index.go @@ -49,7 +49,8 @@ func MapAPIs(app *fiber.App, baseURL string) { stickers := api.Group("/stickers").Name("Stickers API") { stickers.Get("/lookup", lookupStickerBatch) - stickers.Get("/lookup/:alias", lookupSticker) + stickers.Get("/lookup/:alias", getStickerByAlias) + stickers.Get("/lookup/:alias/open", openStickerByAlias) stickers.Get("/", listStickers) stickers.Get("/:stickerId", getSticker) diff --git a/pkg/internal/server/api/stickers_api.go b/pkg/internal/server/api/stickers_api.go index 22fd828..37eed1d 100644 --- a/pkg/internal/server/api/stickers_api.go +++ b/pkg/internal/server/api/stickers_api.go @@ -2,10 +2,11 @@ package api import ( "fmt" + "strings" + "git.solsynth.dev/hypernet/nexus/pkg/nex/sec" "git.solsynth.dev/hypernet/paperclip/pkg/internal/gap" "git.solsynth.dev/hypernet/passport/pkg/authkit" - "strings" "git.solsynth.dev/hypernet/paperclip/pkg/internal/database" "git.solsynth.dev/hypernet/paperclip/pkg/internal/models" @@ -23,7 +24,7 @@ func lookupStickerBatch(c *fiber.Ctx) error { } } -func lookupSticker(c *fiber.Ctx) error { +func getStickerByAlias(c *fiber.Ctx) error { alias := c.Params("alias") if sticker, err := services.GetStickerWithAlias(alias); err != nil { return fiber.NewError(fiber.StatusNotFound, err.Error()) @@ -32,6 +33,36 @@ func lookupSticker(c *fiber.Ctx) error { } } +func openStickerByAlias(c *fiber.Ctx) error { + alias := c.Params("alias") + region := c.Query("region") + + sticker, err := services.GetStickerWithAlias(alias) + if err != nil { + return fiber.NewError(fiber.StatusNotFound, err.Error()) + } + + var url, mimetype string + if len(region) > 0 { + url, mimetype, err = services.OpenAttachmentByRID(sticker.Attachment.Rid, region) + } else { + url, mimetype, err = services.OpenAttachmentByRID(sticker.Attachment.Rid) + } + + if err != nil { + return fiber.NewError(fiber.StatusNotFound, err.Error()) + } + + c.Set(fiber.HeaderContentType, mimetype) + + if strings.HasPrefix(url, "file://") { + fp := strings.Replace(url, "file://", "", 1) + return c.SendFile(fp) + } + + return c.Redirect(url, fiber.StatusFound) +} + func listStickers(c *fiber.Ctx) error { take := c.QueryInt("take", 0) offset := c.QueryInt("offset", 0)