mirror of
https://github.com/aljazceru/njump.git
synced 2026-02-01 04:04:41 +01:00
remove nsfw AI detector thing.
This commit is contained in:
2
justfile
2
justfile
@@ -7,7 +7,7 @@ build: templ tailwind
|
||||
go build -o ./njump
|
||||
|
||||
deploy: templ tailwind
|
||||
GOOS=linux GOARCH=amd64 go build -tags=nsfw -ldflags="-X main.compileTimeTs=$(date '+%s')" -o ./njump
|
||||
GOOS=linux GOARCH=amd64 go build -ldflags="-X main.compileTimeTs=$(date '+%s')" -o ./njump
|
||||
rsync --progress njump njump:njump/njump-new
|
||||
ssh njump 'systemctl stop njump'
|
||||
ssh njump 'mv njump/njump-new njump/njump'
|
||||
|
||||
@@ -1,80 +0,0 @@
|
||||
//go:build nsfw
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"image/png"
|
||||
"os"
|
||||
"sync"
|
||||
|
||||
"github.com/ccuetoh/nsfw"
|
||||
lru "github.com/hashicorp/golang-lru/v2"
|
||||
)
|
||||
|
||||
var nsfwCache, _ = lru.New[string, bool](64)
|
||||
|
||||
var nsfwPredictor = func() *nsfw.Predictor {
|
||||
p, err := nsfw.NewLatestPredictor()
|
||||
if err != nil {
|
||||
log.Fatal().Err(err).Msg("failed to start keras nsfw detector")
|
||||
return p
|
||||
}
|
||||
log.Info().Msg("keras nsfw detector enabled")
|
||||
return p
|
||||
}()
|
||||
|
||||
var tempFileLocks = [3]sync.Mutex{{}, {}, {}}
|
||||
|
||||
func isImageNSFW(url string) bool {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
log.Error().Interface("err", r).Str("url", url).Msg("panic while checking nsfw")
|
||||
return
|
||||
}
|
||||
}()
|
||||
|
||||
if is, ok := nsfwCache.Get(url); ok {
|
||||
return is
|
||||
}
|
||||
|
||||
img, err := fetchImageFromURL(url)
|
||||
if err != nil {
|
||||
return false // if we can't read it that means it's ok
|
||||
}
|
||||
|
||||
// grab mutex
|
||||
var tempPath string
|
||||
for i, mu := range tempFileLocks {
|
||||
if ok := mu.TryLock(); ok {
|
||||
tempPath = fmt.Sprintf("/tmp/nsfw-detection-%d.png", i)
|
||||
defer mu.Unlock()
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if tempPath == "" {
|
||||
// apparently we can't allocate a temporary file for this, so let's warn and return false
|
||||
log.Warn().Msg("failed to allocate a temp file for nsfw detection")
|
||||
return false
|
||||
}
|
||||
|
||||
tempFile, err := os.Create(tempPath)
|
||||
if err != nil {
|
||||
log.Warn().Err(err).Msg("failed to open a temp file for nsfw detection")
|
||||
return false
|
||||
}
|
||||
if err := png.Encode(tempFile, img); err != nil {
|
||||
log.Warn().Err(err).Msg("failed to encode png for nsfw detection")
|
||||
tempFile.Close()
|
||||
return false
|
||||
}
|
||||
tempFile.Close() // close here so the thing can read it below
|
||||
|
||||
res := nsfwPredictor.Predict(nsfwPredictor.NewImage(tempPath, 3))
|
||||
log.Debug().Str("url", url).Str("desc", res.Describe()).Msg("image analyzed")
|
||||
|
||||
is := res.Porn > 0.85 || res.Hentai > 0.85
|
||||
nsfwCache.Add(url, is)
|
||||
return is
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
//go:build !nsfw
|
||||
|
||||
package main
|
||||
|
||||
func isImageNSFW(url string) bool {
|
||||
return false
|
||||
}
|
||||
@@ -111,22 +111,12 @@ func renderEvent(w http.ResponseWriter, r *http.Request) {
|
||||
//
|
||||
|
||||
// if it's porn we return a 404
|
||||
allUrls := urlRegex.FindAllString(data.event.Content, len(data.event.Content)+1)
|
||||
if len(allUrls) > 0 && hasProhibitedWordOrTag(data.event.Event) {
|
||||
hasURL := urlRegex.MatchString(data.event.Content)
|
||||
if hasURL && hasProhibitedWordOrTag(data.event.Event) {
|
||||
log.Warn().Str("event", data.nevent).Msg("detect prohibited porn content")
|
||||
http.Error(w, "event is not allowed", 404)
|
||||
return
|
||||
}
|
||||
for _, url := range allUrls {
|
||||
url = strings.Split(strings.Split(url, "?")[0], "#")[0]
|
||||
if imageExtensionMatcher.MatchString(url) {
|
||||
if isImageNSFW(url) {
|
||||
log.Warn().Str("url", url).Str("event", data.nevent).Msg("detect nsfw image")
|
||||
http.Error(w, "event is unsuitable for work", 404)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// gather page style from user-agent
|
||||
style := getPreviewStyle(r)
|
||||
|
||||
Reference in New Issue
Block a user