mirror of
https://github.com/transloadit/uppy.git
synced 2026-07-24 10:47:44 +00:00
* core: add test for ID generation with non-latin names * core: adjust ID generation to keep non-latin characters This shouldn't be bc-breaking! Now, non-latin characters are encoded as their charcode in base 32, so files that only differ by name in a non-latin language will generate different IDs.
30 lines
785 B
JavaScript
30 lines
785 B
JavaScript
/**
|
|
* Takes a file object and turns it into fileID, by converting file.name to lowercase,
|
|
* removing extra characters and adding type, size and lastModified
|
|
*
|
|
* @param {Object} file
|
|
* @returns {string} the fileID
|
|
*
|
|
*/
|
|
module.exports = function generateFileID (file) {
|
|
// filter is needed to not join empty values with `-`
|
|
return [
|
|
'uppy',
|
|
file.name ? encodeFilename(file.name.toLowerCase()) : '',
|
|
file.type,
|
|
file.data.size,
|
|
file.data.lastModified
|
|
].filter(val => val).join('-')
|
|
}
|
|
|
|
function encodeFilename (name) {
|
|
let suffix = ''
|
|
return name.replace(/[^A-Z0-9]/ig, (character) => {
|
|
suffix += '-' + encodeCharacter(character)
|
|
return '/'
|
|
}) + suffix
|
|
}
|
|
|
|
function encodeCharacter (character) {
|
|
return character.charCodeAt(0).toString(32)
|
|
}
|