aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--juick-common/src/main/java/com/juick/server/CommandsManager.java5
-rw-r--r--juick-common/src/main/java/com/juick/server/util/ImageUtils.java33
-rw-r--r--juick-server/src/main/java/com/juick/service/MessagesServiceImpl.java2
-rw-r--r--juick-server/src/test/java/com/juick/server/tests/ServerTests.java21
-rw-r--r--juick-server/src/test/resources/2915104.jpgbin0 -> 227253 bytes
5 files changed, 38 insertions, 23 deletions
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<ImageReader> 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<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();
+ }
}
}
- 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
diff --git a/juick-server/src/main/java/com/juick/service/MessagesServiceImpl.java b/juick-server/src/main/java/com/juick/service/MessagesServiceImpl.java
index f79e4142..054503c1 100644
--- a/juick-server/src/main/java/com/juick/service/MessagesServiceImpl.java
+++ b/juick-server/src/main/java/com/juick/service/MessagesServiceImpl.java
@@ -118,7 +118,7 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ
insertMap.put("user_id", uid);
Instant now = Instant.now();
insertMap.put("ts", Timestamp.from(now));
- if (attachment != null) {
+ if (StringUtils.isNotEmpty(attachment)) {
insertMap.put("attach", attachment);
}
int mid = simpleJdbcInsert.executeAndReturnKey(insertMap).intValue();
diff --git a/juick-server/src/test/java/com/juick/server/tests/ServerTests.java b/juick-server/src/test/java/com/juick/server/tests/ServerTests.java
index b468daff..b4d91b79 100644
--- a/juick-server/src/test/java/com/juick/server/tests/ServerTests.java
+++ b/juick-server/src/test/java/com/juick/server/tests/ServerTests.java
@@ -79,9 +79,7 @@ import javax.xml.parsers.ParserConfigurationException;
import java.io.*;
import java.net.URI;
import java.net.URISyntaxException;
-import java.nio.file.Files;
-import java.nio.file.Path;
-import java.nio.file.Paths;
+import java.nio.file.*;
import java.sql.Timestamp;
import java.time.Instant;
import java.util.*;
@@ -1089,6 +1087,23 @@ public class ServerTests {
assertThat(postJpgCmyk.getNewMessage().get().getAttachment().getSmall().getHeight(), is(512));
}
@Test
+ public void JpegFromJuickUriShouldBeProcessedCorrectly() throws Exception {
+ Path tmpFile = Paths.get(tmpDir, "2915104.jpg");
+ Files.copy(Paths.get(ClassLoader.getSystemResource("2915104.jpg").toURI()), tmpFile, StandardCopyOption.REPLACE_EXISTING);
+ assertThat(tmpFile.toFile().exists(), is(true));
+ CommandResult postJpgiPhone = commandsManager.processCommand(ugnich, "YO", URI.create("juick://2915104.jpg"));
+ assertThat(postJpgiPhone.getNewMessage().isPresent(), is(true));
+ int mid = postJpgiPhone.getNewMessage().get().getMid();
+ File originalFile = Paths.get(imgDir, "p", String.format("%d.jpg", mid)).toFile();
+ assertThat(originalFile.exists(), is(true));
+ File mediumFile = Paths.get(imgDir, "photos-1024", String.format("%d.jpg", mid)).toFile();
+ assertThat(mediumFile.exists(), is(true));
+ assertThat(postJpgiPhone.getNewMessage().get().getAttachment().getWidth(), is(1280));
+ assertThat(postJpgiPhone.getNewMessage().get().getAttachment().getHeight(), is(1280));
+ assertThat(postJpgiPhone.getNewMessage().get().getAttachment().getMedium().getHeight(), is(1024));
+ assertThat(postJpgiPhone.getNewMessage().get().getAttachment().getSmall().getHeight(), is(512));
+ }
+ @Test
public void changeExtensionWhenReceiveFileWithWrongContentType() throws Exception {
Path pngOutput = Paths.get(tmpDir, "cmyk.png");
Files.deleteIfExists(pngOutput);
diff --git a/juick-server/src/test/resources/2915104.jpg b/juick-server/src/test/resources/2915104.jpg
new file mode 100644
index 00000000..7f0fc3ba
--- /dev/null
+++ b/juick-server/src/test/resources/2915104.jpg
Binary files differ