1
0
mirror of https://github.com/Foltik/Shimapan synced 2024-11-15 09:08:06 -05:00
shimapan/app/util/upload/id.js

23 lines
576 B
JavaScript
Raw Normal View History

const config = require('config');
const ModelPath = '../../models/';
const Upload = require(ModelPath + 'Upload.js');
const exists = async id =>
await Upload.countDocuments({id: id}) === 1;
const generate = async () => {
const charset = config.get('Upload.charset');
const len = config.get('Upload.idLength');
const id = [...Array(len)]
.map(() => charset.charAt(Math.floor(Math.random() * charset.length)))
.join('');
return await exists(id)
? generate()
: id;
};
exports.generate = generate;
exports.exists = exists;