Skip to content

Commit

Permalink
allow empty file types for validation
Browse files Browse the repository at this point in the history
  • Loading branch information
4rthem committed Jul 7, 2023
1 parent 402a206 commit 6452050
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 26 deletions.
51 changes: 29 additions & 22 deletions databox/client/src/components/Upload/UploadDropzone.tsx
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -37,25 +37,32 @@ export default function UploadDropzone({
}: Props) {
const accept = useAccept();

return <Dropzone
onDrop={onDrop}
accept={accept}
>
{({getRootProps, getInputProps, isDragActive}) => (
<Box
sx={theme => ({
border: `1px dashed ${grey[500]}`,
borderRadius: theme.shape.borderRadius,
p: 3,
mb: 2,
bgcolor: isDragActive ? 'info.main' : undefined,
cursor: 'pointer',
})}
{...getRootProps()}
>
<input {...getInputProps()} />
<Typography>Drag 'n' drop some files here, or click to select files</Typography>
</Box>
)}
</Dropzone>
const {
getRootProps,
getInputProps,
isDragActive,
} = useDropzone({
onDrop,
accept,
noClick: true,
});

return <>
<Box
component={'label'}
sx={theme => ({
display: 'block',
border: `1px dashed ${grey[500]}`,
borderRadius: theme.shape.borderRadius,
p: 3,
mb: 2,
bgcolor: isDragActive ? 'info.main' : undefined,
cursor: 'pointer',
})}
{...getRootProps()}
>
<input {...getInputProps()} />
<Typography>Drag 'n' drop some files here, or click to select files</Typography>
</Box>
</>
}
18 changes: 14 additions & 4 deletions lib/php/storage-bundle/Upload/FileValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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');
}
}

Expand All @@ -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;
Expand Down Expand Up @@ -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);
}
}

0 comments on commit 6452050

Please sign in to comment.