diff options
author | Vitaly Takmazov | 2020-10-08 17:02:39 +0300 |
---|---|---|
committer | Vitaly Takmazov | 2020-10-08 17:02:39 +0300 |
commit | dd31da6fc5482e9a2bb386d252c54c7a22cb0681 (patch) | |
tree | c43d0e9d040fceab55980e6f6c3bdd634fec7752 /src/main | |
parent | 21439c3e0b7c7396ad859149bcbf2197ac6334e1 (diff) |
Images: catch exception when image file does not exists
Diffstat (limited to 'src/main')
-rw-r--r-- | src/main/java/com/juick/util/ImageUtils.java | 31 |
1 files changed, 18 insertions, 13 deletions
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<ImageReader> 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<ImageReader> 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()); |