From cbe26a60972a7b681dc1d0f94fdd496d9b963661 Mon Sep 17 00:00:00 2001 From: Scala Steward Date: Wed, 15 Dec 2021 22:54:32 +0100 Subject: [PATCH 1/2] Update scalafmt-core to 3.0.8 --- .scalafmt.conf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.scalafmt.conf b/.scalafmt.conf index 88c98ad..8a6242f 100644 --- a/.scalafmt.conf +++ b/.scalafmt.conf @@ -1,3 +1,3 @@ -version="3.0.5" +version="3.0.8" maxColumn = 120 runner.dialect = scala213source3 From f463b487eb0def2b97618c4df8d0e51626943ee7 Mon Sep 17 00:00:00 2001 From: Scala Steward Date: Wed, 15 Dec 2021 22:54:41 +0100 Subject: [PATCH 2/2] Reformat with scalafmt 3.0.8 --- build.sbt | 2 +- src/main/scala/lc/Main.scala | 25 +++++++++++-------- src/main/scala/lc/captchas/DebugCaptcha.scala | 13 +++++++--- .../scala/lc/captchas/FilterChallenge.scala | 9 ++++--- src/main/scala/lc/captchas/LabelCaptcha.scala | 9 ++++--- .../scala/lc/captchas/RainDropsCaptcha.scala | 9 ++++--- src/main/scala/lc/core/captchaProviders.scala | 9 +++---- src/main/scala/lc/core/config.scala | 8 +++--- src/main/scala/lc/server/Server.scala | 9 ++++--- 9 files changed, 57 insertions(+), 36 deletions(-) diff --git a/build.sbt b/build.sbt index 350eaaa..514999c 100644 --- a/build.sbt +++ b/build.sbt @@ -27,6 +27,6 @@ compileOrder := CompileOrder.JavaThenScala javafmtOnCompile := false assembly / mainClass := Some("lc.LCFramework") Compile / run / mainClass := Some("lc.LCFramework") -assembly / assemblyJarName := "LibreCaptcha.jar" +assembly / assemblyJarName := "LibreCaptcha.jar" run / fork := true diff --git a/src/main/scala/lc/Main.scala b/src/main/scala/lc/Main.scala index 912769a..66ea2b7 100644 --- a/src/main/scala/lc/Main.scala +++ b/src/main/scala/lc/Main.scala @@ -6,7 +6,7 @@ import lc.background.BackgroundTask object LCFramework { def main(args: scala.Array[String]): Unit = { - val configFilePath = if (args.length > 0){ + val configFilePath = if (args.length > 0) { args(0) } else { "data/config.json" @@ -16,14 +16,20 @@ object LCFramework { val captcha = new Captcha(config = config, captchaProviders = captchaProviders) val backgroundTask = new BackgroundTask(config = config, captcha = captcha) backgroundTask.beginThread(delay = config.threadDelay) - val server = new Server(address = config.address, port = config.port, captcha = captcha, playgroundEnabled = config.playgroundEnabled, corsHeader = config.corsHeader) + val server = new Server( + address = config.address, + port = config.port, + captcha = captcha, + playgroundEnabled = config.playgroundEnabled, + corsHeader = config.corsHeader + ) server.start() } } object MakeSamples { def main(args: scala.Array[String]): Unit = { - val configFilePath = if (args.length > 0){ + val configFilePath = if (args.length > 0) { args(0) } else { "data/config.json" @@ -31,14 +37,13 @@ object MakeSamples { val config = new Config(configFilePath) val captchaProviders = new CaptchaProviders(config = config) val samples = captchaProviders.generateChallengeSamples() - samples.foreach { - case (key, sample) => - val extensionMap = Map("image/png" -> "png", "image/gif" -> "gif") - println(key + ": " + sample) + samples.foreach { case (key, sample) => + val extensionMap = Map("image/png" -> "png", "image/gif" -> "gif") + println(key + ": " + sample) - val outStream = new java.io.FileOutputStream("samples/" + key + "." + extensionMap(sample.contentType)) - outStream.write(sample.content) - outStream.close + val outStream = new java.io.FileOutputStream("samples/" + key + "." + extensionMap(sample.contentType)) + outStream.write(sample.content) + outStream.close } } } diff --git a/src/main/scala/lc/captchas/DebugCaptcha.scala b/src/main/scala/lc/captchas/DebugCaptcha.scala index f0add52..b0ca809 100644 --- a/src/main/scala/lc/captchas/DebugCaptcha.scala +++ b/src/main/scala/lc/captchas/DebugCaptcha.scala @@ -13,7 +13,9 @@ import lc.captchas.interfaces.Challenge import lc.captchas.interfaces.ChallengeProvider import lc.misc.PngImageWriter -/** This captcha is only for debugging purposes. It creates very simple captchas that are deliberately easy to solve with OCR engines */ +/** This captcha is only for debugging purposes. It creates very simple captchas that are deliberately easy to solve + * with OCR engines + */ class DebugCaptcha extends ChallengeProvider { def getId(): String = { @@ -26,9 +28,12 @@ class DebugCaptcha extends ChallengeProvider { def supportedParameters(): Map[String, List[String]] = { Map.of( - "supportedLevels", List.of("debug"), - "supportedMedia", List.of("image/png"), - "supportedInputType", List.of("text") + "supportedLevels", + List.of("debug"), + "supportedMedia", + List.of("image/png"), + "supportedInputType", + List.of("text") ) } diff --git a/src/main/scala/lc/captchas/FilterChallenge.scala b/src/main/scala/lc/captchas/FilterChallenge.scala index c7f2b70..5f83ff6 100644 --- a/src/main/scala/lc/captchas/FilterChallenge.scala +++ b/src/main/scala/lc/captchas/FilterChallenge.scala @@ -20,9 +20,12 @@ class FilterChallenge extends ChallengeProvider { def supportedParameters(): JavaMap[String, JavaList[String]] = { JavaMap.of( - "supportedLevels",JavaList.of("medium", "hard"), - "supportedMedia", JavaList.of("image/png"), - "supportedInputType", JavaList.of("text") + "supportedLevels", + JavaList.of("medium", "hard"), + "supportedMedia", + JavaList.of("image/png"), + "supportedInputType", + JavaList.of("text") ) } diff --git a/src/main/scala/lc/captchas/LabelCaptcha.scala b/src/main/scala/lc/captchas/LabelCaptcha.scala index 18048f8..5173346 100644 --- a/src/main/scala/lc/captchas/LabelCaptcha.scala +++ b/src/main/scala/lc/captchas/LabelCaptcha.scala @@ -31,9 +31,12 @@ class LabelCaptcha extends ChallengeProvider { def supportedParameters(): JavaMap[String, JavaList[String]] = { JavaMap.of( - "supportedLevels", JavaList.of("hard"), - "supportedMedia", JavaList.of("image/png"), - "supportedInputType", JavaList.of("text") + "supportedLevels", + JavaList.of("hard"), + "supportedMedia", + JavaList.of("image/png"), + "supportedInputType", + JavaList.of("text") ) } diff --git a/src/main/scala/lc/captchas/RainDropsCaptcha.scala b/src/main/scala/lc/captchas/RainDropsCaptcha.scala index 88581fd..9e8f6f3 100644 --- a/src/main/scala/lc/captchas/RainDropsCaptcha.scala +++ b/src/main/scala/lc/captchas/RainDropsCaptcha.scala @@ -38,9 +38,12 @@ class RainDropsCP extends ChallengeProvider { def supportedParameters(): JavaMap[String, JavaList[String]] = { JavaMap.of( - "supportedLevels", JavaList.of("medium", "easy"), - "supportedMedia", JavaList.of("image/gif"), - "supportedInputType", JavaList.of("text") + "supportedLevels", + JavaList.of("medium", "easy"), + "supportedMedia", + JavaList.of("image/gif"), + "supportedInputType", + JavaList.of("text") ) } diff --git a/src/main/scala/lc/core/captchaProviders.scala b/src/main/scala/lc/core/captchaProviders.scala index 65267db..96ad212 100644 --- a/src/main/scala/lc/core/captchaProviders.scala +++ b/src/main/scala/lc/core/captchaProviders.scala @@ -9,18 +9,17 @@ import lc.misc.HelperFunctions class CaptchaProviders(config: Config) { private val providers = Map( "FilterChallenge" -> new FilterChallenge, - //"FontFunCaptcha" -> new FontFunCaptcha, + // "FontFunCaptcha" -> new FontFunCaptcha, "PoppingCharactersCaptcha" -> new PoppingCharactersCaptcha, "ShadowTextCaptcha" -> new ShadowTextCaptcha, "RainDropsCaptcha" -> new RainDropsCP, "DebugCaptcha" -> new DebugCaptcha - //"LabelCaptcha" -> new LabelCaptcha + // "LabelCaptcha" -> new LabelCaptcha ) def generateChallengeSamples(): Map[String, Challenge] = { - providers.map { - case (key, provider) => - (key, provider.returnChallenge()) + providers.map { case (key, provider) => + (key, provider.returnChallenge()) } } diff --git a/src/main/scala/lc/core/config.scala b/src/main/scala/lc/core/config.scala index 42aaf19..cc63936 100644 --- a/src/main/scala/lc/core/config.scala +++ b/src/main/scala/lc/core/config.scala @@ -21,7 +21,7 @@ class Config(configFilePath: String) { } catch { case _: FileNotFoundException => { val configFileContent = getDefaultConfig() - val file = if(new File(configFilePath).isDirectory){ + val file = if (new File(configFilePath).isDirectory) { new File(configFilePath.concat("/config.json")) } else { new File(configFilePath) @@ -49,8 +49,8 @@ class Config(configFilePath: String) { val corsHeader: String = (configJson \ AttributesEnum.CORS_HEADER.toString).extract[String] private val captchaConfigJson = (configJson \ "captchas") - val captchaConfigTransform: JValue = captchaConfigJson transformField { - case JField("config", JObject(config)) => ("config", JString(config.toString)) + val captchaConfigTransform: JValue = captchaConfigJson transformField { case JField("config", JObject(config)) => + ("config", JString(config.toString)) } val captchaConfig: List[CaptchaConfig] = captchaConfigTransform.extract[List[CaptchaConfig]] val allowedLevels: Set[String] = captchaConfig.flatMap(_.allowedLevels).toSet @@ -105,6 +105,6 @@ class Config(configFilePath: String) { } -object Config{ +object Config { implicit val formats: DefaultFormats.type = DefaultFormats } diff --git a/src/main/scala/lc/server/Server.scala b/src/main/scala/lc/server/Server.scala index afecd40..5134373 100644 --- a/src/main/scala/lc/server/Server.scala +++ b/src/main/scala/lc/server/Server.scala @@ -14,7 +14,7 @@ import scala.jdk.CollectionConverters._ class Server(address: String, port: Int, captcha: Captcha, playgroundEnabled: Boolean, corsHeader: String) { var headerMap: util.Map[String, util.List[String]] = _ - if( corsHeader.nonEmpty ) { + if (corsHeader.nonEmpty) { headerMap = Map("Access-Control-Allow-Origin" -> List(corsHeader).asJava).asJava } val serverBuilder: ServerBuilder = picoserve.Server @@ -53,7 +53,7 @@ class Server(address: String, port: Int, captcha: Captcha, playgroundEnabled: Bo getResponse(result, headerMap) } ) - if( playgroundEnabled ) { + if (playgroundEnabled) { serverBuilder.GET( "/demo/index.html", (_) => { @@ -66,7 +66,10 @@ class Server(address: String, port: Int, captcha: Captcha, playgroundEnabled: Bo val server: picoserve.Server = serverBuilder.build() - private def getResponse(response: Either[Error, ByteConvert], responseHeaders: util.Map[String, util.List[String]]): ByteResponse = { + private def getResponse( + response: Either[Error, ByteConvert], + responseHeaders: util.Map[String, util.List[String]] + ): ByteResponse = { response match { case Right(value) => { new ByteResponse(200, value.toBytes(), responseHeaders)