1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | module.exports = function generateFileID (file) {
|
9 |
|
10 |
|
11 |
|
12 | let id = 'uppy'
|
13 | if (typeof file.name === 'string') {
|
14 | id += '-' + encodeFilename(file.name.toLowerCase())
|
15 | }
|
16 |
|
17 | if (file.type !== undefined) {
|
18 | id += '-' + file.type
|
19 | }
|
20 |
|
21 | if (file.meta && typeof file.meta.relativePath === 'string') {
|
22 | id += '-' + encodeFilename(file.meta.relativePath.toLowerCase())
|
23 | }
|
24 |
|
25 | if (file.data.size !== undefined) {
|
26 | id += '-' + file.data.size
|
27 | }
|
28 | if (file.data.lastModified !== undefined) {
|
29 | id += '-' + file.data.lastModified
|
30 | }
|
31 |
|
32 | return id
|
33 | }
|
34 |
|
35 | function encodeFilename (name) {
|
36 | let suffix = ''
|
37 | return name.replace(/[^A-Z0-9]/ig, (character) => {
|
38 | suffix += '-' + encodeCharacter(character)
|
39 | return '/'
|
40 | }) + suffix
|
41 | }
|
42 |
|
43 | function encodeCharacter (character) {
|
44 | return character.charCodeAt(0).toString(32)
|
45 | }
|