-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathaddFiles.ts
47 lines (43 loc) · 1.37 KB
/
addFiles.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import type { UnknownPlugin } from '@uppy/core'
import type {
CompanionClientProvider,
CompanionClientSearchProvider,
} from '@uppy/utils/lib/CompanionClientProvider'
import type { CompanionFile } from '@uppy/utils/lib/CompanionFile'
import type { Meta, Body, TagFile } from '@uppy/utils/lib/UppyFile'
import { getSafeFileId } from '@uppy/utils/lib/generateFileID'
import getTagFile from './getTagFile.js'
const addFiles = <M extends Meta, B extends Body>(
companionFiles: CompanionFile[],
plugin: UnknownPlugin<M, B>,
provider: CompanionClientProvider | CompanionClientSearchProvider,
): void => {
const tagFiles: TagFile<M>[] = companionFiles.map((f) =>
getTagFile<M, B>(f, plugin, provider),
)
const filesToAdd: TagFile<M>[] = []
const filesAlreadyAdded: TagFile<M>[] = []
tagFiles.forEach((tagFile) => {
if (
plugin.uppy.checkIfFileAlreadyExists(
getSafeFileId(tagFile, plugin.uppy.getID()),
)
) {
filesAlreadyAdded.push(tagFile)
} else {
filesToAdd.push(tagFile)
}
})
if (filesToAdd.length > 0) {
plugin.uppy.info(
plugin.uppy.i18n('addedNumFiles', { numFiles: filesToAdd.length }),
)
}
if (filesAlreadyAdded.length > 0) {
plugin.uppy.info(
`Not adding ${filesAlreadyAdded.length} files because they already exist`,
)
}
plugin.uppy.addFiles(filesToAdd)
}
export default addFiles