Uploaded image for project: 'OpenOLAT'
  1. OpenOLAT
  2. OO-5391

Forum: make archiver more robust against erroneous image attachments

    XMLWordPrintable

    Details

    • Type: Improvement
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 15.4.4
    • Fix Version/s: 15.3.14, 15.4.6
    • Component/s: Forum tool
    • Labels:
      None

      Description

      There are some cases where users attach images to a forum message which has e.g. the suffix "jpg" but the file format is "PNG". This case results in an empty archive. I also attach an image which has the suffix png but its format is WEBP. For this type an appropriate ImageReader is not found. For this case the rendering of the image in the resulting document has not to be done and the image has to be handled as a normal file attachment.

      This patch tries to catch the wrong cases and delivers a non empty archive:

      diff --git a/src/main/java/org/olat/core/commons/services/image/ImageUtils.java b/src/main/java/org/olat/core/commons/services/image/ImageUtils.java
      index 36b61a5..8a4fe4a 100644
      --- a/src/main/java/org/olat/core/commons/services/image/ImageUtils.java
      +++ b/src/main/java/org/olat/core/commons/services/image/ImageUtils.java
      @@ -58,10 +58,11 @@
       	public static Size getImageSize(String suffix, InputStream in) {
       		Size result = null;
       
      -		Iterator<ImageReader> iter = ImageIO.getImageReadersBySuffix(suffix);
      +		ImageInputStream stream = new MemoryCacheImageInputStream(in);
      +		Iterator<ImageReader> iter = ImageIO.getImageReaders(stream);
       		if (iter.hasNext()) {
       			ImageReader reader = iter.next();
      -			try(ImageInputStream stream = new MemoryCacheImageInputStream(in)) {
      +			try {
       				reader.setInput(stream);
       				
       				int imageIndex = reader.getMinIndex();
      diff --git a/src/main/java/org/olat/core/util/openxml/OpenXMLDocument.java b/src/main/java/org/olat/core/util/openxml/OpenXMLDocument.java
      index 85fa835..40aabb7 100644
      --- a/src/main/java/org/olat/core/util/openxml/OpenXMLDocument.java
      +++ b/src/main/java/org/olat/core/util/openxml/OpenXMLDocument.java
      @@ -1098,7 +1098,7 @@
       		return mathEls;
       	}
       	
      -	public void appendImage(File file) {
      +	public boolean appendImage(File file) {
       		Element imgEl = createImageEl(file);
       		if(imgEl != null) {
       			Element runEl = createRunEl(Collections.singletonList(imgEl));
      @@ -1106,6 +1106,7 @@
       			paragraphEl.appendChild(runEl);
       			getCursor().appendChild(paragraphEl);
       		}
      +		return imgEl != null;
       	}
       
       	public Element createImageEl(String path, double maxWidthCm) {
      @@ -1182,6 +1183,9 @@
       	
       	public Element createImageEl(File image, double widthCm) {
       		DocReference ref = registerImage(image, widthCm);
      +		if (ref == null)
      +			return null;
      +		
       		String id = ref.getId();
       		OpenXMLSize emuSize = ref.getEmuSize();
       		String filename = ref.getFilename();
      @@ -1280,7 +1284,10 @@
       		} else {
       			String id = generateId();
       			Size size = ImageUtils.getImageSize(image);
      -			OpenXMLSize emuSize = OpenXMLUtils.convertPixelToEMUs(size, DPI, widthCm/* cm */);
      +			if (size == null)
      +				return null;
      +			
      +			OpenXMLSize	emuSize = OpenXMLUtils.convertPixelToEMUs(size, DPI, widthCm/* cm */);
       			String filename = getUniqueFilename(image);
       			ref = new DocReference(id, filename, emuSize, image);
       			fileToImagesMap.put(image, ref);
      diff --git a/src/main/java/org/olat/modules/fo/archiver/formatters/ForumOpenXMLFormatter.java b/src/main/java/org/olat/modules/fo/archiver/formatters/ForumOpenXMLFormatter.java
      index 7d08751..9070fa4 100644
      --- a/src/main/java/org/olat/modules/fo/archiver/formatters/ForumOpenXMLFormatter.java
      +++ b/src/main/java/org/olat/modules/fo/archiver/formatters/ForumOpenXMLFormatter.java
      @@ -186,8 +186,7 @@
       				if (lastDot > 0) {
       					String extension = filename.substring(lastDot + 1).toLowerCase();
       					if("jpeg".equals(extension) || "jpg".equals(extension) || "gif".equals(extension) || "png".equals(extension)) {
      -						document.appendImage(file);
      -						attach = false;
      +						attach = !document.appendImage(file);
       					}
       				}
      

       

        Figma for Jira By 42nd

          Attachments

            Activity

              People

              Assignee:
              srosse Stéphane Rossé
              Reporter:
              stephan Stephan Clemenz
              Tester:
              Mandy Menzel
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: