Add file type restriction to image upload input using accept attribute (#872)

* Add file type restriction to image upload input using accept attribute

Signed-off-by: eternal-flame-AD <yume@yumechi.jp>

---------

Signed-off-by: eternal-flame-AD <yume@yumechi.jp>
Co-authored-by: eternal-flame-AD <yume@yumechi.jp>
This commit is contained in:
Laurence Jones 2025-11-05 21:03:06 +00:00 committed by GitHub
parent f263221133
commit 71e38b9824
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 6 additions and 6 deletions

View File

@ -6,6 +6,7 @@ import (
"net/http"
"os"
"path/filepath"
"strings"
"github.com/gin-gonic/gin"
"github.com/gotify/server/v2/auth"
@ -459,7 +460,7 @@ func generateNonExistingImageName(imgDir string, gen func() string) string {
}
func ValidApplicationImageExt(ext string) bool {
switch ext {
switch strings.ToLower(ext) {
case ".gif", ".png", ".jpg", ".jpeg":
return true
default:

View File

@ -59,6 +59,8 @@ const Applications = observer(() => {
useEffect(() => void appStore.refresh(), []);
const validExtensions = ['.gif', '.png', '.jpg', '.jpeg'];
const handleImageUploadClick = (id: number) => {
uploadId.current = id;
if (fileInputRef.current) {
@ -71,11 +73,7 @@ const Applications = observer(() => {
if (!file) {
return;
}
if (['image/png', 'image/jpeg', 'image/gif'].indexOf(file.type) !== -1) {
appStore.uploadImage(uploadId.current, file);
} else {
alert('Uploaded file must be of type png, jpeg or gif.');
}
appStore.uploadImage(uploadId.current, file);
};
return (
@ -128,6 +126,7 @@ const Applications = observer(() => {
<input
ref={fileInputRef}
type="file"
accept={validExtensions.join(',')}
style={{display: 'none'}}
onChange={onUploadImage}
/>