@@ -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
3336type 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+ }
0 commit comments