From dd31da6fc5482e9a2bb386d252c54c7a22cb0681 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Thu, 8 Oct 2020 17:02:39 +0300 Subject: Images: catch exception when image file does not exists --- src/main/java/com/juick/util/ImageUtils.java | 31 ++++++++++++++++------------ 1 file changed, 18 insertions(+), 13 deletions(-) (limited to 'src/main') diff --git a/src/main/java/com/juick/util/ImageUtils.java b/src/main/java/com/juick/util/ImageUtils.java index d2158cca..3549a89a 100644 --- a/src/main/java/com/juick/util/ImageUtils.java +++ b/src/main/java/com/juick/util/ImageUtils.java @@ -150,21 +150,26 @@ public class ImageUtils { } public Attachment getAttachment(File imgFile) throws IOException { Attachment attachment = new Attachment(); - try (ImageInputStream stream = ImageIO.createImageInputStream(imgFile)) { - Iterator iter = ImageIO.getImageReaders(stream); - while (iter.hasNext()) { - ImageReader reader = iter.next(); - try { - reader.setInput(stream); - attachment.setWidth(reader.getWidth(reader.getMinIndex())); - attachment.setHeight(reader.getHeight(reader.getMinIndex())); - return attachment; - } catch (Exception e) { - logger.debug("Error reading {}, trying next reader", imgFile.getAbsolutePath()); - } finally { - reader.dispose(); + if (imgFile.exists()) { + try (ImageInputStream stream = ImageIO.createImageInputStream(imgFile)) { + Iterator iter = ImageIO.getImageReaders(stream); + while (iter.hasNext()) { + ImageReader reader = iter.next(); + try { + reader.setInput(stream); + attachment.setWidth(reader.getWidth(reader.getMinIndex())); + attachment.setHeight(reader.getHeight(reader.getMinIndex())); + return attachment; + } catch (Exception e) { + logger.debug("Error reading {}, trying next reader", imgFile.getAbsolutePath()); + } finally { + reader.dispose(); + } } } + } else { + logger.warn("File not exists yet: {}", imgFile.getAbsolutePath()); + return attachment; } logger.warn("Not a known image file {}", imgFile.getAbsolutePath()); -- cgit v1.2.3