mirror of
https://github.com/muety/wakapi.git
synced 2023-08-10 21:12:56 +03:00
fix: non-ascii project badges (resolve #357)
chore: locally generated badges (resolve #348)
This commit is contained in:
@ -5,7 +5,9 @@ import (
|
||||
"github.com/gorilla/mux"
|
||||
lru "github.com/hashicorp/golang-lru"
|
||||
conf "github.com/muety/wakapi/config"
|
||||
"github.com/muety/wakapi/utils"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
type AvatarHandler struct {
|
||||
@ -33,6 +35,10 @@ func (h *AvatarHandler) RegisterRoutes(router *mux.Router) {
|
||||
func (h *AvatarHandler) Get(w http.ResponseWriter, r *http.Request) {
|
||||
hash := mux.Vars(r)["hash"]
|
||||
|
||||
if utils.IsNoCache(r, 1*time.Hour) {
|
||||
h.cache.Remove(hash)
|
||||
}
|
||||
|
||||
if !h.cache.Contains(hash) {
|
||||
h.cache.Add(hash, avatars.MakeMaleAvatar(hash))
|
||||
}
|
||||
|
98
routes/api/badge.go
Normal file
98
routes/api/badge.go
Normal file
@ -0,0 +1,98 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duke-git/lancet/v2/maputil"
|
||||
"github.com/duke-git/lancet/v2/slice"
|
||||
"github.com/gorilla/mux"
|
||||
conf "github.com/muety/wakapi/config"
|
||||
"github.com/muety/wakapi/models"
|
||||
v1 "github.com/muety/wakapi/models/compat/shields/v1"
|
||||
routeutils "github.com/muety/wakapi/routes/utils"
|
||||
"github.com/muety/wakapi/services"
|
||||
"github.com/muety/wakapi/utils"
|
||||
"github.com/narqo/go-badge"
|
||||
"github.com/patrickmn/go-cache"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
type BadgeHandler struct {
|
||||
config *conf.Config
|
||||
cache *cache.Cache
|
||||
userSrvc services.IUserService
|
||||
summarySrvc services.ISummaryService
|
||||
}
|
||||
|
||||
func NewBadgeHandler(userService services.IUserService, summaryService services.ISummaryService) *BadgeHandler {
|
||||
return &BadgeHandler{
|
||||
config: conf.Get(),
|
||||
cache: cache.New(time.Hour, time.Hour),
|
||||
userSrvc: userService,
|
||||
summarySrvc: summaryService,
|
||||
}
|
||||
}
|
||||
|
||||
func (h *BadgeHandler) RegisterRoutes(router *mux.Router) {
|
||||
r := router.PathPrefix("/badge/{user}").Subrouter()
|
||||
r.Methods(http.MethodGet).HandlerFunc(h.Get)
|
||||
}
|
||||
|
||||
func (h *BadgeHandler) Get(w http.ResponseWriter, r *http.Request) {
|
||||
user, err := h.userSrvc.GetUserById(mux.Vars(r)["user"])
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
interval, filters, err := routeutils.GetBadgeParams(r, user)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusForbidden)
|
||||
w.Write([]byte(err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
cacheKey := fmt.Sprintf("%s_%v_%s", user.ID, *interval.Key, filters.Hash())
|
||||
noCache := utils.IsNoCache(r, 1*time.Hour)
|
||||
if cacheResult, ok := h.cache.Get(cacheKey); ok && !noCache {
|
||||
respondSvg(w, cacheResult.([]byte))
|
||||
return
|
||||
}
|
||||
|
||||
params := &models.SummaryParams{
|
||||
From: interval.Start,
|
||||
To: interval.End,
|
||||
User: user,
|
||||
Filters: filters,
|
||||
}
|
||||
|
||||
summary, err, status := routeutils.LoadUserSummaryByParams(h.summarySrvc, params)
|
||||
if err != nil {
|
||||
w.WriteHeader(status)
|
||||
w.Write([]byte(err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
badgeData := v1.NewBadgeDataFrom(summary)
|
||||
if customLabel := r.URL.Query().Get("label"); customLabel != "" {
|
||||
badgeData.Label = customLabel
|
||||
}
|
||||
if customColor := r.URL.Query().Get("color"); customColor != "" {
|
||||
badgeData.Color = customColor
|
||||
}
|
||||
|
||||
if badgeData.Color[0:1] != "#" && !slice.Contain(maputil.Keys(badge.ColorScheme), badgeData.Color) {
|
||||
badgeData.Color = "#" + badgeData.Color
|
||||
}
|
||||
|
||||
badgeSvg, err := badge.RenderBytes(badgeData.Label, badgeData.Message, badge.Color(badgeData.Color))
|
||||
h.cache.SetDefault(cacheKey, badgeSvg)
|
||||
respondSvg(w, badgeSvg)
|
||||
}
|
||||
|
||||
func respondSvg(w http.ResponseWriter, data []byte) {
|
||||
w.Header().Set("Content-Type", "image/svg+xml")
|
||||
w.Header().Set("Cache-Control", "max-age=3600")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write(data)
|
||||
}
|
Reference in New Issue
Block a user