Add hacky option to split id into path segments in proxy

This commit is contained in:
Tulir Asokan
2026-02-18 22:17:48 +02:00
parent b89f0ce6d6
commit 4c13a2c254
+12
View File
@@ -22,6 +22,7 @@ import (
"fmt"
"os"
"regexp"
"slices"
"time"
"go.mau.fi/util/exerrors"
@@ -34,6 +35,7 @@ type Config struct {
mediaproxy.BasicConfig `yaml:",inline"`
mediaproxy.ServerConfig `yaml:",inline"`
Destination string `yaml:"destination"`
SplitIdx []int `yaml:"split_idx"`
}
var configPath = flag.String("config", "config.yaml", "config file path")
@@ -41,6 +43,7 @@ var generateServerKey = flag.Bool("generate-key", false, "generate a new server
var giphyIDRegex = regexp.MustCompile(`^[a-zA-Z0-9-_]+$`)
var destination = "https://i.giphy.com/%s.webp"
var splitIdx []int
func main() {
flag.Parse()
@@ -54,6 +57,9 @@ func main() {
mp.KeyServer.Version.Name = "mautrix-go + maunium-stickerpicker giphy proxy"
if cfg.Destination != "" {
destination = cfg.Destination
splitIdx = cfg.SplitIdx
slices.Sort(splitIdx)
slices.Reverse(splitIdx)
}
exerrors.PanicIfNotNil(mp.Listen(cfg.ServerConfig))
}
@@ -70,6 +76,12 @@ func getMedia(_ context.Context, id string, _ map[string]string) (response media
if !giphyIDRegex.MatchString(id) {
return nil, mediaproxy.ErrInvalidMediaIDSyntax
}
for _, idx := range splitIdx {
if idx >= len(id) {
return nil, mediaproxy.ErrInvalidMediaIDSyntax
}
id = id[:idx] + "/" + id[idx:]
}
return &mediaproxy.GetMediaResponseURL{
URL: fmt.Sprintf(destination, id),
}, nil