diff --git a/src/main/java/de/thm/arsnova/ImageUtils.java b/src/main/java/de/thm/arsnova/ImageUtils.java index c47be1635158107faeaee8755d46e9fcfebdb7c8..093ecc831a35feb0fdf45a1e40a53a186459665b 100644 --- a/src/main/java/de/thm/arsnova/ImageUtils.java +++ b/src/main/java/de/thm/arsnova/ImageUtils.java @@ -32,6 +32,8 @@ import org.apache.commons.codec.binary.Base64; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; +import org.springframework.context.annotation.Configuration; import de.thm.arsnova.entities.Answer; @@ -41,6 +43,7 @@ import de.thm.arsnova.entities.Answer; * @author Daniel Vogel (daniel.vogel@mni.thm.de) * @author Jan Sladek (jan.sladek@mni.thm.de) */ +@Component("imageUtils") public class ImageUtils { // Or whatever size you want to read in at a time. @@ -53,24 +56,17 @@ public class ImageUtils { public static final String IMAGE_PREFIX_MIDDLE = ";base64,"; /* default value is 200 pixel in width, set the value in the configuration file */ - private static int THUMB_WIDTH = 200; + private static final int THUMB_WIDTH_DEFAULT = 200; /* default value is 200 pixel in height, set the value in the configuration file */ - private static int THUMB_HEIGHT = 200; + private static final int THUMB_HEIGHT_DEFAULT = 200; + + public static final Logger LOGGER = LoggerFactory.getLogger(ImageUtils.class); @Value("${imageupload.thumbnail.width}") - public void setThumbWidth(int thumbWidth) { - ImageUtils.THUMB_WIDTH = thumbWidth; - } + private int thumbWidth = THUMB_WIDTH_DEFAULT; @Value("${imageupload.thumbnail.height}") - public void setThumbHeight(int thumbHeight) { - ImageUtils.THUMB_HEIGHT = thumbHeight; - } - - private ImageUtils() { - } - - public static final Logger LOGGER = LoggerFactory.getLogger(ImageUtils.class); + private int thumbHeight = THUMB_HEIGHT_DEFAULT; /** * Converts an image to an Base64 String. @@ -78,7 +74,7 @@ public class ImageUtils { * @param imageUrl The image url as a {@link String} * @return The Base64 {@link String} of the image on success, otherwise <code>null</code>. */ - public static String encodeImageToString(final String imageUrl) { + public String encodeImageToString(final String imageUrl) { final String[] urlParts = imageUrl.split("\\."); final StringBuilder result = new StringBuilder(); @@ -105,7 +101,7 @@ public class ImageUtils { * @param maybeImage The Image as a base64 encoded {@link String} * @return true if the string is a potentially a base 64 encoded image. */ - public static boolean isBase64EncodedImage(String maybeImage) { + public boolean isBase64EncodedImage(String maybeImage) { return extractImageInfo(maybeImage) != null; } @@ -120,7 +116,7 @@ public class ImageUtils { * @return two-dimensional {@link String}-array containing the information * "extension" and the "raw-image-{@link String}" */ - public static String[] extractImageInfo(final String maybeImage) { + public String[] extractImageInfo(final String maybeImage) { if (maybeImage == null) { return null; } else if (maybeImage.isEmpty()) { @@ -166,7 +162,7 @@ public class ImageUtils { * @return The rescaled Image as Base64-encoded {@link String}, returns null * if the passed-on image isn't in a valid format (a Base64-Image). */ - public static String createCover(String originalImageString, final int width, final int height) { + public String createCover(String originalImageString, final int width, final int height) { if (!isBase64EncodedImage(originalImageString)) { return null; } else { @@ -226,9 +222,9 @@ public class ImageUtils { * @return true if the thumbnail image didn't exist before calling this * method, false otherwise */ - public static boolean generateThumbnailImage(Answer answer) { + public boolean generateThumbnailImage(Answer answer) { if (!isBase64EncodedImage(answer.getAnswerThumbnailImage())) { - final String thumbImage = createCover(answer.getAnswerImage(), THUMB_WIDTH, THUMB_HEIGHT); + final String thumbImage = createCover(answer.getAnswerImage(), thumbWidth, thumbHeight); answer.setAnswerThumbnailImage(thumbImage); return true; } @@ -241,7 +237,7 @@ public class ImageUtils { * @param imageUrl The image url as a {@link String} * @return The <code>byte[]</code> of the image on success, otherwise <code>null</code>. */ - public static byte[] convertImageToByteArray(final String imageUrl, final String extension) { + public byte[] convertImageToByteArray(final String imageUrl, final String extension) { try { final URL url = new URL(imageUrl); @@ -269,7 +265,7 @@ public class ImageUtils { * @param imageUrl The image url as a {@link String} * @return The <code>byte[]</code> of the image on success, otherwise <code>null</code>. */ - public static byte[] convertFileToByteArray(final String imageUrl) { + public byte[] convertFileToByteArray(final String imageUrl) { try { diff --git a/src/main/java/de/thm/arsnova/config/ExtraConfig.java b/src/main/java/de/thm/arsnova/config/ExtraConfig.java index 45b375c2351a2774c7c8233fa17fb66d19613020..f971a219a76f02030d1b4aa025b1d22dd222bd8a 100644 --- a/src/main/java/de/thm/arsnova/config/ExtraConfig.java +++ b/src/main/java/de/thm/arsnova/config/ExtraConfig.java @@ -35,6 +35,7 @@ import de.thm.arsnova.connector.client.ConnectorClient; import de.thm.arsnova.connector.client.ConnectorClientImpl; import de.thm.arsnova.socket.ARSnovaSocket; import de.thm.arsnova.socket.ARSnovaSocketIOServer; +import de.thm.arsnova.ImageUtils; @Configuration @EnableCaching @@ -108,4 +109,9 @@ public class ExtraConfig { public CacheManager cacheManager() { return new ConcurrentMapCacheManager(); } + + @Bean + public ImageUtils imageUtils() { + return new ImageUtils(); + } } diff --git a/src/main/java/de/thm/arsnova/services/QuestionService.java b/src/main/java/de/thm/arsnova/services/QuestionService.java index 800b5dab1ec1fab3b18f4e6a65750e136808c4c9..fae75ff96c7b35ca8bc89aac5aed74d585a0cef0 100644 --- a/src/main/java/de/thm/arsnova/services/QuestionService.java +++ b/src/main/java/de/thm/arsnova/services/QuestionService.java @@ -71,6 +71,9 @@ public class QuestionService implements IQuestionService, ApplicationEventPublis @Autowired private IUserService userService; + @Autowired + private ImageUtils imageUtils; + @Value("${upload.filesize_b}") private int uploadFileSizeByte; @@ -118,7 +121,7 @@ public class QuestionService implements IQuestionService, ApplicationEventPublis // convert imageurl to base64 if neccessary if ("grid".equals(question.getQuestionType())) { if (question.getImage().startsWith("http")) { - final String base64ImageString = ImageUtils.encodeImageToString(question.getImage()); + final String base64ImageString = imageUtils.encodeImageToString(question.getImage()); if (base64ImageString == null) { throw new BadRequestException(); } @@ -539,7 +542,7 @@ public class QuestionService implements IQuestionService, ApplicationEventPublis Answer theAnswer = answer.generateAnswerEntity(user, question); if ("freetext".equals(question.getQuestionType())) { - ImageUtils.generateThumbnailImage(theAnswer); + imageUtils.generateThumbnailImage(theAnswer); } return databaseDao.saveAnswer(theAnswer, user, question, getSession(question.getSessionKeyword())); @@ -556,7 +559,7 @@ public class QuestionService implements IQuestionService, ApplicationEventPublis final Question question = getQuestion(answer.getQuestionId()); if ("freetext".equals(question.getQuestionType())) { - ImageUtils.generateThumbnailImage(realAnswer); + imageUtils.generateThumbnailImage(realAnswer); } final Answer result = databaseDao.updateAnswer(realAnswer); final Session session = databaseDao.getSessionFromKeyword(question.getSessionKeyword()); diff --git a/src/main/java/de/thm/arsnova/services/SessionService.java b/src/main/java/de/thm/arsnova/services/SessionService.java index 2f8211bd58b5f751d64de0fe4c89f7482bbf776c..46ab62b48a31a351fe6614437d977d0f0a71b91c 100644 --- a/src/main/java/de/thm/arsnova/services/SessionService.java +++ b/src/main/java/de/thm/arsnova/services/SessionService.java @@ -100,6 +100,9 @@ public class SessionService implements ISessionService, ApplicationEventPublishe @Autowired(required = false) private ConnectorClient connectorClient; + @Autowired + private ImageUtils imageUtils; + @Value("${pp.logofilesize_b}") private int uploadFileSizeByte; @@ -222,7 +225,7 @@ public class SessionService implements ISessionService, ApplicationEventPublishe } if (session.getPpLogo() != null) { if (session.getPpLogo().startsWith("http")) { - final String base64ImageString = ImageUtils.encodeImageToString(session.getPpLogo()); + final String base64ImageString = imageUtils.encodeImageToString(session.getPpLogo()); if (base64ImageString == null) { throw new BadRequestException(); } diff --git a/src/test/java/de/thm/arsnova/ImageUtilsTest.java b/src/test/java/de/thm/arsnova/ImageUtilsTest.java index 0a8237721da37bcddcfcbbf383237eda50e54253..e3159b23318ce1cdae661dd94d11d6dd677f7b47 100644 --- a/src/test/java/de/thm/arsnova/ImageUtilsTest.java +++ b/src/test/java/de/thm/arsnova/ImageUtilsTest.java @@ -1,7 +1,6 @@ package de.thm.arsnova; -import static de.thm.arsnova.ImageUtils.isBase64EncodedImage; -import static de.thm.arsnova.ImageUtils.extractImageInfo; +import de.thm.arsnova.ImageUtils; import static de.thm.arsnova.ImageUtils.IMAGE_PREFIX_START; import static de.thm.arsnova.ImageUtils.IMAGE_PREFIX_MIDDLE; @@ -10,17 +9,35 @@ import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertEquals; import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.web.WebAppConfiguration; + +@RunWith(SpringJUnit4ClassRunner.class) +@WebAppConfiguration +@ContextConfiguration(locations = { + "file:src/main/webapp/WEB-INF/spring/arsnova-servlet.xml", + "file:src/main/webapp/WEB-INF/spring/spring-main.xml", + "file:src/test/resources/test-config.xml", + "file:src/test/resources/test-socketioconfig.xml" +}) +@ActiveProfiles("test") public class ImageUtilsTest { + private ImageUtils imageUtils = new ImageUtils(); + @Test public void testNullIsNoValidBase64String() { - assertFalse("\"null\" is no valid Base64 String.", isBase64EncodedImage(null)); + assertFalse("\"null\" is no valid Base64 String.", imageUtils.isBase64EncodedImage(null)); } @Test public void testEmptyStringIsNoValidBase64String() { - assertFalse("The empty String is no valid Base64 String.", isBase64EncodedImage("")); + assertFalse("The empty String is no valid Base64 String.", imageUtils.isBase64EncodedImage("")); } @Test @@ -33,7 +50,7 @@ public class ImageUtilsTest { for (String fakeString : fakeStrings) { assertFalse( String.format("The String %s is not a valid Base64 String.", fakeString), - isBase64EncodedImage(fakeString) + imageUtils.isBase64EncodedImage(fakeString) ); } } @@ -41,7 +58,7 @@ public class ImageUtilsTest { @Test public void testValidBase64String() { final String imageString = String.format("%spng%sIMAGE-DATA", IMAGE_PREFIX_START, IMAGE_PREFIX_MIDDLE); - assertTrue(isBase64EncodedImage(imageString)); + assertTrue(imageUtils.isBase64EncodedImage(imageString)); } @Test @@ -51,7 +68,7 @@ public class ImageUtilsTest { final String imageString = String.format("%s%s%s%s", IMAGE_PREFIX_START, extension, IMAGE_PREFIX_MIDDLE, imageData); - final String[] imageInfo = extractImageInfo(imageString); + final String[] imageInfo = imageUtils.extractImageInfo(imageString); assertNotNull(imageInfo); assertEquals("Extracted information doesn't match its specification.", 2, imageInfo.length);