From 645205032e565c37e5b7d76efb51af41eac251d0 Mon Sep 17 00:00:00 2001 From: Arthur de Moulins Date: Fri, 7 Jul 2023 11:05:07 +0200 Subject: [PATCH] allow empty file types for validation --- .../src/components/Upload/UploadDropzone.tsx | 51 +++++++++++-------- .../storage-bundle/Upload/FileValidator.php | 18 +++++-- 2 files changed, 43 insertions(+), 26 deletions(-) diff --git a/databox/client/src/components/Upload/UploadDropzone.tsx b/databox/client/src/components/Upload/UploadDropzone.tsx index b1c00fe76..a7667c449 100644 --- a/databox/client/src/components/Upload/UploadDropzone.tsx +++ b/databox/client/src/components/Upload/UploadDropzone.tsx @@ -1,5 +1,5 @@ import React from 'react'; -import Dropzone, {Accept, DropzoneOptions} from "react-dropzone"; +import {Accept, DropzoneOptions, useDropzone} from "react-dropzone"; import {Box, Typography} from "@mui/material"; import {grey} from "@mui/material/colors"; import config from "../../config"; @@ -37,25 +37,32 @@ export default function UploadDropzone({ }: Props) { const accept = useAccept(); - return - {({getRootProps, getInputProps, isDragActive}) => ( - ({ - border: `1px dashed ${grey[500]}`, - borderRadius: theme.shape.borderRadius, - p: 3, - mb: 2, - bgcolor: isDragActive ? 'info.main' : undefined, - cursor: 'pointer', - })} - {...getRootProps()} - > - - Drag 'n' drop some files here, or click to select files - - )} - + const { + getRootProps, + getInputProps, + isDragActive, + } = useDropzone({ + onDrop, + accept, + noClick: true, + }); + + return <> + ({ + display: 'block', + border: `1px dashed ${grey[500]}`, + borderRadius: theme.shape.borderRadius, + p: 3, + mb: 2, + bgcolor: isDragActive ? 'info.main' : undefined, + cursor: 'pointer', + })} + {...getRootProps()} + > + + Drag 'n' drop some files here, or click to select files + + } diff --git a/lib/php/storage-bundle/Upload/FileValidator.php b/lib/php/storage-bundle/Upload/FileValidator.php index 82908bfe6..2adb49df7 100644 --- a/lib/php/storage-bundle/Upload/FileValidator.php +++ b/lib/php/storage-bundle/Upload/FileValidator.php @@ -30,7 +30,9 @@ private function normalizeTypes(array|string $value): array $extensions = []; if (!empty($matches[2][$i])) { - $extensions = array_map('trim', explode(',', substr($matches[2][$i], 1, -1))); + $extensions = array_map(function (string $ext): string { + return preg_replace('#^\.#', '', trim($ext)); + }, explode(',', substr($matches[2][$i], 1, -1))); } $types[$matches[1][$i]] = $extensions; @@ -42,8 +44,17 @@ private function normalizeTypes(array|string $value): array return $value; } + private function getAllowedExtensions(): array + { + return array_merge(...array_values($this->allowedTypes)); + } + public function validateFile(string $path, ?string $type): void { + if (empty($this->allowedTypes)) { + return; + } + $extension = FileUtil::getExtensionFromPath($path); if (null === $type) { $type = FileUtil::getTypeFromExtension($extension); @@ -54,7 +65,7 @@ public function validateFile(string $path, ?string $type): void } if (!$this->hasValidExtension($extension)) { - throw $this->createException($extension, $this->allowedExtensions, 'extension'); + throw $this->createException($extension, $this->getAllowedExtensions(), 'extension'); } } @@ -69,7 +80,7 @@ private function createException(string $value, array $allowed, string $type): B private function hasValidExtension(string $extension): bool { - $allowedExtensions = array_merge(...$this->allowedTypes); + $allowedExtensions = $this->getAllowedExtensions(); if (in_array($extension, $allowedExtensions, true)) { return true; @@ -103,7 +114,6 @@ private function hasValidType(string $type): bool private function matches(string $value, string $mask): bool { - dump('#^'.str_replace('*', '.+', $mask).'$#'); return 1 === preg_match('#^'.str_replace('*', '.+', $mask).'$#', $value); } }