Skip to content

Commit

Permalink
fix(open-spark): modified logic to add validation for file selection
Browse files Browse the repository at this point in the history
  • Loading branch information
aniketceminds committed Jan 15, 2025
1 parent d7dd991 commit 091613d
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions apps/open-spark/components/dragAndDropUpload/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ interface DragAndDropUploadProps {
}

const DragAndDropUpload = (props: DragAndDropUploadProps) => {
const { fileSelectionElement, multiple, setFiles, dragAndDrop = false, accept } = props
const { fileSelectionElement, multiple, setFiles, dragAndDrop = false, accept = '.json' } = props
const [isDragging, setIsDragging] = useState(false)

const fileInputRef = useRef<HTMLInputElement | null>(null)
Expand Down Expand Up @@ -46,10 +46,22 @@ const DragAndDropUpload = (props: DragAndDropUploadProps) => {
if (e.target.files) {
const uploadedFiles = Array.from(e.target.files)

const validFiles = uploadedFiles.filter(file => file.name.endsWith('.json'))
// Parse accept attribute and dynamically validate
const acceptedTypes = accept?.split(',').map(type => type.trim())
const validFiles = uploadedFiles.filter(file => {
return acceptedTypes?.some(type => {
if (type.startsWith('.')) {
// Match file extension
return file.name.endsWith(type)
} else {
// Match MIME type
return file.type === type
}
})
})

if (validFiles.length !== uploadedFiles.length) {
alert('Only JSON files are allowed. Please upload valid .json files.')
alert(`Invalid files uploaded. Please upload files matching: ${accept}`)
}

if (validFiles.length > 0) {
Expand Down

0 comments on commit 091613d

Please sign in to comment.