2020-03-01 21:30:37 +01:00
|
|
|
// Vikunja is a to-do-list application to facilitate your life.
|
|
|
|
// Copyright 2018-2020 Vikunja and contributors. All rights reserved.
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
2020-12-23 16:41:52 +01:00
|
|
|
// it under the terms of the GNU Affero General Public Licensee as published by
|
2020-03-01 21:30:37 +01:00
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2020-12-23 16:41:52 +01:00
|
|
|
// GNU Affero General Public Licensee for more details.
|
2020-03-01 21:30:37 +01:00
|
|
|
//
|
2020-12-23 16:41:52 +01:00
|
|
|
// You should have received a copy of the GNU Affero General Public Licensee
|
2020-03-01 21:30:37 +01:00
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package gravatar
|
|
|
|
|
|
|
|
import (
|
2020-10-11 22:10:03 +02:00
|
|
|
"context"
|
2020-03-01 21:30:37 +01:00
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
2020-10-11 22:10:03 +02:00
|
|
|
|
|
|
|
"code.vikunja.io/api/pkg/config"
|
|
|
|
"code.vikunja.io/api/pkg/log"
|
|
|
|
"code.vikunja.io/api/pkg/user"
|
|
|
|
"code.vikunja.io/api/pkg/utils"
|
2020-03-01 21:30:37 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type avatar struct {
|
|
|
|
content []byte
|
|
|
|
loadedAt time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
// Provider is the gravatar provider
|
|
|
|
type Provider struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
// avatars is a global map which contains cached avatars of the users
|
|
|
|
var avatars map[string]*avatar
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
avatars = make(map[string]*avatar)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetAvatar implements getting the avatar for the user
|
|
|
|
func (g *Provider) GetAvatar(user *user.User, size int64) ([]byte, string, error) {
|
|
|
|
sizeString := strconv.FormatInt(size, 10)
|
|
|
|
cacheKey := user.Username + "_" + sizeString
|
|
|
|
a, exists := avatars[cacheKey]
|
|
|
|
var needsRefetch bool
|
|
|
|
if exists {
|
|
|
|
// elaped is alway < 0 so the next check would always succeed.
|
|
|
|
// To have it make sense, we flip that.
|
|
|
|
elapsed := time.Until(a.loadedAt) * -1
|
2020-03-01 22:10:25 +01:00
|
|
|
needsRefetch = elapsed > time.Duration(config.AvatarGravaterExpiration.GetInt64())*time.Second
|
2020-03-01 21:30:37 +01:00
|
|
|
if needsRefetch {
|
|
|
|
log.Debugf("Refetching avatar for user %d after %v", user.ID, elapsed)
|
|
|
|
} else {
|
|
|
|
log.Debugf("Serving avatar for user %d from cache", user.ID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !exists || needsRefetch {
|
|
|
|
log.Debugf("Gravatar for user %d with size %d not cached, requesting from gravatar...", user.ID, size)
|
2020-10-11 22:10:03 +02:00
|
|
|
req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, "https://www.gravatar.com/avatar/"+utils.Md5String(user.Email)+"?s="+sizeString+"&d=mp", nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, "", err
|
|
|
|
}
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
2020-03-01 21:30:37 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, "", err
|
|
|
|
}
|
2020-10-11 22:10:03 +02:00
|
|
|
defer resp.Body.Close()
|
2020-03-01 21:30:37 +01:00
|
|
|
avatarContent, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, "", err
|
|
|
|
}
|
|
|
|
avatars[cacheKey] = &avatar{
|
|
|
|
content: avatarContent,
|
|
|
|
loadedAt: time.Now(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return avatars[cacheKey].content, "image/jpg", nil
|
|
|
|
}
|