This repository has been archived on 2020-03-24. You can view files and clone it, but cannot push or open issues or pull requests.
quay/static/js/services/avatar/avatar.service.impl.ts
Alec Merdler 7a352ddfbc Use ng-metadata as a Backport of Angular 2+ API (#2486)
* starting UtilService refactor

* pre find-replace angular.module('quay') => angular.module('QuayModule')

* successfully switched to ng-metadata for backported Angular2 API

* working with parent component reference in child

* fixing @Output to use EventEmitter

* fixed @Output events for custom git trigger

* more fixes

* refactored QuayPages module for backwards-compatibility

* reinitialized test.db

* use minified libraries

* replaced references for angular-ts-decorators

* fixed ng-show
2017-04-05 14:14:08 -07:00

50 lines
No EOL
1.3 KiB
TypeScript

import { AvatarService } from './avatar.service';
import { Injectable, Inject } from 'ng-metadata/core';
@Injectable(AvatarService.name)
export class AvatarServiceImpl implements AvatarService {
private cache: {[cacheKey: string]: string} = {};
constructor(@Inject('Config') private Config: any,
@Inject('md5') private md5: any) {
}
public getAvatar(hash: string, size: number = 16, notFound: string = '404'): string {
var avatarURL: string;
switch (this.Config['AVATAR_KIND']) {
case 'local':
avatarURL = `/avatar/${hash}?size=${size}`;
break;
case 'gravatar':
avatarURL = `//www.gravatar.com/avatar/${hash}?d=${notFound}&size=${size}`;
break;
}
return avatarURL;
}
public computeHash(email: string = '', name: string = ''): string {
const cacheKey: string = email + ':' + name;
if (this.cache[cacheKey]) {
return this.cache[cacheKey];
}
var hash: string = this.md5.createHash(email.toString().toLowerCase());
switch (this.Config['AVATAR_KIND']) {
case 'local':
if (name) {
hash = name[0] + hash;
} else if (email) {
hash = email[0] + hash;
}
break;
}
return this.cache[cacheKey] = hash;
}
}