UNPKG

1.17 kBJavaScriptView Raw
1/**
2 * Takes a file object and turns it into fileID, by converting file.name to lowercase,
3 * removing extra characters and adding type, size and lastModified
4 *
5 * @param {object} file
6 * @returns {string} the fileID
7 */
8module.exports = function generateFileID (file) {
9 // It's tempting to do `[items].filter(Boolean).join('-')` here, but that
10 // is slower! simple string concatenation is fast
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
35function encodeFilename (name) {
36 let suffix = ''
37 return name.replace(/[^A-Z0-9]/ig, (character) => {
38 suffix += '-' + encodeCharacter(character)
39 return '/'
40 }) + suffix
41}
42
43function encodeCharacter (character) {
44 return character.charCodeAt(0).toString(32)
45}