You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
38 lines
865 B
38 lines
865 B
|
3 years ago
|
import { File } from '../interfaces'
|
||
|
|
export type RemoveUploadedFileError = { file?: File; field?: string } & Error
|
||
|
|
|
||
|
|
function removeUploadedFiles(
|
||
|
|
uploadedFiles: File[],
|
||
|
|
remove: (file: File, cb: (error?: Error | null) => void) => void,
|
||
|
|
cb: (err: Error | null, storageErrors: RemoveUploadedFileError[]) => void,
|
||
|
|
) {
|
||
|
|
const length = uploadedFiles.length
|
||
|
|
const errors: Error[] = []
|
||
|
|
|
||
|
|
if (length === 0) {
|
||
|
|
return cb(null, errors)
|
||
|
|
}
|
||
|
|
|
||
|
|
function handleFile(idx: number) {
|
||
|
|
const file = uploadedFiles[idx]
|
||
|
|
|
||
|
|
remove(file, function(err?: RemoveUploadedFileError | null) {
|
||
|
|
if (err) {
|
||
|
|
err.file = file
|
||
|
|
err.field = file.fieldname
|
||
|
|
errors.push(err)
|
||
|
|
}
|
||
|
|
|
||
|
|
if (idx < length - 1) {
|
||
|
|
handleFile(idx + 1)
|
||
|
|
} else {
|
||
|
|
cb(null, errors)
|
||
|
|
}
|
||
|
|
})
|
||
|
|
}
|
||
|
|
|
||
|
|
handleFile(0)
|
||
|
|
}
|
||
|
|
|
||
|
|
export default removeUploadedFiles
|