Skip to content

Commit 9f7dcfa

Browse files
committed
to fix ImageIO.read calls
1 parent 81dc3b1 commit 9f7dcfa

File tree

1 file changed

+43
-17
lines changed

1 file changed

+43
-17
lines changed

common/src/main/scala/app/softnetwork/utils/ImageTools.scala

Lines changed: 43 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -78,21 +78,34 @@ object ImageTools extends StrictLogging {
7878
s"""Trying to resize image $originalPath to
7979
|${sizes.map(s => s"${s.width}x${s.height}").mkString(",")}""".stripMargin
8080
)
81-
Try(ImageIO.read(Files.newInputStream(originalPath))) match {
82-
case Success(src) =>
83-
val originalWidth = src.getWidth
84-
val originalHeight = src.getHeight
85-
for (size <- sizes) {
86-
resizeImage(
87-
src,
88-
originalWidth,
89-
originalHeight,
90-
originalPath,
91-
format,
92-
size
93-
)
81+
Try(
82+
Option(
83+
ImageIO.read(Files.newInputStream(originalPath))
84+
)
85+
) match {
86+
case Success(value) =>
87+
value match {
88+
case Some(src) =>
89+
val originalWidth = src.getWidth
90+
val originalHeight = src.getHeight
91+
for (size <- sizes) {
92+
resizeImage(
93+
src,
94+
originalWidth,
95+
originalHeight,
96+
originalPath,
97+
format,
98+
size
99+
)
100+
}
101+
true
102+
case _ =>
103+
logger.error(
104+
s"""Unable to read image $originalPath while trying to resize it to
105+
|${sizes.map(s => s"${s.width}x${s.height}").mkString(",")}""".stripMargin
106+
)
107+
false
94108
}
95-
true
96109
case Failure(f) =>
97110
logger.error(
98111
s"""an error occurred while trying to resize image $originalPath to
@@ -126,9 +139,22 @@ object ImageTools extends StrictLogging {
126139
if (
127140
!Files.exists(out) || originalPath.toFile.lastModified() > out.toFile.lastModified()
128141
) {
129-
Try(ImageIO.read(Files.newInputStream(originalPath))) match {
130-
case Success(src) =>
131-
resizeImage(src, src.getWidth, src.getHeight, originalPath, format, s)
142+
Try(
143+
Option(
144+
ImageIO.read(Files.newInputStream(originalPath))
145+
)
146+
) match {
147+
case Success(value) =>
148+
value match {
149+
case Some(src) =>
150+
resizeImage(src, src.getWidth, src.getHeight, originalPath, format, s)
151+
case _ =>
152+
logger.error(
153+
s"""Unable to read image $originalPath while trying to resize it to
154+
|s"${s.width}x${s.height}""".stripMargin
155+
)
156+
originalPath
157+
}
132158
case Failure(f) =>
133159
logger.error(
134160
s"""an error occurred while trying to resize image $originalPath to

0 commit comments

Comments
 (0)