catch panics on nsfw image detection.

This commit is contained in:
fiatjaf
2024-06-19 22:02:25 -03:00
parent 47423ddcc4
commit 037e55b230
2 changed files with 8 additions and 0 deletions

View File

@@ -27,6 +27,13 @@ var nsfwPredictor = func() *nsfw.Predictor {
var tempFileLocks = [3]sync.Mutex{{}, {}, {}} var tempFileLocks = [3]sync.Mutex{{}, {}, {}}
func isImageNSFW(url string) bool { 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 { if is, ok := nsfwCache.Get(url); ok {
return is return is
} }

View File

@@ -118,6 +118,7 @@ func renderEvent(w http.ResponseWriter, r *http.Request) {
return return
} }
for _, url := range allUrls { for _, url := range allUrls {
url = strings.Split(strings.Split(url, "?")[0], "#")[0]
if imageExtensionMatcher.MatchString(url) { if imageExtensionMatcher.MatchString(url) {
if isImageNSFW(url) { if isImageNSFW(url) {
log.Warn().Str("url", url).Str("event", data.nevent).Msg("detect nsfw image") log.Warn().Str("url", url).Str("event", data.nevent).Msg("detect nsfw image")