Skip to content

Commit 633214b

Browse files
committed
🐛 Attempted fix.
1 parent 3e42165 commit 633214b

File tree

3 files changed

+31
-8
lines changed

3 files changed

+31
-8
lines changed

api/r0/upload_async.go

+10-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package r0
22

33
import (
44
"errors"
5-
"io"
65
"net/http"
76
"path/filepath"
87

@@ -15,6 +14,7 @@ import (
1514
"github.com/t2bot/matrix-media-repo/common"
1615
"github.com/t2bot/matrix-media-repo/common/rcontext"
1716
"github.com/t2bot/matrix-media-repo/pipelines/pipeline_upload"
17+
"github.com/t2bot/matrix-media-repo/util"
1818
)
1919

2020
var supportedFileTypes = []string{
@@ -59,23 +59,29 @@ func UploadMediaAsync(r *http.Request, rctx rcontext.RequestContext, user _apime
5959
}
6060

6161
// GK-CUSTOMIZATION: Check if the file type is supported
62-
buf, err := io.ReadAll(r.Body)
62+
rctx.Log.Error("🔊 Attempting to read file: ", filename)
63+
var reqBody interface{}
64+
b, err := util.DecodeWithBuffer(r.Body, &reqBody)
6365
if err != nil {
66+
rctx.Log.Error("🔊 Error decoding body: ", err)
6467
return &_responses.ErrorResponse{
6568
Code: common.ErrCodeBadRequest,
66-
Message: "Error reading file.",
69+
Message: "Error decoding request.",
6770
InternalCode: common.ErrCodeBadRequest,
6871
}
6972
}
70-
kind, err := filetype.Match(buf)
73+
kind, err := filetype.Match(b.Bytes())
7174
if err != nil {
75+
rctx.Log.Error("🔊 Error matching file type: ", err)
7276
return &_responses.ErrorResponse{
7377
Code: common.ErrCodeBadRequest,
7478
Message: "Error matching file type.",
7579
InternalCode: common.ErrCodeBadRequest,
7680
}
7781
}
82+
rctx.Log.Error("🔊 File type: ", kind)
7883
if !isSupportedFileType(kind.Extension) {
84+
rctx.Log.Error("🔊 unsupported file type")
7985
return &_responses.ErrorResponse{
8086
Code: common.ErrCodeBadRequest,
8187
Message: "Unsupported file type.",

api/r0/upload_sync.go

+9-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package r0
22

33
import (
44
"errors"
5-
"io"
65
"net/http"
76
"path/filepath"
87
"strconv"
@@ -35,23 +34,29 @@ func UploadMediaSync(r *http.Request, rctx rcontext.RequestContext, user _apimet
3534
}
3635

3736
// GK-CUSTOMIZATION: Check if the file type is supported
38-
buf, err := io.ReadAll(r.Body)
37+
rctx.Log.Error("🔊 Attempting to read file: ", filename)
38+
var reqBody interface{}
39+
b, err := util.DecodeWithBuffer(r.Body, &reqBody)
3940
if err != nil {
41+
rctx.Log.Error("🔊 Error decoding body: ", err)
4042
return &_responses.ErrorResponse{
4143
Code: common.ErrCodeBadRequest,
42-
Message: "Error reading file.",
44+
Message: "Error decoding request.",
4345
InternalCode: common.ErrCodeBadRequest,
4446
}
4547
}
46-
kind, err := filetype.Match(buf)
48+
kind, err := filetype.Match(b.Bytes())
4749
if err != nil {
50+
rctx.Log.Error("🔊 Error matching file type: ", err)
4851
return &_responses.ErrorResponse{
4952
Code: common.ErrCodeBadRequest,
5053
Message: "Error matching file type.",
5154
InternalCode: common.ErrCodeBadRequest,
5255
}
5356
}
57+
rctx.Log.Error("🔊 File type: ", kind)
5458
if !isSupportedFileType(kind.Extension) {
59+
rctx.Log.Error("🔊 unsupported file type")
5560
return &_responses.ErrorResponse{
5661
Code: common.ErrCodeBadRequest,
5762
Message: "Unsupported file type.",

util/mxc.go

+12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
package util
22

3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"io"
7+
)
8+
39
func MxcUri(origin string, mediaId string) string {
410
return "mxc://" + origin + "/" + mediaId
511
}
12+
13+
func DecodeWithBuffer(r io.Reader, dest interface{}) (*bytes.Buffer, error) {
14+
b := bytes.Buffer{}
15+
dec := json.NewDecoder(io.TeeReader(r, &b))
16+
return &b, dec.Decode(dest)
17+
}

0 commit comments

Comments
 (0)