-
Notifications
You must be signed in to change notification settings - Fork 2k
/
generateFileID.ts
82 lines (70 loc) · 2.22 KB
/
generateFileID.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import type { MinimalRequiredUppyFile } from './UppyFile'
import getFileType from './getFileType.ts'
function encodeCharacter(character: string): string {
return character.charCodeAt(0).toString(32)
}
function encodeFilename(name: string): string {
let suffix = ''
return (
name.replace(/[^A-Z0-9]/gi, (character) => {
suffix += `-${encodeCharacter(character)}`
return '/'
}) + suffix
)
}
/**
* Takes a file object and turns it into fileID, by converting file.name to lowercase,
* removing extra characters and adding type, size and lastModified
*/
export default function generateFileID(
file: MinimalRequiredUppyFile<any, any>,
instanceId: string,
): string {
// It's tempting to do `[items].filter(Boolean).join('-')` here, but that
// is slower! simple string concatenation is fast
let id = instanceId || 'uppy'
if (typeof file.name === 'string') {
id += `-${encodeFilename(file.name.toLowerCase())}`
}
if (file.type !== undefined) {
id += `-${file.type}`
}
if (file.meta && typeof file.meta.relativePath === 'string') {
id += `-${encodeFilename(file.meta.relativePath.toLowerCase())}`
}
if (file.data.size !== undefined) {
id += `-${file.data.size}`
}
if ((file.data as File).lastModified !== undefined) {
id += `-${(file.data as File).lastModified}`
}
return id
}
// If the provider has a stable, unique ID, then we can use that to identify the file.
// Then we don't have to generate our own ID, and we can add the same file many times if needed (different path)
function hasFileStableId(file: MinimalRequiredUppyFile<any, any>): boolean {
if (!file.isRemote || !file.remote) return false
// These are the providers that it seems like have stable IDs for their files. The other's I haven't checked yet.
const stableIdProviders = new Set([
'box',
'dropbox',
'drive',
'facebook',
'unsplash',
])
return stableIdProviders.has(file.remote.provider as any)
}
export function getSafeFileId(
file: MinimalRequiredUppyFile<any, any>,
instanceId: string,
): string {
if (hasFileStableId(file)) return file.id!
const fileType = getFileType(file)
return generateFileID(
{
...file,
type: fileType,
},
instanceId,
)
}