diff --git a/api/r0/upload_async.go b/api/r0/upload_async.go index a00063d2..ea899ef1 100644 --- a/api/r0/upload_async.go +++ b/api/r0/upload_async.go @@ -2,7 +2,6 @@ package r0 import ( "errors" - "io" "net/http" "path/filepath" @@ -15,6 +14,7 @@ import ( "github.com/t2bot/matrix-media-repo/common" "github.com/t2bot/matrix-media-repo/common/rcontext" "github.com/t2bot/matrix-media-repo/pipelines/pipeline_upload" + "github.com/t2bot/matrix-media-repo/util" ) var supportedFileTypes = []string{ @@ -59,23 +59,29 @@ func UploadMediaAsync(r *http.Request, rctx rcontext.RequestContext, user _apime } // GK-CUSTOMIZATION: Check if the file type is supported - buf, err := io.ReadAll(r.Body) + rctx.Log.Error("🔊 Attempting to read file: ", filename) + var reqBody interface{} + b, err := util.DecodeWithBuffer(r.Body, &reqBody) if err != nil { + rctx.Log.Error("🔊 Error decoding body: ", err) return &_responses.ErrorResponse{ Code: common.ErrCodeBadRequest, - Message: "Error reading file.", + Message: "Error decoding request.", InternalCode: common.ErrCodeBadRequest, } } - kind, err := filetype.Match(buf) + kind, err := filetype.Match(b.Bytes()) if err != nil { + rctx.Log.Error("🔊 Error matching file type: ", err) return &_responses.ErrorResponse{ Code: common.ErrCodeBadRequest, Message: "Error matching file type.", InternalCode: common.ErrCodeBadRequest, } } + rctx.Log.Error("🔊 File type: ", kind) if !isSupportedFileType(kind.Extension) { + rctx.Log.Error("🔊 unsupported file type") return &_responses.ErrorResponse{ Code: common.ErrCodeBadRequest, Message: "Unsupported file type.", diff --git a/api/r0/upload_sync.go b/api/r0/upload_sync.go index afc5b738..ccc82700 100644 --- a/api/r0/upload_sync.go +++ b/api/r0/upload_sync.go @@ -2,7 +2,6 @@ package r0 import ( "errors" - "io" "net/http" "path/filepath" "strconv" @@ -35,23 +34,29 @@ func UploadMediaSync(r *http.Request, rctx rcontext.RequestContext, user _apimet } // GK-CUSTOMIZATION: Check if the file type is supported - buf, err := io.ReadAll(r.Body) + rctx.Log.Error("🔊 Attempting to read file: ", filename) + var reqBody interface{} + b, err := util.DecodeWithBuffer(r.Body, &reqBody) if err != nil { + rctx.Log.Error("🔊 Error decoding body: ", err) return &_responses.ErrorResponse{ Code: common.ErrCodeBadRequest, - Message: "Error reading file.", + Message: "Error decoding request.", InternalCode: common.ErrCodeBadRequest, } } - kind, err := filetype.Match(buf) + kind, err := filetype.Match(b.Bytes()) if err != nil { + rctx.Log.Error("🔊 Error matching file type: ", err) return &_responses.ErrorResponse{ Code: common.ErrCodeBadRequest, Message: "Error matching file type.", InternalCode: common.ErrCodeBadRequest, } } + rctx.Log.Error("🔊 File type: ", kind) if !isSupportedFileType(kind.Extension) { + rctx.Log.Error("🔊 unsupported file type") return &_responses.ErrorResponse{ Code: common.ErrCodeBadRequest, Message: "Unsupported file type.", diff --git a/util/mxc.go b/util/mxc.go index 83d57337..6746b8fb 100644 --- a/util/mxc.go +++ b/util/mxc.go @@ -1,5 +1,17 @@ package util +import ( + "bytes" + "encoding/json" + "io" +) + func MxcUri(origin string, mediaId string) string { return "mxc://" + origin + "/" + mediaId } + +func DecodeWithBuffer(r io.Reader, dest interface{}) (*bytes.Buffer, error) { + b := bytes.Buffer{} + dec := json.NewDecoder(io.TeeReader(r, &b)) + return &b, dec.Decode(dest) +}