tcp.ac/txt.go

75 lines
1.7 KiB
Go
Raw Normal View History

2021-02-15 20:52:35 +00:00
package main
import (
2022-01-21 12:58:22 +00:00
"errors"
"strings"
2022-07-18 10:41:29 +00:00
"git.tcp.direct/kayos/common/squish"
termdumpster "git.tcp.direct/kayos/putxt"
2021-02-15 20:52:35 +00:00
"github.com/gin-gonic/gin"
"github.com/rs/zerolog/log"
2022-07-08 20:23:20 +00:00
"git.tcp.direct/tcp.direct/tcp.ac/config"
2021-07-28 07:31:34 +00:00
)
2022-07-18 10:41:29 +00:00
type textValidator struct {
in []byte
out []byte
2021-07-28 07:31:34 +00:00
}
2021-02-15 20:52:35 +00:00
2022-07-18 10:41:29 +00:00
type textIngestor struct{}
2021-02-15 20:52:35 +00:00
2022-07-18 10:41:29 +00:00
func (i textIngestor) Ingest(data []byte) ([]byte, error) {
tp := &textValidator{in: data}
err := post(tp, tp, Text, false)
2021-02-15 20:52:35 +00:00
if err != nil {
2022-07-18 10:41:29 +00:00
return nil, err
2021-02-15 20:52:35 +00:00
}
2022-07-18 10:41:29 +00:00
return tp.out, nil
2021-02-15 20:52:35 +00:00
}
2022-07-18 10:41:29 +00:00
func (i textValidator) finalize(data []byte) ([]byte, error) {
return squish.Gunzip(data)
}
2021-07-28 07:31:34 +00:00
2022-07-18 10:41:29 +00:00
func (i textValidator) getContentType(c *gin.Context) (string, error) {
return "text/plain", nil
}
2021-07-28 07:31:34 +00:00
2022-07-18 10:41:29 +00:00
func (i textValidator) checkURL(c *gin.Context) error {
sUID := strings.Split(c.Param("uid"), ".")
var fExt string
if len(sUID) > 1 {
fExt = strings.ToLower(sUID[1])
log.Trace().Str("caller", c.Request.RequestURI).Str("ext", fExt).Msg("detected file extension")
2021-02-15 20:52:35 +00:00
if fExt != "txt" {
2022-07-18 10:41:29 +00:00
return errors.New("bad file extension")
2021-02-15 20:52:35 +00:00
}
2022-07-18 10:41:29 +00:00
c.Set("url.extension", fExt)
2021-02-15 20:52:35 +00:00
}
2022-07-18 10:41:29 +00:00
return nil
2021-02-15 20:52:35 +00:00
}
2022-07-18 10:41:29 +00:00
func (i textValidator) checkContent(c *gin.Context, data []byte) error {
return nil
}
2021-02-15 20:52:35 +00:00
2022-07-18 10:41:29 +00:00
func (i textValidator) checkAndScrubPost(c any) ([]byte, error) {
if i.in == nil {
return nil, errors.New("no data")
2021-07-29 19:40:53 +00:00
}
2022-07-18 10:41:29 +00:00
return i.in, nil
2021-07-28 07:31:34 +00:00
}
2021-02-15 20:52:35 +00:00
2022-07-18 10:41:29 +00:00
func serveTermbin() error {
td := termdumpster.NewTermDumpster(textIngestor{}).WithGzip().WithLogger(&log.Logger).
WithMaxSize(int64(config.KVMaxValueSizeMB * 1024 * 1024))
2022-07-08 20:23:20 +00:00
split := strings.Split(config.TermbinListen, ":")
2022-07-18 10:41:29 +00:00
log.Info().Str("listen", config.TermbinListen).Msg("starting termbin")
err := td.Listen(split[0], split[1])
2021-07-28 07:31:34 +00:00
if err != nil {
2022-07-18 10:41:29 +00:00
return err
2021-02-15 20:52:35 +00:00
}
2022-07-18 10:41:29 +00:00
return nil
2021-02-15 20:52:35 +00:00
}