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

View File

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