Skip to content

Commit df59cda

Browse files
committed
Add exceptions to the convertHexToImage function to check that the argument is as expected (Hexadecimal String).
1 parent d645f56 commit df59cda

File tree

1 file changed

+9
-8
lines changed
  • spra-web/src/main/scala/net/wiringbits/spra/ui/web/utils

1 file changed

+9
-8
lines changed

spra-web/src/main/scala/net/wiringbits/spra/ui/web/utils/Images.scala

+9-8
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,17 @@ object Images {
2424
}
2525

2626
def convertHexToImage(imageHex: String): String = {
27-
// Remove the "0x" prefix from the hex string, as it's not part of the actual image data
27+
// Check if the argument is a hexadecimal string"
28+
if (!imageHex.startsWith("\\x") || (imageHex.length % 2) == 1) {
29+
throw new IllegalArgumentException(s"Error: Expected a hexadecimal string but found: $imageHex")
30+
}
31+
// Remove the "\x" prefix from the hex string, as it's not part of the actual image data
2832
val hex = imageHex.tail.tail
2933
val imageBinary: Array[Byte] =
30-
if ((hex.length % 2) == 1)
31-
Array.empty
32-
else
33-
Try(hex.grouped(2).map { hex => Integer.parseInt(hex, 16).toByte }.toArray) match {
34-
case Success(value) => value
35-
case Failure(_) => Array.empty
36-
}
34+
Try(hex.grouped(2).map { hex => Integer.parseInt(hex, 16).toByte }.toArray) match {
35+
case Success(value) => value
36+
case Failure(_) => Array.empty
37+
}
3738
val byteArray = Uint8Array(js.Array(imageBinary.map(_.toShort): _*))
3839
dom.URL.createObjectURL(dom.Blob(js.Array(byteArray.buffer)))
3940
}

0 commit comments

Comments
 (0)