You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: pkg/routes/api/v2/avatar_upload.go
+6-24Lines changed: 6 additions & 24 deletions
Original file line number
Diff line number
Diff line change
@@ -18,23 +18,20 @@ package apiv2
18
18
19
19
import (
20
20
"context"
21
-
"io"
21
+
"errors"
22
22
"net/http"
23
-
"strings"
24
23
25
24
"code.vikunja.io/api/pkg/config"
26
25
"code.vikunja.io/api/pkg/db"
27
26
"code.vikunja.io/api/pkg/models"
28
27
"code.vikunja.io/api/pkg/modules/avatar"
29
-
"code.vikunja.io/api/pkg/modules/avatar/upload"
30
28
"code.vikunja.io/api/pkg/user"
31
29
32
30
"github.com/danielgtaylor/huma/v2"
33
-
"github.com/gabriel-vasile/mimetype"
34
31
)
35
32
36
33
typeavatarUploadInputstruct {
37
-
// Broad allow-list because Huma's MimeTypeValidator rejects the part pre-handler; octet-stream covers programmatic clients. The real gate is mimetype.DetectReader in the handler.
34
+
// Broad allow-list because Huma's MimeTypeValidator rejects the part pre-handler; octet-stream covers programmatic clients. The real gate is the byte-level image check in avatar.StoreUploadedAvatar.
38
35
RawBody huma.MultipartFormFiles[struct {
39
36
Avatar huma.FormFile`form:"avatar" contentType:"image/png,image/jpeg,image/gif,image/webp,image/svg+xml,application/octet-stream" required:"true" doc:"The avatar image to upload. Must be an image; it is resized server-side and re-encoded as PNG."`
40
37
}]
@@ -86,24 +83,11 @@ func avatarUpload(ctx context.Context, in *avatarUploadInput) (*avatarUploadBody
86
83
src:=in.RawBody.Data().Avatar
87
84
deferfunc() { _=src.Close() }()
88
85
89
-
// Byte-level image check, same allow-list as v1's UploadAvatar.
90
-
mime, err:=mimetype.DetectReader(src)
91
-
iferr!=nil {
92
-
_=s.Rollback()
93
-
returnnil, translateDomainError(err)
94
-
}
95
-
if!strings.HasPrefix(mime.String(), "image") {
96
-
_=s.Rollback()
97
-
returnnil, huma.Error400BadRequest("Uploaded file is no image.")
98
-
}
99
-
if_, err:=src.Seek(0, io.SeekStart); err!=nil {
100
-
_=s.Rollback()
101
-
returnnil, translateDomainError(err)
102
-
}
103
-
104
-
u.AvatarProvider="upload"
105
-
iferr:=upload.StoreAvatarFile(s, u, src); err!=nil {
86
+
iferr:=avatar.StoreUploadedAvatar(s, u, src); err!=nil {
106
87
_=s.Rollback()
88
+
iferrors.Is(err, avatar.ErrNotAnImage) {
89
+
returnnil, huma.Error400BadRequest("Uploaded file is no image.")
90
+
}
107
91
returnnil, translateDomainError(err)
108
92
}
109
93
@@ -112,7 +96,5 @@ func avatarUpload(ctx context.Context, in *avatarUploadInput) (*avatarUploadBody
112
96
returnnil, translateDomainError(err)
113
97
}
114
98
115
-
avatar.FlushAllCaches(u)
116
-
117
99
return&avatarUploadBody{Body: &models.Message{Message: "Avatar was uploaded successfully."}}, nil
0 commit comments