2015-07-09 23:01:43 +01:00
|
|
|
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
|
|
|
|
|
|
|
|
function randomID() {
|
|
|
|
let rand = '';
|
|
|
|
for (let i = 0; i < 12; i++) {
|
|
|
|
rand += chars.charAt(Math.floor((Math.random() * chars.length)));
|
|
|
|
}
|
|
|
|
return rand;
|
|
|
|
}
|
|
|
|
|
|
|
|
function* checkId(Files, fileId, attempts) {
|
|
|
|
if (attempts > 10) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-06-06 13:39:42 +01:00
|
|
|
const file = yield Files.findOne({ _id: fileId });
|
2015-08-23 22:12:32 +01:00
|
|
|
if (file === null) {
|
2015-07-09 23:01:43 +01:00
|
|
|
return fileId;
|
|
|
|
}
|
2015-08-23 22:12:32 +01:00
|
|
|
return checkId(randomID(), ++attempts); // eslint-disable-line no-param-reassign
|
2015-07-09 23:01:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export default function* (Files) {
|
2015-08-23 22:12:32 +01:00
|
|
|
return yield checkId(Files, randomID(), 0);
|
2015-07-09 23:01:43 +01:00
|
|
|
}
|