Convert objects to classes
Update references Signed-off-by: Rahul Rudragoudar <rr83019@gmail.com>
This commit is contained in:
parent
6f38a77c2f
commit
ecff4087ad
|
@ -6,24 +6,24 @@ import lc.core.{Captcha, Config}
|
|||
import lc.core.{Parameters, Size}
|
||||
import lc.misc.HelperFunctions
|
||||
|
||||
class BackgroundTask(throttle: Int, timeLimit: Int) {
|
||||
class BackgroundTask(config: Config, captcha: Captcha) {
|
||||
|
||||
private val task = new Runnable {
|
||||
def run(): Unit = {
|
||||
try {
|
||||
val mapIdGCPstmt = Statements.tlStmts.get.mapIdGCPstmt
|
||||
mapIdGCPstmt.setInt(1, timeLimit)
|
||||
mapIdGCPstmt.setInt(1, config.captchaExpiryTimeLimit)
|
||||
mapIdGCPstmt.executeUpdate()
|
||||
|
||||
val challengeGCPstmt = Statements.tlStmts.get.challengeGCPstmt
|
||||
challengeGCPstmt.executeUpdate()
|
||||
|
||||
val imageNum = Statements.tlStmts.get.getCountChallengeTable.executeQuery()
|
||||
var throttleIn = (throttle * 1.1).toInt
|
||||
var throttleIn = (config.throttle * 1.1).toInt
|
||||
if (imageNum.next())
|
||||
throttleIn = (throttleIn - imageNum.getInt("total"))
|
||||
while (0 < throttleIn) {
|
||||
Captcha.generateChallenge(getRandomParam())
|
||||
captcha.generateChallenge(getRandomParam())
|
||||
throttleIn -= 1
|
||||
}
|
||||
} catch { case exception: Exception => println(exception) }
|
||||
|
@ -31,7 +31,7 @@ class BackgroundTask(throttle: Int, timeLimit: Int) {
|
|||
}
|
||||
|
||||
private def getRandomParam(): Parameters = {
|
||||
val captcha = pickRandom(Config.captchaConfig)
|
||||
val captcha = pickRandom(config.captchaConfig)
|
||||
val level = pickRandom(captcha.allowedLevels)
|
||||
val media = pickRandom(captcha.allowedMedia)
|
||||
val inputType = pickRandom(captcha.allowedInputType)
|
||||
|
|
|
@ -9,7 +9,7 @@ import lc.captchas.interfaces.ChallengeProvider
|
|||
import lc.captchas.interfaces.Challenge
|
||||
import java.sql.Blob
|
||||
|
||||
object Captcha {
|
||||
class Captcha(config: Config, captchaProviders: CaptchaProviders) {
|
||||
|
||||
def getCaptcha(id: Id): Either[Error, Image] = {
|
||||
val blob = getImage(id.id)
|
||||
|
@ -37,7 +37,7 @@ object Captcha {
|
|||
}
|
||||
|
||||
def generateChallenge(param: Parameters): Option[Int] = {
|
||||
val provider = CaptchaProviders.getProvider(param)
|
||||
val provider = captchaProviders.getProvider(param)
|
||||
provider match {
|
||||
case Some(value) => {
|
||||
val providerId = value.getId()
|
||||
|
@ -75,9 +75,9 @@ object Captcha {
|
|||
}
|
||||
}
|
||||
|
||||
val allowedInputType = Config.allowedInputType
|
||||
val allowedLevels = Config.allowedLevels
|
||||
val allowedMedia = Config.allowedMedia
|
||||
val allowedInputType = config.allowedInputType
|
||||
val allowedLevels = config.allowedLevels
|
||||
val allowedMedia = config.allowedMedia
|
||||
|
||||
private def validateParam(param: Parameters): Array[String] = {
|
||||
var invalid_params = Array[String]()
|
||||
|
@ -142,7 +142,7 @@ object Captcha {
|
|||
case None => Right(Success(ResultEnum.EXPIRED.toString))
|
||||
case Some(value) => {
|
||||
val (provider, secret) = value
|
||||
val check = CaptchaProviders.getProviderById(provider).checkAnswer(secret, answer.answer)
|
||||
val check = captchaProviders.getProviderById(provider).checkAnswer(secret, answer.answer)
|
||||
deleteCaptcha(answer.id)
|
||||
val result = if (check) ResultEnum.TRUE.toString else ResultEnum.FALSE.toString
|
||||
Right(Success(result))
|
||||
|
@ -152,7 +152,7 @@ object Captcha {
|
|||
|
||||
private def getSecret(id: String): Option[(String, String)] = {
|
||||
val selectPstmt = Statements.tlStmts.get.selectPstmt
|
||||
selectPstmt.setInt(1, Config.captchaExpiryTimeLimit)
|
||||
selectPstmt.setInt(1, config.captchaExpiryTimeLimit)
|
||||
selectPstmt.setString(2, id)
|
||||
val rs: ResultSet = selectPstmt.executeQuery()
|
||||
if (rs.first()) {
|
||||
|
|
|
@ -6,7 +6,7 @@ import lc.captchas.interfaces.Challenge
|
|||
import scala.collection.mutable.Map
|
||||
import lc.misc.HelperFunctions
|
||||
|
||||
object CaptchaProviders {
|
||||
class CaptchaProviders(config: Config) {
|
||||
private val providers = Map(
|
||||
"FilterChallenge" -> new FilterChallenge,
|
||||
//"FontFunCaptcha" -> new FontFunCaptcha,
|
||||
|
@ -24,7 +24,7 @@ object CaptchaProviders {
|
|||
}
|
||||
}
|
||||
|
||||
private val config = Config.captchaConfig
|
||||
private val captchaConfig = config.captchaConfig
|
||||
|
||||
def getProviderById(id: String): ChallengeProvider = {
|
||||
return providers(id)
|
||||
|
@ -32,7 +32,7 @@ object CaptchaProviders {
|
|||
|
||||
private def filterProviderByParam(param: Parameters): Iterable[(String, String)] = {
|
||||
val configFilter = for {
|
||||
configValue <- config
|
||||
configValue <- captchaConfig
|
||||
if configValue.allowedLevels.contains(param.level)
|
||||
if configValue.allowedMedia.contains(param.media)
|
||||
if configValue.allowedInputType.contains(param.input_type)
|
||||
|
|
|
@ -10,7 +10,7 @@ import org.limium.picoserve.Server.ByteResponse
|
|||
import scala.io.Source
|
||||
import org.limium.picoserve.Server.StringResponse
|
||||
|
||||
class Server(port: Int) {
|
||||
class Server(port: Int, captcha: Captcha) {
|
||||
val server: picoserve.Server = picoserve.Server
|
||||
.builder()
|
||||
.port(port)
|
||||
|
@ -20,7 +20,7 @@ class Server(port: Int) {
|
|||
(request) => {
|
||||
val json = parse(request.getBodyString())
|
||||
val param = json.extract[Parameters]
|
||||
val id = Captcha.getChallenge(param)
|
||||
val id = captcha.getChallenge(param)
|
||||
getResponse(id)
|
||||
}
|
||||
)
|
||||
|
@ -31,7 +31,7 @@ class Server(port: Int) {
|
|||
val result = if (params.containsKey("id")) {
|
||||
val paramId = params.get("id").get(0)
|
||||
val id = Id(paramId)
|
||||
Captcha.getCaptcha(id)
|
||||
captcha.getCaptcha(id)
|
||||
} else {
|
||||
Left(Error(ErrorMessageEnum.INVALID_PARAM.toString + "=> id"))
|
||||
}
|
||||
|
@ -43,7 +43,7 @@ class Server(port: Int) {
|
|||
(request) => {
|
||||
val json = parse(request.getBodyString())
|
||||
val answer = json.extract[Answer]
|
||||
val result = Captcha.checkAnswer(answer)
|
||||
val result = captcha.checkAnswer(answer)
|
||||
getResponse(result)
|
||||
}
|
||||
)
|
||||
|
|
Loading…
Reference in New Issue