From 4c13a2c254d68980bcf43f132e0b78e3c08ed3b1 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Wed, 18 Feb 2026 22:17:48 +0200 Subject: [PATCH] Add hacky option to split id into path segments in proxy --- giphyproxy/main.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/giphyproxy/main.go b/giphyproxy/main.go index c8bed12..3bf1620 100644 --- a/giphyproxy/main.go +++ b/giphyproxy/main.go @@ -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