Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix file extension validation #705

34 changes: 23 additions & 11 deletions src/lib/elements/forms/inputFile.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,6 @@
function setFiles(value: FileList) {
if (!value) return;

const hasInvalidExt = Array.from(value).some((file) => {
const fileExtension = file.name.split('.').pop();
return allowedFileExtensions?.length
? !allowedFileExtensions.includes(fileExtension)
: false;
});
if (hasInvalidExt) {
error = 'Invalid file extension';
return;
}

files = value;
input.files = value;
}
Expand All @@ -42,11 +31,22 @@
setFiles(new DataTransfer().files);
}

function isFileExtensionAllowed(fileExtension: string) {
if (allowedFileExtensions.length && !allowedFileExtensions.includes(fileExtension)) {
return false;
}
return true;
}
function dropHandler(ev: DragEvent) {
ev.dataTransfer.dropEffect = 'move';
hovering = false;
if (!ev.dataTransfer.items) return;
for (let i = 0; i < ev.dataTransfer.items.length; i++) {
const fileExtension = ev.dataTransfer.items[i].getAsFile().name.split('.')[1];
if (!isFileExtensionAllowed(fileExtension)) {
error = 'Invalid file extension';
return;
}
if (ev.dataTransfer.items[i].kind === 'file') {
const dataTransfer = new DataTransfer();
dataTransfer.items.add(ev.dataTransfer.items[i].getAsFile());
Expand Down Expand Up @@ -81,6 +81,18 @@

const handleChange = (event: Event) => {
const target = event.currentTarget as HTMLInputElement;

const isValidFiles = Array.from(target.files).every((file) => {
const fileExtension = file.name.split('.').pop();
return isFileExtensionAllowed(fileExtension);
});

if (!isValidFiles) {
error = 'Invalid file extension';
target.value = '';
return;
}

setFiles(target.files);
};
</script>
Expand Down
Loading