mirror of https://github.com/velour/catbase.git
tappd: save images and serve them
This commit is contained in:
parent
df9db4e6fd
commit
c147e65497
|
@ -4,6 +4,7 @@ import (
|
|||
"bytes"
|
||||
"github.com/disintegration/imageorient"
|
||||
"github.com/fogleman/gg"
|
||||
"github.com/gabriel-vasile/mimetype"
|
||||
"github.com/nfnt/resize"
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/velour/catbase/plugins/meme"
|
||||
|
@ -127,6 +128,7 @@ func (p *Tappd) getAndOverlay(id, srcURL string, texts []textSpec) (imageInfo, e
|
|||
return imageInfo{}, err
|
||||
}
|
||||
bounds := img.Bounds()
|
||||
mime := mimetype.Detect(data)
|
||||
info := imageInfo{
|
||||
ID: id,
|
||||
SrcURL: srcURL,
|
||||
|
@ -135,6 +137,8 @@ func (p *Tappd) getAndOverlay(id, srcURL string, texts []textSpec) (imageInfo, e
|
|||
Repr: data,
|
||||
W: bounds.Dx(),
|
||||
H: bounds.Dy(),
|
||||
MimeType: mime.String(),
|
||||
FileName: id + mime.Extension(),
|
||||
}
|
||||
p.imageMap[id] = info
|
||||
log.Debug().
|
||||
|
|
|
@ -4,12 +4,13 @@ import (
|
|||
"bytes"
|
||||
"fmt"
|
||||
"github.com/bwmarrin/discordgo"
|
||||
"github.com/gabriel-vasile/mimetype"
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/velour/catbase/bot"
|
||||
"github.com/velour/catbase/config"
|
||||
"github.com/velour/catbase/connectors/discord"
|
||||
"image"
|
||||
"os"
|
||||
"path"
|
||||
"regexp"
|
||||
"time"
|
||||
)
|
||||
|
@ -28,6 +29,8 @@ type imageInfo struct {
|
|||
Repr []byte
|
||||
W int
|
||||
H int
|
||||
MimeType string
|
||||
FileName string
|
||||
}
|
||||
|
||||
func New(b bot.Bot) *Tappd {
|
||||
|
@ -144,10 +147,9 @@ func (p *Tappd) tap(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
|||
Height: info.H,
|
||||
},
|
||||
}}
|
||||
mime := mimetype.Detect(info.Repr)
|
||||
files := []*discordgo.File{{
|
||||
Name: info.ID + mime.Extension(),
|
||||
ContentType: mime.String(),
|
||||
Name: info.FileName,
|
||||
ContentType: info.MimeType,
|
||||
Reader: bytes.NewBuffer(info.Repr),
|
||||
}}
|
||||
content := info.BotURL
|
||||
|
@ -175,6 +177,16 @@ func (p *Tappd) tap(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
|||
if err != nil {
|
||||
log.Error().Err(err).Msgf("error recording tap")
|
||||
}
|
||||
imgPath := p.c.Get("tappd.imagepath", "tappdimg")
|
||||
err = os.MkdirAll(imgPath, 0775)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msgf("error creating directory")
|
||||
return
|
||||
}
|
||||
err = os.WriteFile(path.Join(imgPath, info.FileName), info.Repr, 0664)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msgf("error writing file")
|
||||
}
|
||||
}
|
||||
|
||||
func (p *Tappd) register() {
|
||||
|
|
|
@ -2,9 +2,13 @@ package tappd
|
|||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/rs/zerolog/log"
|
||||
"net/http"
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func (p *Tappd) registerWeb() {
|
||||
|
@ -13,19 +17,35 @@ func (p *Tappd) registerWeb() {
|
|||
p.b.RegisterWeb(r, "/tappd/{id}")
|
||||
}
|
||||
|
||||
func (p *Tappd) getImg(id string) ([]byte, error) {
|
||||
imgData, ok := p.imageMap[id]
|
||||
if ok {
|
||||
return imgData.Repr, nil
|
||||
}
|
||||
imgPath := p.c.Get("tappd.imagepath", "tappdimg")
|
||||
entries, err := os.ReadDir(imgPath)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msgf("can't read image path")
|
||||
return nil, err
|
||||
}
|
||||
for _, e := range entries {
|
||||
if strings.HasPrefix(e.Name(), id) {
|
||||
return os.ReadFile(path.Join(imgPath, e.Name()))
|
||||
}
|
||||
}
|
||||
log.Error().Msgf("didn't find image")
|
||||
return nil, fmt.Errorf("file not found")
|
||||
}
|
||||
|
||||
func (p *Tappd) serveImage(w http.ResponseWriter, r *http.Request) {
|
||||
id := chi.URLParam(r, "id")
|
||||
imgData, ok := p.imageMap[id]
|
||||
log.Debug().
|
||||
Str("id", id).
|
||||
Str("SrcURL", imgData.SrcURL).
|
||||
Bool("ok", ok).
|
||||
Msgf("creating request")
|
||||
if !ok {
|
||||
data, err := p.getImg(id)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msgf("error getting image")
|
||||
w.WriteHeader(404)
|
||||
out, _ := json.Marshal(struct{ err string }{"could not find ID"})
|
||||
out, _ := json.Marshal(struct{ err string }{"could not find ID: " + err.Error()})
|
||||
w.Write(out)
|
||||
return
|
||||
}
|
||||
w.Write(imgData.Repr)
|
||||
w.Write(data)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue