-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.js
33 lines (29 loc) · 909 Bytes
/
common.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
const path = require('path');
const fs = require('fs');
exports.fileExists = function(path) {
try {
if (fs.existsSync(path)) {
return true;
}
} catch(err) {
return false;
}
}
exports.generateToken = function(length) {
let result = '';
let validChars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
for ( var i = 0; i < length; i++ ) {
result += validChars.charAt(Math.floor(Math.random() * validChars.length));
}
return result;
}
exports.generateUniqueFilename = function(parentPath, extension) {
let name = this.generateToken(48);
if(this.fileExists(path.join(parentPath, name, extension))) {
return this.generateUniqueFilename(parentPath, extension);
}
return name + extension;
}
exports.getFileExtension = function(filename) {
return filename.split('.').pop();
}