aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--juick-common/src/main/java/com/juick/server/util/ImageUtils.java38
-rw-r--r--juick-common/src/main/java/com/juick/service/ImagesService.java2
-rw-r--r--juick-common/src/main/java/com/juick/service/ImagesServiceImpl.java6
-rw-r--r--juick-server-jdbc/src/main/java/com/juick/service/MessagesServiceImpl.java12
-rw-r--r--juick-server/src/main/resources/1x1.pngbin0 -> 95 bytes
-rw-r--r--juick-server/src/test/java/com/juick/server/tests/ServerTests.java26
6 files changed, 54 insertions, 30 deletions
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 71dec381..cea4cc88 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
@@ -19,7 +19,6 @@
package com.juick.server.util;
import com.juick.Attachment;
-import org.apache.commons.imaging.ImageInfo;
import org.apache.commons.imaging.ImageReadException;
import org.apache.commons.imaging.Imaging;
import org.apache.commons.imaging.common.ImageMetadata;
@@ -33,6 +32,9 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.imageio.ImageIO;
+import javax.imageio.ImageReader;
+import javax.imageio.stream.FileImageInputStream;
+import javax.imageio.stream.ImageInputStream;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
@@ -40,6 +42,7 @@ import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
+import java.util.Iterator;
public class ImageUtils {
private static final Logger logger = LoggerFactory.getLogger(ImageUtils.class);
@@ -144,19 +147,32 @@ public class ImageUtils {
ImageIO.write(Scalr.resize(originalImage, 96), targetExt, Paths.get(imgDir, "a", targetName).toFile());
ImageIO.write(Scalr.resize(originalImage, 32), targetExt, Paths.get(imgDir, "as", targetName).toFile());
}
-
- public Attachment getAttachment(File imageFile) throws IOException {
+ public Attachment getAttachment(File imgFile) throws IOException {
Attachment attachment = new Attachment();
- if (imageFile.exists()) {
+ 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();
+ ImageInputStream stream = null;
try {
- ImageInfo info = Imaging.getImageInfo(imageFile);
- attachment.setHeight(info.getHeight());
- attachment.setWidth(info.getWidth());
- } catch (ImageReadException e) {
- logger.info("Can not read {}, moved to {}", imageFile.toPath(),
- Files.move(imageFile.toPath(), Paths.get(tmpDir, imageFile.getName()), StandardCopyOption.REPLACE_EXISTING));
+ stream = new FileImageInputStream(imgFile);
+ reader.setInput(stream);
+ attachment.setWidth(reader.getWidth(reader.getMinIndex()));
+ attachment.setHeight(reader.getHeight(reader.getMinIndex()));
+ return attachment;
+ } catch (IOException e) {
+ logger.warn("Error reading: " + imgFile.getAbsolutePath(), e);
+ } finally {
+ if (stream != null) {
+ stream.close();
+ }
+ reader.dispose();
}
}
- return attachment;
+
+ throw new IOException("Not a known image file: " + imgFile.getAbsolutePath());
}
} \ No newline at end of file
diff --git a/juick-common/src/main/java/com/juick/service/ImagesService.java b/juick-common/src/main/java/com/juick/service/ImagesService.java
index 192217fe..902301ed 100644
--- a/juick-common/src/main/java/com/juick/service/ImagesService.java
+++ b/juick-common/src/main/java/com/juick/service/ImagesService.java
@@ -5,7 +5,7 @@ import com.juick.Message;
import java.io.IOException;
public interface ImagesService {
- void setAttachmentMetadata(String imgDir, String baseUrl, Message msg) throws Exception;
+ void setAttachmentMetadata(String baseUrl, Message msg) throws Exception;
/**
* Move attached image from temp folder to image folder.
* Create preview images in corresponding folders.
diff --git a/juick-common/src/main/java/com/juick/service/ImagesServiceImpl.java b/juick-common/src/main/java/com/juick/service/ImagesServiceImpl.java
index dbc3f3a9..67c8360e 100644
--- a/juick-common/src/main/java/com/juick/service/ImagesServiceImpl.java
+++ b/juick-common/src/main/java/com/juick/service/ImagesServiceImpl.java
@@ -12,11 +12,15 @@ import java.nio.file.Paths;
public class ImagesServiceImpl implements ImagesService {
private ImageUtils imageUtils;
+ private String imgDir;
+ private String tmpDir;
public ImagesServiceImpl(String imgDir, String tmpDir) {
+ this.imgDir = imgDir;
+ this.tmpDir = tmpDir;
imageUtils = new ImageUtils(imgDir, tmpDir);
}
@Override
- public void setAttachmentMetadata(String imgDir, String baseUrl, Message msg) throws Exception {
+ public void setAttachmentMetadata(String baseUrl, Message msg) throws Exception {
if (!StringUtils.isEmpty(msg.getAttachmentType())) {
Photo photo = new Photo();
if (msg.getRid()> 0) {
diff --git a/juick-server-jdbc/src/main/java/com/juick/service/MessagesServiceImpl.java b/juick-server-jdbc/src/main/java/com/juick/service/MessagesServiceImpl.java
index 6c12583c..612431bf 100644
--- a/juick-server-jdbc/src/main/java/com/juick/service/MessagesServiceImpl.java
+++ b/juick-server-jdbc/src/main/java/com/juick/service/MessagesServiceImpl.java
@@ -56,8 +56,6 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ
private SearchService searchService;
@Inject
private ImagesService imagesService;
- @Value("${img_path:#{systemEnvironment['TEMP'] ?: '/tmp'}}")
- private String imgDir;
@Value("${img_url:https://i.juick.com/}")
private String baseImagesUrl;
@@ -94,10 +92,12 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ
quoteUser.setName(rs.getString(20));
msg.setTo(quoteUser);
}
- try {
- imagesService.setAttachmentMetadata(imgDir, baseImagesUrl, msg);
- } catch (Exception e) {
- logger.warn("exception reading images for mid {} rid {}", msg.getMid(), msg.getRid(), e);
+ if (StringUtils.isNotEmpty(msg.getAttachmentType())) {
+ try {
+ imagesService.setAttachmentMetadata(baseImagesUrl, msg);
+ } catch (Exception e) {
+ logger.warn("exception reading images for mid {} rid {}", msg.getMid(), msg.getRid(), e);
+ }
}
return msg;
}
diff --git a/juick-server/src/main/resources/1x1.png b/juick-server/src/main/resources/1x1.png
new file mode 100644
index 00000000..1914264c
--- /dev/null
+++ b/juick-server/src/main/resources/1x1.png
Binary files differ
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 177ced5e..924615d7 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
@@ -21,16 +21,14 @@ import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
-import com.juick.ExternalToken;
-import com.juick.Message;
-import com.juick.Tag;
-import com.juick.User;
+import com.juick.*;
import com.juick.server.*;
import com.juick.server.component.MessageEvent;
import com.juick.server.helpers.AnonymousUser;
import com.juick.server.helpers.CommandResult;
import com.juick.server.helpers.TagStats;
import com.juick.server.util.HttpUtils;
+import com.juick.server.util.ImageUtils;
import com.juick.service.*;
import com.juick.util.DateFormattersHolder;
import com.juick.util.MessageUtils;
@@ -84,11 +82,9 @@ import javax.xml.bind.Unmarshaller;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
-import java.io.ByteArrayInputStream;
-import java.io.IOException;
-import java.io.StringReader;
-import java.io.StringWriter;
+import java.io.*;
import java.net.URI;
+import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.sql.Timestamp;
@@ -170,7 +166,7 @@ public class ServerTests {
private static boolean isSetUp = false;
@Before
- public void setUp() throws IOException {
+ public void setUp() throws Exception {
Files.createDirectory(Paths.get(imgDir, "p"));
Files.createDirectory(Paths.get(imgDir, "photos-1024"));
Files.createDirectory(Paths.get(imgDir, "photos-512"));
@@ -190,8 +186,8 @@ public class ServerTests {
juick = userService.getUserByUID(juickId).orElseThrow(IllegalStateException::new);
String msgText = "Привет, я - Угнич";
-
- int mid = messagesService.createMessage(ugnich.getUid(), msgText, "png", null);
+ CommandResult result = commandsManager.processCommand(ugnich, msgText, URI.create("http://static.juick.com/settings/facebook.png"));
+ int mid = result.getNewMessage().get().getMid();
msg = messagesService.getMessage(mid);
tagService.createTag("тест");
juickTagId = tagService.createTag("juick");
@@ -1021,4 +1017,12 @@ public class ServerTests {
.with(httpBasic(juickName, juickPassword)))
.andExpect(status().isBadRequest());
}
+ @Test
+ public void attachmentSizeTests() throws URISyntaxException, IOException {
+ ImageUtils imageUtils = new ImageUtils(StringUtils.EMPTY, StringUtils.EMPTY);
+ Attachment attachment = imageUtils.getAttachment(new File(getClass().getClassLoader().getResource("Transparent.gif").toURI()));
+ assertThat(attachment.getHeight(), is(1));
+ assertThat(attachment.getWidth(), is(1));
+
+ }
}