diff --git a/mediaapi/routing/routing.go b/mediaapi/routing/routing.go index 71dad19b6..d5348e5d4 100644 --- a/mediaapi/routing/routing.go +++ b/mediaapi/routing/routing.go @@ -61,8 +61,8 @@ func Setup( // TODO: Add AS support r0mux.Handle("/upload", common.MakeAuthAPI( "upload", authData, - func(req *http.Request, _ *authtypes.Device) util.JSONResponse { - return Upload(req, cfg, db, activeThumbnailGeneration) + func(req *http.Request, dev *authtypes.Device) util.JSONResponse { + return Upload(req, cfg, dev, db, activeThumbnailGeneration, ) }, )).Methods(http.MethodPost, http.MethodOptions) diff --git a/mediaapi/routing/upload.go b/mediaapi/routing/upload.go index 62d70bf75..1da551646 100644 --- a/mediaapi/routing/upload.go +++ b/mediaapi/routing/upload.go @@ -23,6 +23,8 @@ import ( "path" "strings" + "github.com/matrix-org/dendrite/clientapi/auth/authtypes" + "github.com/matrix-org/dendrite/clientapi/jsonerror" "github.com/matrix-org/dendrite/common/config" "github.com/matrix-org/dendrite/mediaapi/fileutils" @@ -53,8 +55,8 @@ type uploadResponse struct { // This implementation supports a configurable maximum file size limit in bytes. If a user tries to upload more than this, they will receive an error that their upload is too large. // Uploaded files are processed piece-wise to avoid DoS attacks which would starve the server of memory. // TODO: We should time out requests if they have not received any data within a configured timeout period. -func Upload(req *http.Request, cfg *config.Dendrite, db storage.Database, activeThumbnailGeneration *types.ActiveThumbnailGeneration) util.JSONResponse { - r, resErr := parseAndValidateRequest(req, cfg) +func Upload(req *http.Request, cfg *config.Dendrite, dev *authtypes.Device, db storage.Database, activeThumbnailGeneration *types.ActiveThumbnailGeneration) util.JSONResponse { + r, resErr := parseAndValidateRequest(req, cfg, dev) if resErr != nil { return *resErr } @@ -74,13 +76,14 @@ func Upload(req *http.Request, cfg *config.Dendrite, db storage.Database, active // parseAndValidateRequest parses the incoming upload request to validate and extract // all the metadata about the media being uploaded. // Returns either an uploadRequest or an error formatted as a util.JSONResponse -func parseAndValidateRequest(req *http.Request, cfg *config.Dendrite) (*uploadRequest, *util.JSONResponse) { +func parseAndValidateRequest(req *http.Request, cfg *config.Dendrite, device *authtypes.Device) (*uploadRequest, *util.JSONResponse) { r := &uploadRequest{ MediaMetadata: &types.MediaMetadata{ Origin: cfg.Matrix.ServerName, FileSizeBytes: types.FileSizeBytes(req.ContentLength), ContentType: types.ContentType(req.Header.Get("Content-Type")), UploadName: types.Filename(url.PathEscape(req.FormValue("filename"))), + UserID: types.MatrixUserID(device.UserID), }, Logger: util.GetLogger(req.Context()).WithField("Origin", cfg.Matrix.ServerName), }