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) {
|
||||
what := r.Values["what"]
|
||||
// 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 {
|
||||
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.")
|
||||
|
@ -76,8 +76,8 @@ func (p *Cowboy) makeCowboy(r bot.Request) {
|
|||
p.b.Send(r.Conn, bot.Message, r.Msg.Channel, "", bot.ImageAttachment{
|
||||
URL: u,
|
||||
AltTxt: fmt.Sprintf("%s: %s", r.Msg.User.Name, r.Msg.Body),
|
||||
Width: 64,
|
||||
Height: 64,
|
||||
Width: i.Bounds().Max.X,
|
||||
Height: i.Bounds().Max.Y,
|
||||
})
|
||||
}
|
||||
|
||||
|
|
|
@ -18,7 +18,7 @@ import (
|
|||
)
|
||||
|
||||
var (
|
||||
cowboyCache = map[string][]byte{}
|
||||
cowboyCache = map[string]image.Image{}
|
||||
cowboyMutex = sync.Mutex{}
|
||||
)
|
||||
|
||||
|
@ -72,7 +72,7 @@ func cowboyifyImage(c *config.Config, emojyPath string, input image.Image) (imag
|
|||
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()
|
||||
defer cowboyMutex.Unlock()
|
||||
if img, ok := cowboyCache[name]; ok {
|
||||
|
@ -85,6 +85,14 @@ func cowboy(c *config.Config, emojyPath, baseEmojyURL, name string) ([]byte, err
|
|||
return nil, err
|
||||
}
|
||||
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 {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -93,12 +101,11 @@ func cowboy(c *config.Config, emojyPath, baseEmojyURL, name string) ([]byte, err
|
|||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
cowboyCache[name] = w.Bytes()
|
||||
return w.Bytes(), nil
|
||||
}
|
||||
|
||||
func cowboyClearCache() {
|
||||
cowboyMutex.Lock()
|
||||
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) {
|
||||
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 {
|
||||
w.WriteHeader(500)
|
||||
fmt.Fprintf(w, "Error: %s", err)
|
||||
|
|
Loading…
Reference in New Issue