-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
utils.js
28 lines (26 loc) · 878 Bytes
/
utils.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
const config = require('./config/config'),
mime = require('mime-types')
const utils = {
imgUrl: function (id) {
return `${config.publicImageUrl}/${id}`
},
shortlinkUrl: function (id) {
return `${config.publicShortlinkUrl}/${id}`
},
generateUUID: function (maxLen) {
const len = maxLen || 5
const chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
let out = ''
for (let i = 0, clen = chars.length; i < len; i++) {
out += chars.substr(0 | Math.random() * clen, 1)
}
return out
},
isURL: function (url) {
return !!url.match(/https?:\/\/(www\.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_+.~#?&//=]*)/)
},
guessMimeType(extOrFilename) {
return mime.lookup(extOrFilename)
}
}
module.exports = utils