Add Katakana and improve bannedword matching speed

This commit is contained in:
Mememan 2024-04-22 16:27:14 +02:00
parent 07e9b6ddb0
commit 0fb2b22480
6 changed files with 40 additions and 15 deletions

View file

@ -4,6 +4,7 @@ import (
"encoding/json"
"errors"
"os"
"regexp"
"strconv"
"strings"
@ -38,10 +39,12 @@ type Config struct {
BaseModel string `json:"baseModel"` // Base model, leave it empty to use fooocus' default
BannedWords []string `json:"bannedWords"` // user input will be blocked if it contains one of these
// Internal state from config
// Internal app state
state struct {
width int
height int
bannedWordsRegex *regexp.Regexp
}
}
@ -81,7 +84,7 @@ func parseConfig(name string, cfg *Config) (err error) {
cfg.Sharpness = 2.0
}
// parse config input
// initialize aspect ratio
parts := strings.Split(cfg.AspectRatio, "*")
if len(parts) != 2 {
return errors.New("invalid aspect ratio format (must be something like 1024*1024)")
@ -97,6 +100,8 @@ func parseConfig(name string, cfg *Config) (err error) {
if cfg.state.width <= 0 || cfg.state.height <= 0 {
return errors.New("invalid aspect ratio (must be something like 1024*1024)")
}
// initialize badword regex
config.state.bannedWordsRegex = regexp.MustCompile(strings.Join(cfg.BannedWords, "|"))
return nil
}

View file

@ -113,6 +113,11 @@
<img alt="Artwork" src="{{ .Img.URL }}" />
</div>
<div class="label">
{{ if .Label.TitleJpnIsKatakana }}
<div class="headline">
<span class="title">{{ .Label.TitleJpn }}</span>
</div>
{{end}}
<div class="headline">
<span class="title">{{ .Label.Title }}, </span> <span class="date">{{ .Label.Date }} </span>
</div>

View file

@ -11,6 +11,8 @@ import (
"golang.org/x/text/cases"
"golang.org/x/text/language"
"github.com/mochi-co/kana-tools"
_ "embed"
)
@ -37,7 +39,10 @@ type Img struct {
Height int
}
type Label struct {
Title string
Title string
TitleJpn string
TitleJpnIsKatakana bool
Date string
Size string
Technique string
@ -86,7 +91,7 @@ func serveGallery(w http.ResponseWriter, r *http.Request) {
Metadata: metadata.UserMetadata,
})
if err != nil {
serveBadSubdomainError(w)
serveInternalError(w)
ErrorLogger.Println(err)
}
}
@ -105,6 +110,7 @@ func writeDisplayPage(w http.ResponseWriter, r *http.Request, m FileData) (err e
}
widthCm := convToCm(width, PPI)
heightCm := convToCm(height, PPI)
titleKatakana := kana.ToKatakana(m.Name)
// Template building
tmplData := GalleryPageContent{
@ -116,10 +122,12 @@ func writeDisplayPage(w http.ResponseWriter, r *http.Request, m FileData) (err e
},
Label: Label{
Title: cases.Title(language.English).String(m.Name),
Date: fmt.Sprintf("%d", m.Date.Year()),
Size: fmt.Sprintf("%s x %s cm", widthCm, heightCm),
Technique: m.Metadata["Technique"],
Title: cases.Title(language.English).String(m.Name),
TitleJpn: titleKatakana,
TitleJpnIsKatakana: kana.IsKatakana(titleKatakana),
Date: fmt.Sprintf("%d", m.Date.Year()),
Size: fmt.Sprintf("%s x %s cm", widthCm, heightCm),
Technique: m.Metadata["Technique"],
},
}
err = tmpl.Execute(w, tmplData)

3
go.mod
View file

@ -1,9 +1,10 @@
module waifu_gallery
module waifu_gallery
go 1.22.2
require (
github.com/minio/minio-go/v7 v7.0.69
github.com/mochi-co/kana-tools v1.1.0
golang.org/x/text v0.14.0
)

8
go.sum
View file

@ -1,4 +1,5 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
@ -18,16 +19,21 @@ github.com/minio/minio-go/v7 v7.0.69 h1:l8AnsQFyY1xiwa/DaQskY4NXSLA2yrGsW5iD9nRP
github.com/minio/minio-go/v7 v7.0.69/go.mod h1:XAvOPJQ5Xlzk5o3o/ArO2NMbhSGkimC+bpW/ngRKDmQ=
github.com/minio/sha256-simd v1.0.1 h1:6kaan5IFmwTNynnKKpDHe6FWHohJOHhCPchzK49dzMM=
github.com/minio/sha256-simd v1.0.1/go.mod h1:Pz6AKMiUdngCLpeTL/RJY1M9rUuPMYujV5xJjtbRSN8=
github.com/mochi-co/kana-tools v1.1.0 h1:CrMLgiHuI/edj0XznHqlk0uYNCM53UmQfv97WKaUWMo=
github.com/mochi-co/kana-tools v1.1.0/go.mod h1:oGanjNn85gruPrIMLL6Z06cSm3VxJIP6yfpP5IuSPYw=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rs/xid v1.5.0 h1:mKX4bl4iPYJtEIxp6CYiUuLQ/8DYMoz0PUdtGgMFRVc=
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/crypto v0.19.0 h1:ENy+Az/9Y1vSrlrvBSyna3PITt4tiZLf7sgCjZBX7Wo=
golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU=
golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4=
@ -39,3 +45,5 @@ golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

10
http.go
View file

@ -70,12 +70,10 @@ func handleSubdomain(w http.ResponseWriter, r *http.Request) (input string, err
input = strings.ToLower(input)
// Check if prompt has banned words
for _, bannedWord := range config.BannedWords {
if strings.Contains(input, bannedWord) {
w.WriteHeader(http.StatusForbidden)
w.Write([]byte("This incident has been reported. Sick fuck."))
return "", fmt.Errorf("banned words in req '%s'", subdomain)
}
if config.state.bannedWordsRegex.MatchString(input) {
w.WriteHeader(http.StatusForbidden)
w.Write([]byte("This incident has been reported. Sick fuck."))
return "", fmt.Errorf("banned words in req '%s'", subdomain)
}
if len(input) <= 0 {