You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
1.9 KiB

  1. // maunium-stickerpicker - A fast and simple Matrix sticker picker widget.
  2. // Copyright (C) 2024 Tulir Asokan
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. package main
  17. import (
  18. "context"
  19. "flag"
  20. "fmt"
  21. "os"
  22. "regexp"
  23. "go.mau.fi/util/exerrors"
  24. "gopkg.in/yaml.v3"
  25. "maunium.net/go/mautrix/federation"
  26. "maunium.net/go/mautrix/mediaproxy"
  27. )
  28. type Config struct {
  29. mediaproxy.BasicConfig `yaml:",inline"`
  30. mediaproxy.ServerConfig `yaml:",inline"`
  31. }
  32. var configPath = flag.String("config", "config.yaml", "config file path")
  33. var generateServerKey = flag.Bool("generate-key", false, "generate a new server key and exit")
  34. var giphyIDRegex = regexp.MustCompile(`^[a-zA-Z0-9-_]+$`)
  35. func main() {
  36. flag.Parse()
  37. if *generateServerKey {
  38. fmt.Println(federation.GenerateSigningKey().SynapseString())
  39. } else {
  40. cfgFile := exerrors.Must(os.ReadFile(*configPath))
  41. var cfg Config
  42. exerrors.PanicIfNotNil(yaml.Unmarshal(cfgFile, &cfg))
  43. mp := exerrors.Must(mediaproxy.NewFromConfig(cfg.BasicConfig, getMedia))
  44. exerrors.PanicIfNotNil(mp.Listen(cfg.ServerConfig))
  45. }
  46. }
  47. func getMedia(_ context.Context, id string) (response mediaproxy.GetMediaResponse, err error) {
  48. if !giphyIDRegex.MatchString(id) {
  49. return nil, mediaproxy.ErrInvalidMediaIDSyntax
  50. }
  51. return &mediaproxy.GetMediaResponseURL{
  52. URL: fmt.Sprintf("https://i.giphy.com/%s.webp", id),
  53. }, nil
  54. }