mirror of
https://github.com/verdaccio/verdaccio.git
synced 2024-12-16 21:56:25 -05:00
refactor: 🔨 move string md5 and get avatar to utils
This commit is contained in:
parent
cb98ecc096
commit
ba6811bd91
3 changed files with 29 additions and 19 deletions
|
@ -3,7 +3,6 @@
|
|||
const bodyParser = require('body-parser');
|
||||
const express = require('express');
|
||||
const marked = require('marked');
|
||||
const crypto = require('crypto');
|
||||
const _ = require('lodash');
|
||||
const Search = require('../../lib/search');
|
||||
const Middleware = require('./middleware');
|
||||
|
@ -15,6 +14,7 @@ const route = express.Router(); // eslint-disable-line
|
|||
const async = require('async');
|
||||
const HTTPError = require('http-errors');
|
||||
const Utils = require('../../lib/utils');
|
||||
const {generateGravatarUrl} = require('../../utils/user');
|
||||
|
||||
/*
|
||||
This file include all verdaccio only API(Web UI), for npm API please see ../endpoint/
|
||||
|
@ -168,13 +168,12 @@ module.exports = function(config, auth, storage) {
|
|||
delete info[property];
|
||||
}));
|
||||
|
||||
let defaultGravatar = 'https://www.gravatar.com/avatar/00000000000000000000000000000000?d=mm';
|
||||
|
||||
if (typeof _.get(info, 'latest.author.email') === 'string') {
|
||||
info.latest.author.avatar = generateGravatarUrl(info.latest.author.email);
|
||||
} else {
|
||||
// _.get can't guarantee author property exist
|
||||
_.set(info, 'latest.author.avatar', defaultGravatar);
|
||||
_.set(info, 'latest.author.avatar', generateGravatarUrl());
|
||||
}
|
||||
|
||||
if (_.get(info, 'latest.contributors.length', 0) > 0) {
|
||||
|
@ -182,7 +181,7 @@ module.exports = function(config, auth, storage) {
|
|||
if (typeof contributor.email === 'string') {
|
||||
contributor.avatar = generateGravatarUrl(contributor.email);
|
||||
} else {
|
||||
contributor.avatar = defaultGravatar;
|
||||
contributor.avatar = generateGravatarUrl();
|
||||
}
|
||||
|
||||
return contributor;
|
||||
|
@ -206,15 +205,3 @@ module.exports = function(config, auth, storage) {
|
|||
|
||||
return route;
|
||||
};
|
||||
|
||||
/**
|
||||
* Generate gravatar url from email address
|
||||
* @param {string} email
|
||||
* @return {string} url
|
||||
*/
|
||||
function generateGravatarUrl(email) {
|
||||
email = email.trim().toLocaleLowerCase();
|
||||
let emailMD5 = crypto.createHash('md5').update(email).digest('hex');
|
||||
|
||||
return `https://www.gravatar.com/avatar/${emailMD5}`;
|
||||
}
|
||||
|
|
|
@ -1,5 +1,13 @@
|
|||
/* eslint prefer-rest-params:off */
|
||||
// @flow
|
||||
import crypto from 'crypto';
|
||||
|
||||
export const spliceURL = function spliceURL(...args) {
|
||||
export function spliceURL(...args: Array<string>): string {
|
||||
return Array.from(args).reduce((lastResult, current) => lastResult + current).replace(/([^:])(\/)+(.)/g, `$1/$3`);
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Get MD5 from string
|
||||
*/
|
||||
export function stringToMD5(string: string): string {
|
||||
return crypto.createHash('md5').update(string).digest('hex');
|
||||
}
|
||||
|
|
15
src/utils/user.js
Normal file
15
src/utils/user.js
Normal file
|
@ -0,0 +1,15 @@
|
|||
// @flow
|
||||
import {stringToMD5} from './string';
|
||||
|
||||
/**
|
||||
* Generate gravatar url from email address
|
||||
*/
|
||||
export function generateGravatarUrl(email?: string): string {
|
||||
if (typeof email === 'string') {
|
||||
email = email.trim().toLocaleLowerCase();
|
||||
let emailMD5 = stringToMD5(email);
|
||||
return `https://www.gravatar.com/avatar/${emailMD5}`;
|
||||
} else {
|
||||
return 'https://www.gravatar.com/avatar/00000000000000000000000000000000?d=mm';
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue