2020-04-11 09:01:30 -04:00
|
|
|
import { createHash } from 'crypto'
|
|
|
|
import randomColor from 'randomcolor'
|
2020-04-12 07:11:06 -04:00
|
|
|
import { config } from './config'
|
2017-01-05 23:37:40 -05:00
|
|
|
|
|
|
|
// core
|
2020-04-11 09:01:30 -04:00
|
|
|
export function generateAvatar (name: string): string {
|
|
|
|
const color = randomColor({
|
2017-03-08 05:45:51 -05:00
|
|
|
seed: name,
|
|
|
|
luminosity: 'dark'
|
|
|
|
})
|
2018-04-12 07:14:42 -04:00
|
|
|
const letter = name.substring(0, 1).toUpperCase()
|
2017-01-05 23:37:40 -05:00
|
|
|
|
2018-04-12 07:14:42 -04:00
|
|
|
let svg = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>'
|
2017-03-08 05:45:51 -05: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-05 23:37:40 -05:00
|
|
|
|
2018-04-12 07:14:42 -04:00
|
|
|
return svg
|
|
|
|
}
|
|
|
|
|
2020-04-11 09:01:30 -04:00
|
|
|
export function generateAvatarURL (name: string, email = '', big = true): string {
|
2018-06-23 17:40:46 -04:00
|
|
|
let photo
|
2020-04-11 09:01:30 -04:00
|
|
|
if (email.length === 0) {
|
2018-07-27 07:29:08 -04:00
|
|
|
email = '' + name + '@example.com'
|
|
|
|
}
|
2019-05-30 18:27:56 -04:00
|
|
|
name = encodeURIComponent(name)
|
2018-07-27 07:29:08 -04:00
|
|
|
|
2020-04-11 09:01:30 -04:00
|
|
|
const hash = createHash('md5')
|
2018-12-22 08:19:02 -05:00
|
|
|
hash.update(email.toLowerCase())
|
2020-04-11 09:01:30 -04:00
|
|
|
const hexDigest = hash.digest('hex')
|
2018-12-22 08:19:02 -05:00
|
|
|
|
2018-06-23 17:40:46 -04:00
|
|
|
if (email !== '' && config.allowGravatar) {
|
2019-05-30 18:27:56 -04:00
|
|
|
photo = 'https://cdn.libravatar.org/avatar/' + hexDigest
|
2018-06-23 17:40:46 -04:00
|
|
|
if (big) {
|
|
|
|
photo += '?s=400'
|
|
|
|
} else {
|
|
|
|
photo += '?s=96'
|
|
|
|
}
|
|
|
|
} else {
|
2018-12-22 08:19:02 -05:00
|
|
|
photo = config.serverURL + '/user/' + (name || email.substring(0, email.lastIndexOf('@')) || hexDigest) + '/avatar.svg'
|
2018-06-23 17:40:46 -04:00
|
|
|
}
|
|
|
|
return photo
|
2017-03-08 05:45:51 -05:00
|
|
|
}
|