Skip to content

Commit

Permalink
add error message for filename with non-ascii chars
Browse files Browse the repository at this point in the history
  • Loading branch information
Bowrna committed Aug 5, 2024
1 parent 01f7450 commit cacb01f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
4 changes: 4 additions & 0 deletions cmd/media.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ func handleUploadMedia(c echo.Context) error {
ext = strings.TrimPrefix(strings.ToLower(filepath.Ext(file.Filename)), ".")
contentType = file.Header.Get("Content-Type")
)
if !isASCII(file.Filename) {
return echo.NewHTTPError(http.StatusUnprocessableEntity,
app.i18n.Ts("media.invalidFileName", "name", file.Filename))
}

// Validate file extension.
if !inArray("*", app.constants.MediaUpload.Extensions) {
Expand Down
10 changes: 10 additions & 0 deletions cmd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"regexp"
"strconv"
"strings"
"unicode"
)

var (
Expand Down Expand Up @@ -113,3 +114,12 @@ func titleCase(input string) string {

return strings.Join(parts, " ")
}

func isASCII(s string) bool {
for _, c := range s {
if c > unicode.MaxASCII {
return false
}
}
return true
}
1 change: 1 addition & 0 deletions i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@
"media.errorSavingThumbnail": "Error saving thumbnail: {error}",
"media.errorUploading": "Error uploading file: {error}",
"media.invalidFile": "Invalid file: {error}",
"media.invalidFileName": "Invalid filename {name}. Use only ASCII characters",
"media.title": "Media",
"media.unsupportedFileType": "Unsupported file type ({type})",
"media.upload": "Upload",
Expand Down

0 comments on commit cacb01f

Please sign in to comment.