diff --git a/plugins/tappd/image.go b/plugins/tappd/image.go index 2aca952..a923789 100644 --- a/plugins/tappd/image.go +++ b/plugins/tappd/image.go @@ -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(). diff --git a/plugins/tappd/tappd.go b/plugins/tappd/tappd.go index c1114ac..1de3b4a 100644 --- a/plugins/tappd/tappd.go +++ b/plugins/tappd/tappd.go @@ -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 { @@ -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() { diff --git a/plugins/tappd/web.go b/plugins/tappd/web.go index d4d1fba..d041fac 100644 --- a/plugins/tappd/web.go +++ b/plugins/tappd/web.go @@ -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) }