diff --git a/CHANGELOG.md b/CHANGELOG.md index ec5e073b..f20db813 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), ### Added * New datastore option to ignore Redis cache when downloading media served by a `publicBaseUrl`. This can help ensure more requests get redirected to the CDN. +* `HEAD /download` is now supported, as per [MSC4120](https://github.com/matrix-org/matrix-spec-proposals/pull/4120). ### Fixed diff --git a/api/r0/download.go b/api/r0/download.go index ab13d4a8..f9c6cf7a 100644 --- a/api/r0/download.go +++ b/api/r0/download.go @@ -53,6 +53,13 @@ func DownloadMedia(r *http.Request, rctx rcontext.RequestContext, user _apimeta. return _responses.BadRequest("timeout_ms does not appear to be an integer") } + recordOnly := false + if r.Method == http.MethodHead { + rctx.Log.Debug("HEAD request received - changing parameters") + //downloadRemote = false // we allow the download to go through to ensure proper metadata is returned + recordOnly = true + } + rctx = rctx.LogWithFields(logrus.Fields{ "mediaId": mediaId, "server": server, @@ -70,6 +77,7 @@ func DownloadMedia(r *http.Request, rctx rcontext.RequestContext, user _apimeta. FetchRemoteIfNeeded: downloadRemote, BlockForReadUntil: blockFor, CanRedirect: canRedirect, + RecordOnly: recordOnly, }) if err != nil { var redirect datastores.RedirectError diff --git a/api/routes.go b/api/routes.go index fe0067b4..8c6353fd 100644 --- a/api/routes.go +++ b/api/routes.go @@ -33,8 +33,8 @@ func buildRoutes() http.Handler { register([]string{"PUT"}, PrefixMedia, "upload/:server/:mediaId", mxV3, router, makeRoute(_routers.RequireAccessToken(r0.UploadMediaAsync), "upload_async", counter)) register([]string{"POST"}, PrefixMedia, "upload", mxSpecV3Transition, router, makeRoute(_routers.RequireAccessToken(r0.UploadMediaSync), "upload", counter)) downloadRoute := makeRoute(_routers.OptionalAccessToken(r0.DownloadMedia), "download", counter) - register([]string{"GET"}, PrefixMedia, "download/:server/:mediaId/:filename", mxSpecV3Transition, router, downloadRoute) - register([]string{"GET"}, PrefixMedia, "download/:server/:mediaId", mxSpecV3Transition, router, downloadRoute) + register([]string{"GET", "HEAD"}, PrefixMedia, "download/:server/:mediaId/:filename", mxSpecV3Transition, router, downloadRoute) + register([]string{"GET", "HEAD"}, PrefixMedia, "download/:server/:mediaId", mxSpecV3Transition, router, downloadRoute) register([]string{"GET"}, PrefixMedia, "thumbnail/:server/:mediaId", mxSpecV3Transition, router, makeRoute(_routers.OptionalAccessToken(r0.ThumbnailMedia), "thumbnail", counter)) register([]string{"GET"}, PrefixMedia, "preview_url", mxSpecV3TransitionCS, router, makeRoute(_routers.RequireAccessToken(r0.PreviewUrl), "url_preview", counter)) register([]string{"GET"}, PrefixMedia, "identicon/*seed", mxR0, router, makeRoute(_routers.OptionalAccessToken(r0.Identicon), "identicon", counter)) diff --git a/go.mod b/go.mod index bd244af0..7890ee89 100644 --- a/go.mod +++ b/go.mod @@ -38,7 +38,7 @@ require ( github.com/sirupsen/logrus v1.9.3 golang.org/x/crypto v0.21.0 // indirect golang.org/x/image v0.15.0 - golang.org/x/net v0.22.0 + golang.org/x/net v0.23.0 ) require ( diff --git a/go.sum b/go.sum index 26142f63..7f8bae2b 100644 --- a/go.sum +++ b/go.sum @@ -451,8 +451,8 @@ golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= golang.org/x/net v0.9.0/go.mod h1:d48xBJpPfHeWQsugry2m+kC02ZBRGRgulfHnEXEuWns= golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg= golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE= -golang.org/x/net v0.22.0 h1:9sGLhx7iRIHEiX0oAJ3MRZMUCElJgy7Br1nO+AMN3Tc= -golang.org/x/net v0.22.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg= +golang.org/x/net v0.23.0 h1:7EYJ93RZ9vYSZAIb2x3lnuvqO5zneoD6IvWjuhfxjTs= +golang.org/x/net v0.23.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=