Skip to content

Commit

Permalink
feat: Add fallback analyzer
Browse files Browse the repository at this point in the history
  • Loading branch information
phinner committed May 26, 2024
1 parent 9a47ee0 commit 7e73851
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 9 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ tasks.shadowJar {

doFirst {
val temp = temporaryDir.resolve("plugin.json")
temp.writeText(metadata.toJson())
temp.writeText(ModMetadata.toJson(metadata))
from(temp)
}

Expand Down
2 changes: 2 additions & 0 deletions src/main/kotlin/com/xpdustry/nohorny/NoHornyConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,7 @@ internal data class NoHornyConfig(
}

data class ModerateContent(val moderateContentToken: Secret) : Analyzer

data class Fallback(val primary: Analyzer, val secondary: Analyzer) : Analyzer
}
}
20 changes: 12 additions & 8 deletions src/main/kotlin/com/xpdustry/nohorny/NoHornyPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import com.sksamuel.hoplite.ConfigException
import com.sksamuel.hoplite.ConfigLoaderBuilder
import com.sksamuel.hoplite.addPathSource
import com.xpdustry.nohorny.analyzer.DebugImageAnalyzer
import com.xpdustry.nohorny.analyzer.FallbackAnalyzer
import com.xpdustry.nohorny.analyzer.ImageAnalyzer
import com.xpdustry.nohorny.analyzer.ModerateContentAnalyzer
import com.xpdustry.nohorny.analyzer.SightEngineAnalyzer
Expand Down Expand Up @@ -128,14 +129,7 @@ public class NoHornyPlugin : Plugin(), NoHornyAPI {
}

val config = loader.loadConfigOrThrow<NoHornyConfig>()
val analyzer =
when (config.analyzer) {
is NoHornyConfig.Analyzer.None -> ImageAnalyzer.None
is NoHornyConfig.Analyzer.Debug -> DebugImageAnalyzer(directory.resolve("debug"))
is NoHornyConfig.Analyzer.ModerateContent ->
ModerateContentAnalyzer(config.analyzer, http)
is NoHornyConfig.Analyzer.SightEngine -> SightEngineAnalyzer(config.analyzer, http)
}
val analyzer = createAnalyzer(config.analyzer)

this.config = config
this.analyzer = analyzer
Expand All @@ -146,6 +140,16 @@ public class NoHornyPlugin : Plugin(), NoHornyAPI {
NoHornyLogger.debug("Set cache to $cache")
}

private fun createAnalyzer(config: NoHornyConfig.Analyzer): ImageAnalyzer =
when (config) {
is NoHornyConfig.Analyzer.None -> ImageAnalyzer.None
is NoHornyConfig.Analyzer.Debug -> DebugImageAnalyzer(directory.resolve("debug"))
is NoHornyConfig.Analyzer.ModerateContent -> ModerateContentAnalyzer(config, http)
is NoHornyConfig.Analyzer.SightEngine -> SightEngineAnalyzer(config, http)
is NoHornyConfig.Analyzer.Fallback ->
FallbackAnalyzer(createAnalyzer(config.primary), createAnalyzer(config.secondary))
}

private object NoHornyThreadFactory : ThreadFactory {
private val count = AtomicInteger(0)

Expand Down
39 changes: 39 additions & 0 deletions src/main/kotlin/com/xpdustry/nohorny/analyzer/FallbackAnalyzer.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* This file is part of NoHorny. The plugin securing your server against nsfw.
*
* MIT License
*
* Copyright (c) 2023 Xpdustry
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.xpdustry.nohorny.analyzer

import com.xpdustry.nohorny.NoHornyLogger
import java.awt.image.BufferedImage
import java.util.concurrent.CompletableFuture

internal class FallbackAnalyzer(val primary: ImageAnalyzer, val secondary: ImageAnalyzer) :
ImageAnalyzer {
override fun analyse(image: BufferedImage): CompletableFuture<ImageAnalyzer.Result> =
primary.analyse(image).exceptionallyCompose { throwable ->
NoHornyLogger.debug("Primary analyzer failed, switching to secondary", throwable)
secondary.analyse(image)
}
}

0 comments on commit 7e73851

Please sign in to comment.