Thumbnail

rani/matterbridge.git

Clone URL: https://git.buni.party/rani/matterbridge.git

commit 5d530f0ba96f2958c8ecd6abcc99b919f43bd7c2 Author: selfhoster1312 <selfhoster1312@kl.netlib.re> Date: Sat Nov 22 13:21:13 2025 +0000 fixup: Use the new helpers (todo: squash this) diff --git a/bridge/mastodon/mastodon.go b/bridge/mastodon/mastodon.go index e454ba6..5f2b85c 100644 --- a/bridge/mastodon/mastodon.go +++ b/bridge/mastodon/mastodon.go @@ -513 +512 @@ import (   "context"   "errors"   "fmt" - "io" - "net/http"   "regexp"   "strings"     "github.com/42wim/matterbridge/bridge"   "github.com/42wim/matterbridge/bridge/config" + "github.com/42wim/matterbridge/bridge/helper"     mastodon "github.com/mattn/go-mastodon"  ) @@ -3024 +296 @@ func InvalidChannelError(name string) error {   return fmt.Errorf("%w: %s", errInvalidChannel, name)  }   -var errHttpGet = errors.New("failed to get HTTP file") - -func HttpGetError(url string) error { - return fmt.Errorf("%w: %s", errHttpGet, url) -} - -var errHttpGetNotOk = errors.New("HTTP server responded non-OK code") - -func HttpGetNotOkError(url string, code int) error { - return fmt.Errorf("%w: %s returned code %d", errHttpGetNotOk, url, code) -} - -var errFileCast = errors.New("failed to cast config.FileInfo") - -func FileCastError() error { - return fmt.Errorf("%w", errFileCast) -} -  type Bmastodon struct {   *bridge.Config   @@ -20931 +19016 @@ func (b *Bmastodon) handleSendRemoteStatus(msg *mastodon.Status, channel string)   }     for _, media := range msg.MediaAttachments { - resp, err2 := http.Get(media.RemoteURL) + b, err2 := helper.DownloadFile(media.RemoteURL)   if err2 != nil { - continue - } - - defer func() { - err := resp.Body.Close() - if err != nil { - b.Log.Warn(err) - } - }() - - if resp.StatusCode != http.StatusOK { - continue - } - - b, err := io.ReadAll(resp.Body) - if err != nil { + // TODO: log   continue   }     remoteMessage.Extra["file"] = append(remoteMessage.Extra["file"], config.FileInfo{   Name: media.Description, - Data: &b, - Size: int64(len(b)), + Data: b, + Size: int64(len(*b)),   Avatar: false,   })   } @@ -2688 +23411 @@ func (b *Bmastodon) handleSendingMessage(ctx context.Context, msg *config.Messag   }   }   - for _, file := range msg.Extra["file"] { - attachment, err := b.extractFile(ctx, file) + for _, file := range *msg.GetFileInfos(b.Log) { + attachment, err := b.c.UploadMediaFromMedia(ctx, &mastodon.Media{ + File: bytes.NewReader(*file.Data), + Description: file.Comment, + })   if err != nil {   b.Log.Error(err)   continue @@ -28050 +2493 @@ func (b *Bmastodon) handleSendingMessage(ctx context.Context, msg *config.Messag     return b.c.PostStatus(ctx, &toot)  } - -func (b *Bmastodon) extractFile(ctx context.Context, file interface{}) (*mastodon.Attachment, error) { - fileInfo, ok := file.(config.FileInfo) - if !ok { - return nil, FileCastError() - } - - var ( - r io.Reader - err error - resp *http.Response - ) - - defer func() { - if resp != nil { - err2 := resp.Body.Close() - if err2 != nil { - b.Log.Warn(err) - } - } - }() - - if fileInfo.URL != "" { - resp, err = http.Get(fileInfo.URL) - if err != nil { - return nil, HttpGetError(fileInfo.URL) - } - - if resp.StatusCode != http.StatusOK { - return nil, HttpGetNotOkError(fileInfo.URL, resp.StatusCode) - } - - r = resp.Body - } else if fileInfo.Data != nil { - r = bytes.NewReader(*fileInfo.Data) - } - - attachment, err := b.c.UploadMediaFromMedia(ctx, &mastodon.Media{ - File: r, - Description: fileInfo.Comment, - }) - if err != nil { - return nil, err - } - - return attachment, nil -} diff --git a/gateway/bridgemap/bmastodon.go b/gateway/bridgemap/bmastodon.go index b51f66a..64b7f9e 100644 --- a/gateway/bridgemap/bmastodon.go +++ b/gateway/bridgemap/bmastodon.go @@ -15 +14 @@  //go:build !nomastodon -// +build !nomastodon    package bridgemap