Skip to content

Commit

Permalink
🐛 Attempted fix.
Browse files Browse the repository at this point in the history
  • Loading branch information
Danieloni1 committed May 23, 2024
1 parent 3e42165 commit 633214b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
14 changes: 10 additions & 4 deletions api/r0/upload_async.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package r0

import (
"errors"
"io"
"net/http"
"path/filepath"

Expand All @@ -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{
Expand Down Expand Up @@ -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.",
Expand Down
13 changes: 9 additions & 4 deletions api/r0/upload_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package r0

import (
"errors"
"io"
"net/http"
"path/filepath"
"strconv"
Expand Down Expand Up @@ -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.",
Expand Down
12 changes: 12 additions & 0 deletions util/mxc.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 633214b

Please sign in to comment.