diff --git a/src/main/java/de/thm/arsnova/ImageUtils.java b/src/main/java/de/thm/arsnova/ImageUtils.java index 41c92a140259651f37e7178a8c889bed5257085c..0af116547c229c335ee88ca292528365c927cf7c 100644 --- a/src/main/java/de/thm/arsnova/ImageUtils.java +++ b/src/main/java/de/thm/arsnova/ImageUtils.java @@ -22,6 +22,7 @@ package de.thm.arsnova; import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import java.io.IOException; +import java.io.InputStream; import java.net.MalformedURLException; import java.net.URL; @@ -59,9 +60,8 @@ public class ImageUtils { String extension = urlParts[urlParts.length-1]; - // TODO: Check in frontend if really needed to append this string result.append("data:image/" + extension + ";base64,"); - result.append(Base64.encodeBase64String(convertImageToByteArray(imageUrl, extension))); + result.append(Base64.encodeBase64String(convertFileToByteArray(imageUrl))); return result.toString(); } @@ -81,9 +81,45 @@ public class ImageUtils { URL url = new URL(imageUrl); BufferedImage image = ImageIO.read(url); ByteArrayOutputStream baos = new ByteArrayOutputStream(); - + ImageIO.write(image, extension, baos); + baos.flush(); + baos.close(); + return baos.toByteArray(); + + } catch (MalformedURLException e) { + LOGGER.error(e.getLocalizedMessage()); + } catch (IOException e) { + LOGGER.error(e.getLocalizedMessage()); + } + + return null; + } + + /** + * Gets the bytestream of an image url. + * s + * @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(String imageUrl) { + + try { + URL url = new URL(imageUrl); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + + InputStream is = url.openStream(); + byte[] byteChunk = new byte[4096]; // Or whatever size you want to read in at a time. + int n; + + while ( (n = is.read(byteChunk)) > 0 ) { + baos.write(byteChunk, 0, n); + } + + baos.flush(); + baos.close(); + return baos.toByteArray(); } catch (MalformedURLException e) { diff --git a/src/main/java/de/thm/arsnova/services/QuestionService.java b/src/main/java/de/thm/arsnova/services/QuestionService.java index 4c2fe20f5c8fa693d7fd5cac38d898954b1a78a2..f91da75e999fb8f84ae0589072373c8325b2a9a1 100644 --- a/src/main/java/de/thm/arsnova/services/QuestionService.java +++ b/src/main/java/de/thm/arsnova/services/QuestionService.java @@ -24,7 +24,10 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import de.thm.arsnova.ImageUtils; @@ -53,6 +56,11 @@ public class QuestionService implements IQuestionService { @Autowired private ARSnovaSocketIOServer socketIoServer; + + @Value("${upload.filesize_b}") + private int uploadFileSizeByte; + + public static final Logger LOGGER = LoggerFactory.getLogger(QuestionService.class); public void setDatabaseDao(IDatabaseDao databaseDao) { this.databaseDao = databaseDao; @@ -91,6 +99,7 @@ public class QuestionService implements IQuestionService { // convert imageurl to base64 if neccessary if ("grid".equals(question.getQuestionType())) { + org.slf4j.Logger logger = LoggerFactory.getLogger(QuestionService.class); if (question.getImage().startsWith("http")) { String base64ImageString = ImageUtils.encodeImageToString(question.getImage()); if (base64ImageString == null) { @@ -98,6 +107,13 @@ public class QuestionService implements IQuestionService { } question.setImage(base64ImageString); } + + // base64 adds offset to filesize, formular taken from: http://en.wikipedia.org/wiki/Base64#MIME + int fileSize = (int)((question.getImage().length()-814)/1.37); + if ( fileSize > this.uploadFileSizeByte ) { + LOGGER.error("Could not save file. File is too large with "+ fileSize + " Byte."); + throw new BadRequestException(); + } } Question result = this.databaseDao.saveQuestion(session, question); diff --git a/src/main/webapp/arsnova.properties b/src/main/webapp/arsnova.properties index dc6de872e4301062dfbcb37afe7f8f25a5b68f9f..65a05595afd0c948b295205002aa87b98bce2c3e 100644 --- a/src/main/webapp/arsnova.properties +++ b/src/main/webapp/arsnova.properties @@ -17,6 +17,9 @@ security.storepass=arsnova # minutes, after which the feedback is deleted feedback.cleanup=10 +# maximal filesize in bytes +upload.filesize_b = 1048576 + couchdb.host=localhost couchdb.port=5984 couchdb.name=arsnova