-
Notifications
You must be signed in to change notification settings - Fork 13
feat(website): Mirror backend filename validation in website #7218
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
base: filename-validation
Are you sure you want to change the base?
Changes from all commits
907bd2b
0eab34e
e77de8f
fa3d926
3fe47b9
346f669
2a883b1
d1018c5
0171b59
9db037c
45ceca5
974236c
2d55d0d
cd5e8cb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,18 +2,19 @@ import { produce } from 'immer'; | |
| import React, { useEffect, useState, type Dispatch, type FC, type SetStateAction } from 'react'; | ||
| import { toast } from 'react-toastify'; | ||
|
|
||
| import type { | ||
| Awaiting, | ||
| FileUploadState, | ||
| Pending, | ||
| PreviousUpload, | ||
| SingleFileUpload, | ||
| Uploaded, | ||
| UploadStatus, | ||
| import { validateFileNames, getFileNameErrorMessage } from './fileNameValidation'; | ||
| import { | ||
| type Awaiting, | ||
| type FileUploadState, | ||
| type Pending, | ||
| type PreviousUpload, | ||
| type SingleFileUpload, | ||
| type Uploaded, | ||
| type UploadStatus, | ||
| } from './fileUpload'; | ||
| import useClientFlag from '../../../hooks/isClient'; | ||
| import { BackendClient } from '../../../services/backendClient'; | ||
| import { type FileCategory } from '../../../types/config'; | ||
| import { type FileCategory, type FileSharingConfig } from '../../../types/config'; | ||
| import type { ClientConfig } from '../../../types/runtimeConfig'; | ||
| import { calculatePartSizeAndCount, splitFileIntoParts, uploadPart } from '../../../utils/multipartUpload'; | ||
| import { displayConfirmationDialog } from '../../ConfirmationDialog'; | ||
|
|
@@ -32,6 +33,7 @@ type FolderUploadComponentProps = { | |
| fileUploadState: FileUploadState | undefined; | ||
| setFileUploadState: Dispatch<SetStateAction<FileUploadState | undefined>>; | ||
| onError: (message: string) => void; | ||
| fileSharingConfig: FileSharingConfig; | ||
| }; | ||
|
|
||
| const FileInput = ({ | ||
|
|
@@ -89,6 +91,7 @@ export const FolderUploadComponent: FC<FolderUploadComponentProps> = ({ | |
| fileUploadState, | ||
| setFileUploadState, | ||
| onError, | ||
| fileSharingConfig, | ||
| }) => { | ||
| const [isDragging, setIsDragging] = useState(false); | ||
|
|
||
|
|
@@ -227,7 +230,7 @@ export const FolderUploadComponent: FC<FolderUploadComponentProps> = ({ | |
| // Reset the input so the same folder can be selected again | ||
| e.target.value = ''; | ||
|
|
||
| const error = isFilesArrayValid(filesArray, inputMode); | ||
| const error = isFilesArrayValid(filesArray, inputMode, fileSharingConfig); | ||
| if (error) { | ||
| onError(error); | ||
| return; | ||
|
|
@@ -269,7 +272,7 @@ export const FolderUploadComponent: FC<FolderUploadComponentProps> = ({ | |
| // Reset the input so the same file can be selected again | ||
| e.target.value = ''; | ||
|
|
||
| const error = isFilesArrayValid(filesArray, inputMode); | ||
| const error = isFilesArrayValid(filesArray, inputMode, fileSharingConfig); | ||
| if (error) { | ||
| onError(error); | ||
| return; | ||
|
|
@@ -524,15 +527,23 @@ const filterDotFiles = (files: File[]): File[] => { | |
| /** | ||
| * Returns `undefined` if the files are fine, or an error otherwise. | ||
| */ | ||
| const isFilesArrayValid = (files: File[], inputMode: InputMode): string | undefined => { | ||
| const isFilesArrayValid = ( | ||
| files: File[], | ||
| inputMode: InputMode, | ||
| fileSharingConfig: FileSharingConfig, | ||
| ): string | undefined => { | ||
| if (inputMode === 'form') { | ||
| if (files.some((f) => f.webkitRelativePath.split('/').length > 2)) { | ||
| return 'Subdirectories are not supported for individual submissions.'; | ||
| } | ||
| } | ||
| const fileNames = files.map((f) => f.name); | ||
| const folderNames = files.flatMap((f) => f.webkitRelativePath.split('/').slice(1, -1)); | ||
| const fileNames = files.map((f) => f.name); | ||
|
|
||
| if (fileNames.some((n) => /\s/.test(n))) return 'File names cannot contain whitespace.'; | ||
| if (folderNames.some((p) => /\s/.test(p))) return 'Folder names cannot contain whitespace.'; | ||
|
|
||
| const fileNameValidationResult = validateFileNames(fileNames, fileSharingConfig); | ||
| if (fileNameValidationResult.isErr()) { | ||
| return 'Encountered errors in uploaded files: ' + getFileNameErrorMessage(fileNameValidationResult.error); | ||
| } | ||
|
Comment on lines
540
to
+548
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Two things worth confirming are intentional:
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The situation described here although valid, is basically never going to happen and it'd be more confusing if the folder component sometimes validated and sometimes didn't
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agree we should not check the folder names, I think it would also be consistent to only check the defined fileNames that are sent to the backend and not the "true" fileNames - but I also think this is an uncommon situation and is more complicated as this comes from parsing the metadata file and not the actual file names - so we could also do that in a later PR
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. or actually, could we just remove the validation here any only do the validation on the file mapping? that seems more correct, less code and potentially less passing down of the FileSharingConfig config?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah could do it just on the file mapping! the only thing you'd lose is the rejecting of files with invalid names before they can even be uploaded. But I guess that's not really a huge benefit and only makes total sense for the form upload anyway? |
||
| }; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice!