mirror of https://github.com/velour/catbase.git
cowboy: use cache to set bounds
This commit is contained in:
parent
112ccea89a
commit
2ce2b01295
|
@ -63,7 +63,7 @@ func (p *Cowboy) register() {
|
||||||
func (p *Cowboy) makeCowboy(r bot.Request) {
|
func (p *Cowboy) makeCowboy(r bot.Request) {
|
||||||
what := r.Values["what"]
|
what := r.Values["what"]
|
||||||
// This'll add the image to the cowboy_cache before discord tries to access it over http
|
// This'll add the image to the cowboy_cache before discord tries to access it over http
|
||||||
_, err := cowboy(p.c, p.emojyPath, p.baseEmojyURL, what)
|
i, err := cowboy(p.c, p.emojyPath, p.baseEmojyURL, what)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error().Err(err).Msg(":cowboy_fail:")
|
log.Error().Err(err).Msg(":cowboy_fail:")
|
||||||
p.b.Send(r.Conn, bot.Ephemeral, r.Msg.Channel, r.Msg.User.ID, "Hey cowboy, that image wasn't there.")
|
p.b.Send(r.Conn, bot.Ephemeral, r.Msg.Channel, r.Msg.User.ID, "Hey cowboy, that image wasn't there.")
|
||||||
|
@ -76,8 +76,8 @@ func (p *Cowboy) makeCowboy(r bot.Request) {
|
||||||
p.b.Send(r.Conn, bot.Message, r.Msg.Channel, "", bot.ImageAttachment{
|
p.b.Send(r.Conn, bot.Message, r.Msg.Channel, "", bot.ImageAttachment{
|
||||||
URL: u,
|
URL: u,
|
||||||
AltTxt: fmt.Sprintf("%s: %s", r.Msg.User.Name, r.Msg.Body),
|
AltTxt: fmt.Sprintf("%s: %s", r.Msg.User.Name, r.Msg.Body),
|
||||||
Width: 64,
|
Width: i.Bounds().Max.X,
|
||||||
Height: 64,
|
Height: i.Bounds().Max.Y,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -18,7 +18,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
cowboyCache = map[string][]byte{}
|
cowboyCache = map[string]image.Image{}
|
||||||
cowboyMutex = sync.Mutex{}
|
cowboyMutex = sync.Mutex{}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -72,7 +72,7 @@ func cowboyifyImage(c *config.Config, emojyPath string, input image.Image) (imag
|
||||||
return dst, nil
|
return dst, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func cowboy(c *config.Config, emojyPath, baseEmojyURL, name string) ([]byte, error) {
|
func cowboy(c *config.Config, emojyPath, baseEmojyURL, name string) (image.Image, error) {
|
||||||
cowboyMutex.Lock()
|
cowboyMutex.Lock()
|
||||||
defer cowboyMutex.Unlock()
|
defer cowboyMutex.Unlock()
|
||||||
if img, ok := cowboyCache[name]; ok {
|
if img, ok := cowboyCache[name]; ok {
|
||||||
|
@ -85,6 +85,14 @@ func cowboy(c *config.Config, emojyPath, baseEmojyURL, name string) ([]byte, err
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
img, err := cowboyifyImage(c, emojyPath, emjy)
|
img, err := cowboyifyImage(c, emojyPath, emjy)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
cowboyCache[name] = img
|
||||||
|
return img, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func encode(img image.Image, err error) ([]byte, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -93,12 +101,11 @@ func cowboy(c *config.Config, emojyPath, baseEmojyURL, name string) ([]byte, err
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
cowboyCache[name] = w.Bytes()
|
|
||||||
return w.Bytes(), nil
|
return w.Bytes(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func cowboyClearCache() {
|
func cowboyClearCache() {
|
||||||
cowboyMutex.Lock()
|
cowboyMutex.Lock()
|
||||||
defer cowboyMutex.Unlock()
|
defer cowboyMutex.Unlock()
|
||||||
cowboyCache = map[string][]byte{}
|
cowboyCache = map[string]image.Image{}
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,7 +15,7 @@ func (p *Cowboy) registerWeb() {
|
||||||
|
|
||||||
func (p *Cowboy) handleImage(w http.ResponseWriter, r *http.Request) {
|
func (p *Cowboy) handleImage(w http.ResponseWriter, r *http.Request) {
|
||||||
what := chi.URLParam(r, "what")
|
what := chi.URLParam(r, "what")
|
||||||
img, err := cowboy(p.c, p.emojyPath, p.baseEmojyURL, what)
|
img, err := encode(cowboy(p.c, p.emojyPath, p.baseEmojyURL, what))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
w.WriteHeader(500)
|
w.WriteHeader(500)
|
||||||
fmt.Fprintf(w, "Error: %s", err)
|
fmt.Fprintf(w, "Error: %s", err)
|
||||||
|
|
Loading…
Reference in New Issue