medium.rip/pkg/converters/id_converter.go
mycodedoesnotcompile2 36e5fe60e7
Update id_converter.go
2025-12-13 12:20:37 +00:00

50 lines
832 B
Go

package converters
import (
"net/url"
"regexp"
"strings"
"github.com/gofiber/fiber/v2"
log "github.com/sirupsen/logrus"
)
var r = regexp.MustCompile(`[\/\-]([0-9a-f]+)\/?$`)
func ConvertId(c *fiber.Ctx) string {
path := c.Path()
postId := idFromParams(c)
if postId == "" {
return idFromPath(path)
}
return ""
}
func idFromPath(path string) string {
if strings.HasPrefix(path, "/tag/") {
return ""
}
matches := r.FindStringSubmatch(path)
if len(matches) != 2 {
return ""
}
return matches[1]
}
func idFromParams(c *fiber.Ctx) string {
ru := c.Query("redirectUrl", "")
//log.Printf("REQ: %s", string(c.Request().String()))
//log.Printf("RESP: %s", string(c.Response().String()))
if ru != "" {
pu, err := url.Parse(ru)
if err != nil {
return ""
}
return idFromPath(pu.Path)
}
return ""
}