Skip to content

Commit

Permalink
Sanitize the filename input by user just incase they input duplicate …
Browse files Browse the repository at this point in the history
…extensions
  • Loading branch information
suubi-joshua committed Oct 25, 2024
1 parent 0649bed commit 6338126
Showing 1 changed file with 9 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ const FilePreview: React.FC<FilePreviewProps> = ({
clearData,
}) => {
const { t } = useTranslation();
const { allowedExtensions } = useContext(CameraMediaUploaderContext);
const fileExtension = uploadedFile.fileName.match(/\.[^\\/.]+$/)?.[0] || '';
const [fileName, setFileName] = useState(uploadedFile.fileName.replace(/\.[^\\/.]+$/, ''));
const [fileDescription, setFileDescription] = useState(uploadedFile.fileDescription);
Expand All @@ -78,13 +79,19 @@ const FilePreview: React.FC<FilePreviewProps> = ({
const saveImageOrPdf = useCallback(
(event: SyntheticEvent) => {
event.preventDefault();

const sanitizedFileName = allowedExtensions.reduce((name, ext) => {
const regex = new RegExp(`(${ext})+$`, 'i');
return name.replace(regex, '');
}, fileName);

onSaveFile?.({
...uploadedFile,
fileName: `${fileName}${fileExtension}`,
fileName: `${sanitizedFileName}${fileExtension}`,
fileDescription,
});
},
[onSaveFile, fileName, fileExtension, fileDescription, uploadedFile],
[onSaveFile, fileName, fileExtension, allowedExtensions, fileDescription, uploadedFile],
);

const cancelCapture = useCallback(
Expand Down

0 comments on commit 6338126

Please sign in to comment.