aboutsummaryrefslogtreecommitdiffstats
path: root/giphyproxy/main.go
diff options
context:
space:
mode:
authorTulir Asokan <tulir@maunium.net>2024-06-19 11:47:35 +0300
committerTulir Asokan <tulir@maunium.net>2024-06-19 11:51:29 +0300
commit5da539ad8409b8902effb59a04d061516e236761 (patch)
tree42d5a9a3d10f7affbe4efbdd8dd9f5998f353ba5 /giphyproxy/main.go
parent6332613e13dd6378b58f8cf557419c127bb2ab1d (diff)
Add MSC3916-compatible giphy media repo proxy
Diffstat (limited to 'giphyproxy/main.go')
-rw-r--r--giphyproxy/main.go62
1 files changed, 62 insertions, 0 deletions
diff --git a/giphyproxy/main.go b/giphyproxy/main.go
new file mode 100644
index 0000000..279afcb
--- /dev/null
+++ b/giphyproxy/main.go
@@ -0,0 +1,62 @@
+// maunium-stickerpicker - A fast and simple Matrix sticker picker widget.
+// Copyright (C) 2024 Tulir Asokan
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+package main
+
+import (
+ "context"
+ "flag"
+ "fmt"
+ "os"
+ "regexp"
+
+ "go.mau.fi/util/exerrors"
+ "gopkg.in/yaml.v3"
+ "maunium.net/go/mautrix/federation"
+ "maunium.net/go/mautrix/mediaproxy"
+)
+
+type Config struct {
+ mediaproxy.BasicConfig `yaml:",inline"`
+ mediaproxy.ServerConfig `yaml:",inline"`
+}
+
+var configPath = flag.String("config", "config.yaml", "config file path")
+var generateServerKey = flag.Bool("generate-key", false, "generate a new server key and exit")
+
+var giphyIDRegex = regexp.MustCompile(`^[a-zA-Z0-9-_]+$`)
+
+func main() {
+ flag.Parse()
+ if *generateServerKey {
+ fmt.Println(federation.GenerateSigningKey().SynapseString())
+ } else {
+ cfgFile := exerrors.Must(os.ReadFile(*configPath))
+ var cfg Config
+ exerrors.PanicIfNotNil(yaml.Unmarshal(cfgFile, &cfg))
+ mp := exerrors.Must(mediaproxy.NewFromConfig(cfg.BasicConfig, getMedia))
+ exerrors.PanicIfNotNil(mp.Listen(cfg.ServerConfig))
+ }
+}
+
+func getMedia(_ context.Context, id string) (response mediaproxy.GetMediaResponse, err error) {
+ if !giphyIDRegex.MatchString(id) {
+ return nil, mediaproxy.ErrInvalidMediaIDSyntax
+ }
+ return &mediaproxy.GetMediaResponseURL{
+ URL: fmt.Sprintf("https://i.giphy.com/%s.webp", id),
+ }, nil
+}
send patches to the email below
yukais@pinapelz.com
include the subject [PATCH repo_name]
pinapelz.com
homepage