Skip to content

Commit 19f66b7

Browse files
committed
fix
1 parent 5a8f39a commit 19f66b7

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

api/routes.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ func RegisterRoutes(e *echo.Echo, svc *service.MessageService, pushSvc *service.
2828
e.POST("/_matrix/push/v1/notify", h.matrixPushNotify)
2929
// Matrix Application Service transactions (push events to AS)
3030
e.PUT("/_matrix/app/v1/transactions/:txnId", h.matrixAppTransaction)
31+
32+
// Matrix Media Download Proxy
33+
e.GET("/_matrix/media/v3/download/:serverName/:mediaId", h.proxyMediaDownload)
3134
}
3235

3336
type handler struct {
@@ -229,3 +232,20 @@ func (h handler) matrixAppTransaction(c echo.Context) error {
229232
// As per spec, acknowledge with an empty JSON object and 200 OK.
230233
return c.JSON(http.StatusOK, map[string]interface{}{})
231234
}
235+
236+
// proxyMediaDownload handles media download requests by proxying them to the Matrix homeserver.
237+
func (h handler) proxyMediaDownload(c echo.Context) error {
238+
serverName := c.Param("serverName")
239+
mediaId := c.Param("mediaId")
240+
mxcURL := "mxc://" + serverName + "/" + mediaId
241+
242+
logger.Debug().Str("endpoint", "proxy_media_download").Str("mxc_url", mxcURL).Msg("proxying media download request")
243+
244+
data, contentType, err := h.svc.DownloadMedia(c.Request().Context(), mxcURL)
245+
if err != nil {
246+
logger.Error().Str("endpoint", "proxy_media_download").Str("mxc_url", mxcURL).Err(err).Msg("failed to download media from matrix")
247+
return echo.NewHTTPError(http.StatusNotFound, "media not found")
248+
}
249+
250+
return c.Blob(http.StatusOK, contentType, data)
251+
}

main_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,30 @@ func TestIntegration_SendAndFetchImageMessages(t *testing.T) {
702702
ft.Attachments[0].ContentSize,
703703
ft.Attachments[0].Preview != nil)
704704
})
705+
706+
// Step 4: Download the actual image from the proxy URL
707+
t.Run("DownloadImageFromProxy", func(t *testing.T) {
708+
resp, err := http.Get(attachment.ContentURL)
709+
if err != nil {
710+
t.Fatalf("failed to download image from proxy: %v", err)
711+
}
712+
defer resp.Body.Close()
713+
714+
if resp.StatusCode != http.StatusOK {
715+
t.Errorf("expected status 200 OK, got %d", resp.StatusCode)
716+
}
717+
718+
downloadedData, err := io.ReadAll(resp.Body)
719+
if err != nil {
720+
t.Fatalf("failed to read downloaded image data: %v", err)
721+
}
722+
723+
if len(downloadedData) == 0 {
724+
t.Errorf("downloaded image data is empty")
725+
} else {
726+
t.Logf("Image downloaded successfully from proxy: %d bytes", len(downloadedData))
727+
}
728+
})
705729
})
706730
}
707731

service/messages.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -898,3 +898,8 @@ func (s *MessageService) buildMediaURL(mxcURL string) string {
898898

899899
return publicURL
900900
}
901+
902+
// DownloadMedia downloads media content from Matrix homeserver given an mxc:// URI.
903+
func (s *MessageService) DownloadMedia(ctx context.Context, mxcURL string) ([]byte, string, error) {
904+
return s.matrixClient.DownloadMedia(ctx, mxcURL)
905+
}

0 commit comments

Comments
 (0)