forked from librecaptcha/lc-core
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmodels.scala
More file actions
138 lines (127 loc) · 4.18 KB
/
Copy pathmodels.scala
File metadata and controls
138 lines (127 loc) · 4.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package lc.core
import zio.blocks.schema._
import zio.blocks.schema.json._
import zio.blocks.schema.codec.BinaryCodec
import java.nio.ByteBuffer
trait ByteConvert { def toBytes(): Array[Byte] }
case class Size(height: Int, width: Int)
case class Parameters(level: String, media: String, input_type: String, size: String)
object Parameters {
implicit val schema: Schema[Parameters] = Schema.derived
implicit val codec: BinaryCodec[Parameters] = schema.derive(JsonFormat.deriver)
}
object BufferEncoder {
def encode[A](value: A, codec: BinaryCodec[A]): Array[Byte] = {
// Start with 1KB, if it fails, try with 10KB, 100KB, etc up to 1MB
var size = 1024
var result: Array[Byte] = null
while (result == null && size <= 1048576) {
try {
val buf = ByteBuffer.allocate(size)
codec.encode(value, buf)
buf.flip()
result = new Array[Byte](buf.remaining())
buf.get(result)
} catch {
case _: java.nio.BufferOverflowException =>
size *= 10
}
}
if (result == null) {
throw new Exception("Buffer overflow encoding object")
}
result
}
}
case class Id(id: String) extends ByteConvert {
def toBytes(): Array[Byte] = {
BufferEncoder.encode(this, Id.codec)
}
}
object Id {
implicit val schema: Schema[Id] = Schema.derived
implicit val codec: BinaryCodec[Id] = schema.derive(JsonFormat.deriver)
}
case class Image(image: Array[Byte]) extends ByteConvert { def toBytes(): Array[Byte] = { image } }
case class Answer(answer: String, id: String)
object Answer {
implicit val schema: Schema[Answer] = Schema.derived
implicit val codec: BinaryCodec[Answer] = schema.derive(JsonFormat.deriver)
}
case class Success(result: String) extends ByteConvert {
def toBytes(): Array[Byte] = {
BufferEncoder.encode(this, Success.codec)
}
}
object Success {
implicit val schema: Schema[Success] = Schema.derived
implicit val codec: BinaryCodec[Success] = schema.derive(JsonFormat.deriver)
}
case class Error(message: String) extends ByteConvert {
def toBytes(): Array[Byte] = {
BufferEncoder.encode(this, Error.codec)
}
}
object Error {
implicit val schema: Schema[Error] = Schema.derived
implicit val codec: BinaryCodec[Error] = schema.derive(JsonFormat.deriver)
}
case class CaptchaConfig(
name: String,
allowedLevels: List[String],
allowedMedia: List[String],
allowedInputType: List[String],
allowedSizes: List[String],
config: zio.blocks.schema.json.Json.Object
)
case class AppConfig(
port: Option[Int] = None,
address: Option[String] = None,
bufferCount: Option[Int] = None,
seed: Option[Int] = None,
captchaExpiryTimeLimit: Option[Int] = None,
threadDelay: Option[Int] = None,
playgroundEnabled: Option[Boolean] = None,
corsHeader: Option[String] = None,
maxAttemptsRatio: Option[Float] = None,
authRequired: Option[Boolean] = None,
captchas: List[CaptchaConfig] = List.empty
) {
def toConfigField: ConfigField = ConfigField(
port,
address,
bufferCount,
seed,
captchaExpiryTimeLimit,
threadDelay,
playgroundEnabled,
corsHeader,
maxAttemptsRatio,
authRequired
)
}
object AppConfig {
implicit val schema: Schema[AppConfig] = Schema.derived
implicit val codec: BinaryCodec[AppConfig] = schema.derive(JsonFormat.deriver)
}
case class ConfigField(
port: Option[Int] = None,
address: Option[String] = None,
bufferCount: Option[Int] = None,
seed: Option[Int] = None,
captchaExpiryTimeLimit: Option[Int] = None,
threadDelay: Option[Int] = None,
playgroundEnabled: Option[Boolean] = None,
corsHeader: Option[String] = None,
maxAttemptsRatio: Option[Float] = None,
authRequired: Option[Boolean] = None
) {
lazy val portInt: Option[Int] = port
lazy val bufferCountInt: Option[Int] = bufferCount
lazy val seedInt: Option[Int] = seed
lazy val captchaExpiryTimeLimitInt: Option[Int] = captchaExpiryTimeLimit
lazy val threadDelayInt: Option[Int] = threadDelay
lazy val maxAttemptsRatioFloat: Option[Float] = maxAttemptsRatio
lazy val playgroundEnabledBool: Option[Boolean] = playgroundEnabled.map(_ || false)
lazy val authRequiredBool: Option[Boolean] = authRequired.map(_ || false)
}