Skip to content

Commit

Permalink
✨ Able to directly open (get the image) of a sticker
Browse files Browse the repository at this point in the history
  • Loading branch information
LittleSheep2Code committed Jan 6, 2025
1 parent 8cd1037 commit 8bcf02f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
3 changes: 2 additions & 1 deletion pkg/internal/server/api/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
35 changes: 33 additions & 2 deletions pkg/internal/server/api/stickers_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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())
Expand All @@ -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)
Expand Down

0 comments on commit 8bcf02f

Please sign in to comment.