Initial commit.
This commit is contained in:
commit
b48a4e92e1
169 changed files with 7538 additions and 0 deletions
26
lib/hostr-id.js
Normal file
26
lib/hostr-id.js
Normal file
|
@ -0,0 +1,26 @@
|
|||
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;
|
||||
}
|
||||
const file = yield Files.findOne({'_id': fileId});
|
||||
if(file === null) {
|
||||
return fileId;
|
||||
} else {
|
||||
return checkId(randomID(), attempts++);
|
||||
}
|
||||
}
|
||||
|
||||
export default function* (Files) {
|
||||
let attempts = 0;
|
||||
return yield checkId(Files, randomID(), attempts);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue