From babb3a50a1c7e6101980a9577364b169b5c6654c Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Thu, 9 Aug 2018 18:21:51 +0300 Subject: Fix attachments errors --- .../java/com/juick/server/CommandsManager.java | 5 ++-- .../java/com/juick/server/util/ImageUtils.java | 33 +++++++++++----------- 2 files changed, 19 insertions(+), 19 deletions(-) (limited to 'juick-common/src/main') diff --git a/juick-common/src/main/java/com/juick/server/CommandsManager.java b/juick-common/src/main/java/com/juick/server/CommandsManager.java index d3b85bef..5c034ce3 100644 --- a/juick-common/src/main/java/com/juick/server/CommandsManager.java +++ b/juick-common/src/main/java/com/juick/server/CommandsManager.java @@ -115,12 +115,13 @@ public class CommandsManager { attachmentType = attachmentFName.substring(attachmentFName.length() - 3); } int mid = messagesService.createMessage(user.getUid(), body, attachmentType, tags); - subscriptionService.subscribeMessage(messagesService.getMessage(mid), user); if (haveAttachment) { String fname = String.format("%d.%s", mid, attachmentType); imagesService.saveImageWithPreviews(attachmentFName, fname); } - com.juick.Message msg = messagesService.getMessage(mid); + Message msg = messagesService.getMessage(mid); + subscriptionService.subscribeMessage(msg, user); + applicationEventPublisher.publishEvent(new MessageReadEvent(this, user, msg)); applicationEventPublisher.publishEvent(new MessageEvent(this, msg, subscriptionService.getSubscribedUsers(msg.getUser().getUid(), msg.getMid()))); return CommandResult.build(msg, "New message posted.\n#" + msg.getMid() + " https://juick.com/m/" + msg.getMid(), String.format("[New message](%s) posted", PlainTextFormatter.formatUrl(msg))); diff --git a/juick-common/src/main/java/com/juick/server/util/ImageUtils.java b/juick-common/src/main/java/com/juick/server/util/ImageUtils.java index d6a455a4..3e03314b 100644 --- a/juick-common/src/main/java/com/juick/server/util/ImageUtils.java +++ b/juick-common/src/main/java/com/juick/server/util/ImageUtils.java @@ -149,25 +149,24 @@ public class ImageUtils { } public Attachment getAttachment(File imgFile) throws IOException { Attachment attachment = new Attachment(); - int pos = imgFile.getName().lastIndexOf("."); - if (pos == -1) - throw new IOException("No extension for file: " + imgFile.getAbsolutePath()); - String suffix = imgFile.getName().substring(pos + 1); - Iterator iter = ImageIO.getImageReadersBySuffix(suffix); - while(iter.hasNext()) { - ImageReader reader = iter.next(); - try (ImageInputStream stream = ImageIO.createImageInputStream(imgFile)) { - 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(); + 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(); + } } } - throw new IOException("Not a known image file: " + imgFile.getAbsolutePath()); + logger.warn("Not a known image file {}", imgFile.getAbsolutePath()); + return attachment; } } \ No newline at end of file -- cgit v1.2.3