Skip to content

Commit 2331a2c

Browse files
committed
add validation for empty file uploads with appropriate error message
1 parent a279197 commit 2331a2c

File tree

2 files changed

+7
-0
lines changed

2 files changed

+7
-0
lines changed

cmd/media.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,12 @@ func handleMediaUpload(r *fastglue.Request) error {
6666
srcFileSize := fileHeader.Size
6767
srcExt := strings.TrimPrefix(strings.ToLower(filepath.Ext(srcFileName)), ".")
6868

69+
// Check if file is empty
70+
if srcFileSize == 0 {
71+
app.lo.Error("error: uploaded file is empty (0 bytes)")
72+
return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.T("media.fileEmpty"), nil, envelope.InputError)
73+
}
74+
6975
// Check file size
7076
consts := app.consts.Load().(*constants)
7177
if bytesToMegabytes(srcFileSize) > float64(consts.MaxFileUploadSizeMB) {

i18n/en.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,7 @@
332332
"user.errorGeneratingPasswordToken": "Error generating password token",
333333
"media.fileSizeTooLarge": "File size too large, please upload a file less than {size} ",
334334
"media.fileTypeNotAllowed": "File type not allowed",
335+
"media.fileEmpty": "This file is 0 bytes, so it will not be attached.",
335336
"inbox.emptyIMAP": "Empty IMAP config",
336337
"inbox.emptySMTP": "Empty SMTP config",
337338
"template.defaultTemplateAlreadyExists": "Default template already exists",

0 commit comments

Comments
 (0)