Skip to content

Commit

Permalink
Adjust logs and update mautrix-go
Browse files Browse the repository at this point in the history
  • Loading branch information
tulir committed Feb 18, 2024
1 parent 389f431 commit 08cde63
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 5 deletions.
9 changes: 9 additions & 0 deletions directmedia.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"strconv"
"strings"
"sync"
"sync/atomic"
"time"

"github.com/bwmarrin/discordgo"
Expand Down Expand Up @@ -102,6 +103,7 @@ func newDirectMediaAPI(br *DiscordBridge) *DirectMediaAPI {
dma.ks.WellKnownTarget = fmt.Sprintf("%s:443", dma.cfg.ServerName)
}
mediaRouter := r.PathPrefix("/_matrix/media").Subrouter()
var reqIDCounter atomic.Uint64
mediaRouter.Use(func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
Expand All @@ -110,6 +112,7 @@ func newDirectMediaAPI(br *DiscordBridge) *DirectMediaAPI {
log := dma.log.With().
Str("remote_addr", r.RemoteAddr).
Str("request_path", r.URL.Path).
Uint64("req_id", reqIDCounter.Add(1)).
Logger()
next.ServeHTTP(w, r.WithContext(log.WithContext(r.Context())))
})
Expand Down Expand Up @@ -310,6 +313,11 @@ func (dma *DirectMediaAPI) GetMediaURL(ctx context.Context, encodedMediaID strin
if ok && time.Until(cached.Expiry) > 5*time.Minute {
return cached.URL, cached.Expiry, nil
}
zerolog.Ctx(ctx).Debug().
Uint64("channel_id", mediaData.AttachmentMediaDataInner.ChannelID).
Uint64("message_id", mediaData.AttachmentMediaDataInner.MessageID).
Uint64("attachment_id", mediaData.AttachmentMediaDataInner.AttachmentID).
Msg("Refreshing attachment URL")
url, err = dma.FetchNewAttachmentURL(ctx, mediaData.AttachmentMediaDataInner)
if err != nil {
zerolog.Ctx(ctx).Err(err).Msg("Failed to refresh attachment URL")
Expand All @@ -325,6 +333,7 @@ func (dma *DirectMediaAPI) GetMediaURL(ctx context.Context, encodedMediaID strin
Status: http.StatusNotFound,
}
} else {
zerolog.Ctx(ctx).Debug().Msg("Successfully refreshed attachment URL")
// TODO find expiry somehow properly?
expiry = time.Now().Add(23 * time.Hour)
dma.attachmentCache[mediaData.AttachmentMediaDataInner.CacheKey()] = AttachmentCacheValue{
Expand Down
4 changes: 2 additions & 2 deletions directmedia_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func (amd *AttachmentMediaData) Read(from io.Reader) (err error) {
}

func (amd *AttachmentMediaData) Size() int {
return binary.Size(amd.AttachmentMediaDataInner) + len(amd.FileName)
return binary.Size(&amd.AttachmentMediaDataInner) + len(amd.FileName)
}

func (amd *AttachmentMediaData) Wrap() *MediaID {
Expand Down Expand Up @@ -218,7 +218,7 @@ func (emd *EmojiMediaData) Read(from io.Reader) (err error) {
}

func (emd *EmojiMediaData) Size() int {
return binary.Size(emd.EmojiMediaDataInner) + len(emd.Name)
return binary.Size(&emd.EmojiMediaDataInner) + len(emd.Name)
}

func (emd *EmojiMediaData) Wrap() *MediaID {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ require (
golang.org/x/exp v0.0.0-20231219180239-dc181d75b848
golang.org/x/sync v0.5.0
maunium.net/go/maulogger/v2 v2.4.1
maunium.net/go/mautrix v0.16.3-0.20240218185415-137ba4400f5f
maunium.net/go/mautrix v0.16.3-0.20240218194839-cfb0ed63142b
)

require (
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,5 @@ maunium.net/go/mauflag v1.0.0 h1:YiaRc0tEI3toYtJMRIfjP+jklH45uDHtT80nUamyD4M=
maunium.net/go/mauflag v1.0.0/go.mod h1:nLivPOpTpHnpzEh8jEdSL9UqO9+/KBJFmNRlwKfkPeA=
maunium.net/go/maulogger/v2 v2.4.1 h1:N7zSdd0mZkB2m2JtFUsiGTQQAdP0YeFWT7YMc80yAL8=
maunium.net/go/maulogger/v2 v2.4.1/go.mod h1:omPuYwYBILeVQobz8uO3XC8DIRuEb5rXYlQSuqrbCho=
maunium.net/go/mautrix v0.16.3-0.20240218185415-137ba4400f5f h1:6iSrRD7v3NL6KZ8mmsrJ2R7OnAu2XW6oyIa+DyK6EB8=
maunium.net/go/mautrix v0.16.3-0.20240218185415-137ba4400f5f/go.mod h1:YL4l4rZB46/vj/ifRMEjcibbvHjgxHftOF1SgmruLu4=
maunium.net/go/mautrix v0.16.3-0.20240218194839-cfb0ed63142b h1:D4wvtLYOsOL0P4J/4Cna37euNVBFOEdK0GOFlN14NkU=
maunium.net/go/mautrix v0.16.3-0.20240218194839-cfb0ed63142b/go.mod h1:YL4l4rZB46/vj/ifRMEjcibbvHjgxHftOF1SgmruLu4=

0 comments on commit 08cde63

Please sign in to comment.