2017-03-14 05:02:43 +00:00
|
|
|
'use strict'
|
2017-01-06 04:37:40 +00:00
|
|
|
// external modules
|
2018-12-22 13:19:02 +00:00
|
|
|
const crypto = require('crypto')
|
2018-04-12 11:14:42 +00:00
|
|
|
const randomcolor = require('randomcolor')
|
|
|
|
const config = require('./config')
|
2017-01-06 04:37:40 +00:00
|
|
|
|
|
|
|
// core
|
2018-04-12 11:14:42 +00:00
|
|
|
exports.generateAvatar = function (name) {
|
|
|
|
const color = randomcolor({
|
2017-03-08 10:45:51 +00:00
|
|
|
seed: name,
|
|
|
|
luminosity: 'dark'
|
|
|
|
})
|
2018-04-12 11:14:42 +00:00
|
|
|
const letter = name.substring(0, 1).toUpperCase()
|
2017-01-06 04:37:40 +00:00
|
|
|
|
2018-04-12 11:14:42 +00:00
|
|
|
let svg = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>'
|
2017-03-08 10:45:51 +00:00
|
|
|
svg += '<svg xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns="http://www.w3.org/2000/svg" height="96" width="96" version="1.1" viewBox="0 0 96 96">'
|
|
|
|
svg += '<g>'
|
|
|
|
svg += '<rect width="96" height="96" fill="' + color + '" />'
|
|
|
|
svg += '<text font-size="64px" font-family="sans-serif" text-anchor="middle" fill="#ffffff">'
|
|
|
|
svg += '<tspan x="48" y="72" stroke-width=".26458px" fill="#ffffff">' + letter + '</tspan>'
|
|
|
|
svg += '</text>'
|
|
|
|
svg += '</g>'
|
|
|
|
svg += '</svg>'
|
2017-01-06 04:37:40 +00:00
|
|
|
|
2018-04-12 11:14:42 +00:00
|
|
|
return svg
|
|
|
|
}
|
|
|
|
|
2018-06-23 21:40:46 +00:00
|
|
|
exports.generateAvatarURL = function (name, email = '', big = true) {
|
|
|
|
let photo
|
2018-07-27 11:29:08 +00:00
|
|
|
if (typeof email !== 'string') {
|
|
|
|
email = '' + name + '@example.com'
|
|
|
|
}
|
2019-03-03 13:55:14 +00:00
|
|
|
name=encodeURIComponent(name)
|
2018-07-27 11:29:08 +00:00
|
|
|
|
2018-12-22 13:19:02 +00:00
|
|
|
let hash = crypto.createHash('md5')
|
|
|
|
hash.update(email.toLowerCase())
|
|
|
|
let hexDigest = hash.digest('hex')
|
|
|
|
|
2018-06-23 21:40:46 +00:00
|
|
|
if (email !== '' && config.allowGravatar) {
|
2019-03-17 22:47:30 +00:00
|
|
|
photo = 'https://cdn.libravatar.org/avatar/' + hexDigest;
|
2018-06-23 21:40:46 +00:00
|
|
|
if (big) {
|
|
|
|
photo += '?s=400'
|
|
|
|
} else {
|
|
|
|
photo += '?s=96'
|
|
|
|
}
|
|
|
|
} else {
|
2018-12-22 13:19:02 +00:00
|
|
|
photo = config.serverURL + '/user/' + (name || email.substring(0, email.lastIndexOf('@')) || hexDigest) + '/avatar.svg'
|
2018-06-23 21:40:46 +00:00
|
|
|
}
|
|
|
|
return photo
|
2017-03-08 10:45:51 +00:00
|
|
|
}
|