mirror of https://github.com/velour/catbase.git
Compare commits
3 Commits
df9db4e6fd
...
04fecf1987
Author | SHA1 | Date |
---|---|---|
Chris Sexton | 04fecf1987 | |
Chris Sexton | bf54f421fe | |
Chris Sexton | 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,14 +128,17 @@ 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,
|
||||
BotURL: u.String(),
|
||||
Img: img,
|
||||
Repr: data,
|
||||
W: bounds.Dx(),
|
||||
H: bounds.Dy(),
|
||||
ID: id,
|
||||
SrcURL: srcURL,
|
||||
BotURL: u.String(),
|
||||
Img: img,
|
||||
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"
|
||||
)
|
||||
|
@ -21,13 +22,15 @@ type Tappd struct {
|
|||
}
|
||||
|
||||
type imageInfo struct {
|
||||
ID string
|
||||
SrcURL string
|
||||
BotURL string
|
||||
Img image.Image
|
||||
Repr []byte
|
||||
W int
|
||||
H int
|
||||
ID string
|
||||
SrcURL string
|
||||
BotURL string
|
||||
Img image.Image
|
||||
Repr []byte
|
||||
W int
|
||||
H int
|
||||
MimeType string
|
||||
FileName string
|
||||
}
|
||||
|
||||
func New(b bot.Bot) *Tappd {
|
||||
|
@ -50,7 +53,7 @@ func (p *Tappd) mkDB() error {
|
|||
return err
|
||||
}
|
||||
_, err = tx.Exec(`create table if not exists tappd (
|
||||
id integer primary key autoincrement,
|
||||
id string primary key,
|
||||
who string,
|
||||
channel string,
|
||||
message string,
|
||||
|
@ -67,15 +70,15 @@ func (p *Tappd) mkDB() error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (p *Tappd) log(who, channel, message string) error {
|
||||
func (p *Tappd) log(id, who, channel, message string) error {
|
||||
db := p.b.DB()
|
||||
tx, err := db.Beginx()
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return err
|
||||
}
|
||||
_, err = tx.Exec(`insert into tappd (who, channel, message, ts) values (?, ?, ? ,?)`,
|
||||
who, channel, message, time.Now())
|
||||
_, err = tx.Exec(`insert into tappd (id, who, channel, message, ts) values (?, ?, ?, ? ,?)`,
|
||||
id, who, channel, message, time.Now())
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return err
|
||||
|
@ -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
|
||||
|
@ -171,10 +173,20 @@ func (p *Tappd) tap(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
|||
log.Error().Err(err).Msgf("error with interaction")
|
||||
return
|
||||
}
|
||||
err = p.log(who, channel, shortMsg)
|
||||
err = p.log(info.ID, who, channel, shortMsg)
|
||||
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)
|
||||
}
|
||||
|
|
|
@ -53,9 +53,3 @@ func makeTwitchPlugin(t *testing.T) (*Twitch, *bot.MockBot) {
|
|||
|
||||
return c, mb
|
||||
}
|
||||
|
||||
func TestTwitch(t *testing.T) {
|
||||
b, mb := makeTwitchPlugin(t)
|
||||
b.twitchStatus(makeRequest("!twitch status"))
|
||||
assert.NotEmpty(t, mb.Messages)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue