Skip to content

Commit

Permalink
Support and use new 3916v2 federation download URL
Browse files Browse the repository at this point in the history
  • Loading branch information
turt2live committed May 23, 2024
1 parent 26fad93 commit 5b49457
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 4 deletions.
3 changes: 2 additions & 1 deletion api/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func buildRoutes() http.Handler {
register([]string{"GET"}, PrefixClient, "media/download/:server/:mediaId", msc3916, router, authedDownloadRoute)
register([]string{"GET"}, PrefixClient, "media/thumbnail/:server/:mediaId", msc3916, router, makeRoute(_routers.RequireAccessToken(r0.ThumbnailMedia), "thumbnail", counter))
register([]string{"GET"}, PrefixFederation, "media/download/:server/:mediaId", msc3916, router, makeRoute(_routers.RequireServerAuth(unstable.FederationDownloadMedia), "download", counter))
register([]string{"GET"}, PrefixFederation, "media/download/:mediaId", msc3916, router, makeRoute(_routers.RequireServerAuth(unstable.FederationDownloadMedia), "download", counter))
register([]string{"GET"}, PrefixFederation, "media/thumbnail/:server/:mediaId", msc3916, router, makeRoute(_routers.RequireServerAuth(unstable.FederationThumbnailMedia), "thumbnail", counter))

// Custom features
Expand Down Expand Up @@ -143,7 +144,7 @@ var (
//mxAllSpec matrixVersions = []string{"r0", "v1", "v3", "unstable", "unstable/io.t2bot.media" /* and MSC routes */}
mxUnstable matrixVersions = []string{"unstable", "unstable/io.t2bot.media"}
msc4034 matrixVersions = []string{"unstable/org.matrix.msc4034"}
msc3916 matrixVersions = []string{"unstable/org.matrix.msc3916"}
msc3916 matrixVersions = []string{"unstable/org.matrix.msc3916", "unstable/org.matrix.msc3916.v2"}
mxSpecV3Transition matrixVersions = []string{"r0", "v1", "v3"}
mxSpecV3TransitionCS matrixVersions = []string{"r0", "v3"}
mxR0 matrixVersions = []string{"r0"}
Expand Down
2 changes: 2 additions & 0 deletions api/unstable/msc3916_download.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/t2bot/matrix-media-repo/api/_apimeta"
"github.com/t2bot/matrix-media-repo/api/_responses"
"github.com/t2bot/matrix-media-repo/api/_routers"
"github.com/t2bot/matrix-media-repo/api/r0"
"github.com/t2bot/matrix-media-repo/common/rcontext"
"github.com/t2bot/matrix-media-repo/util/readers"
Expand All @@ -18,6 +19,7 @@ func ClientDownloadMedia(r *http.Request, rctx rcontext.RequestContext, user _ap

func FederationDownloadMedia(r *http.Request, rctx rcontext.RequestContext, server _apimeta.ServerInfo) interface{} {
r.URL.Query().Set("allow_remote", "false")
r = _routers.ForceSetParam("server", r.Host, r)

res := r0.DownloadMedia(r, rctx, _apimeta.UserInfo{})
if dl, ok := res.(*_responses.DownloadResponse); ok {
Expand Down
2 changes: 1 addition & 1 deletion pipelines/_steps/download/try_download.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func TryDownload(ctx rcontext.RequestContext, origin string, mediaId string) (*d
var downloadUrl string
usesMultipartFormat := false
if ctx.Config.SigningKeyPath != "" {
downloadUrl = fmt.Sprintf("%s/_matrix/federation/unstable/org.matrix.msc3916/media/download/%s/%s?a=b", baseUrl, url.PathEscape(origin), url.PathEscape(mediaId))
downloadUrl = fmt.Sprintf("%s/_matrix/federation/unstable/org.matrix.msc3916.v2/media/download/%s", baseUrl, url.PathEscape(mediaId))
resp, err = matrix.FederatedGet(ctx, downloadUrl, realHost, ctx.Config.SigningKeyPath)
metrics.MediaDownloaded.With(prometheus.Labels{"origin": origin}).Inc()
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions test/msc3916_downloads_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func (s *MSC3916DownloadsSuite) TestFederationDownloads() {
assert.NotEmpty(t, mediaId)

// Verify the federation download *fails* when lacking auth
uri := fmt.Sprintf("/_matrix/federation/unstable/org.matrix.msc3916/media/download/%s/%s", origin, mediaId)
uri := fmt.Sprintf("/_matrix/federation/unstable/org.matrix.msc3916.v2/media/download/%s", mediaId)
raw, err := remoteClient.DoRaw("GET", uri, nil, "", nil)
assert.NoError(t, err)
assert.Equal(t, http.StatusUnauthorized, raw.StatusCode)
Expand All @@ -182,7 +182,7 @@ func (s *MSC3916DownloadsSuite) TestFederationMakesAuthedDownloads() {
origin := ""
mediaId := "abc123"
testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, fmt.Sprintf("/_matrix/federation/unstable/org.matrix.msc3916/media/download/%s/%s", origin, mediaId), r.URL.Path)
assert.Equal(t, fmt.Sprintf("/_matrix/federation/unstable/org.matrix.msc3916.v2/media/download/%s", mediaId), r.URL.Path)
origin, err := matrix.ValidateXMatrixAuth(r, true)
assert.NoError(t, err)
assert.Equal(t, client1.ServerName, origin)
Expand Down

0 comments on commit 5b49457

Please sign in to comment.