diff --git a/README.md b/README.md index c4e59b2..b1be2be 100644 --- a/README.md +++ b/README.md @@ -334,7 +334,7 @@ or Full description: https://proxy.services.wire.com/swagger#!/default/post -**Note:** `token` that comes with `conversation.init` events is _lifelong_. It should be stored for later usage. `token` +**Note:** `token` that comes with `conversation.bot_request` events is _lifelong_. It should be stored for later usage. `token` that comes with other event types has lifespan of 20 seconds. ### Bot Examples diff --git a/backend/roman.yaml b/backend/roman.yaml index ba1a2f4..c9608cf 100644 --- a/backend/roman.yaml +++ b/backend/roman.yaml @@ -22,7 +22,7 @@ logging: "com.wire.bots.logger": ${LOG_LEVEL:-INFO} swagger: - # make sure that this settings is the same as "server.rootPath" + # make sure that these settings is the same as "server.rootPath" uriPrefix: /api title: Roman Swagger description: Roman - Wire Bots Proxy diff --git a/backend/src/main/java/com/wire/bots/roman/ImageProcessor.java b/backend/src/main/java/com/wire/bots/roman/ImageProcessor.java index 1888373..6771dce 100644 --- a/backend/src/main/java/com/wire/bots/roman/ImageProcessor.java +++ b/backend/src/main/java/com/wire/bots/roman/ImageProcessor.java @@ -1,29 +1,30 @@ package com.wire.bots.roman; import com.wire.bots.roman.resources.Picture; -import com.wire.xenon.assets.ImagePreview; +import javax.imageio.ImageIO; import java.awt.*; import java.awt.image.BufferedImage; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; public class ImageProcessor { - private static final int MEDIUM_DIMENSION = 2896; public static Picture getMediumImage(Picture picture) throws Exception { - return getScaledImage(picture, MEDIUM_DIMENSION); + return getScaledImage(picture); } - private static Boolean shouldScaleOriginalSize(double width, double height, double dimension) { - final double maxPixelCount = 1.3 * dimension * dimension; - return (width > 1.3 * dimension || height > 1.3 * dimension) + private static Boolean shouldScaleOriginalSize(double width, double height) { + final double maxPixelCount = 1.3 * (double) ImageProcessor.MEDIUM_DIMENSION * (double) ImageProcessor.MEDIUM_DIMENSION; + return (width > 1.3 * (double) ImageProcessor.MEDIUM_DIMENSION || height > 1.3 * (double) ImageProcessor.MEDIUM_DIMENSION) && width * height > maxPixelCount; } - private static Size getScaledSize(double origWidth, double origHeight, double dimension) { + private static Size getScaledSize(double origWidth, double origHeight) { Size ret = new Size(); - double op1 = Math.min(dimension / origWidth, dimension / origHeight); - double op2 = dimension / Math.sqrt(origWidth * origHeight); + double op1 = Math.min((double) ImageProcessor.MEDIUM_DIMENSION / origWidth, (double) ImageProcessor.MEDIUM_DIMENSION / origHeight); + double op2 = (double) ImageProcessor.MEDIUM_DIMENSION / Math.sqrt(origWidth * origHeight); double scale = Math.max(op1, op2); double width = Math.ceil(scale * origWidth); ret.width = width; @@ -52,37 +53,29 @@ private static BufferedImage resizeImage(BufferedImage originalImage, return resizedImage; } - // todo. fixme, xenon to expose getMimeType type and getImageData - private static Picture getScaledImage(ImagePreview image, int dimension) throws Exception { -// String resultImageType; -// switch ("image/jpeg") { -// case "image/jpeg": -// resultImageType = "jpg"; -// break; -// case "image/png": -// resultImageType = "png"; -// break; -// default: -// throw new IllegalArgumentException("Unsupported mime type"); -// } -// -// int origWidth = image.getWidth(); -// int origHeight = image.getHeight(); -// -// BufferedImage resultImage = ImageIO.read(new ByteArrayInputStream(image.getImageData())); -// -// if (shouldScaleOriginalSize(origWidth, origHeight, dimension)) { -// Size scaledSize = getScaledSize(origWidth, origHeight, dimension); -// resultImage = resizeImage(resultImage, (int) scaledSize.width, -// (int) scaledSize.height); -// } -// -// try (ByteArrayOutputStream resultStream = new ByteArrayOutputStream()) { -// ImageIO.write(resultImage, resultImageType, resultStream); -// resultStream.flush(); -// return new Picture(resultStream.toByteArray(), image.getMimeType()); -// } - return null; + private static Picture getScaledImage(Picture image) throws Exception { + String resultImageType = switch (image.getMimeType()) { + case "image/jpeg" -> "jpg"; + case "image/png" -> "png"; + default -> throw new IllegalArgumentException("Unsupported mime type"); + }; + + int origWidth = image.getWidth(); + int origHeight = image.getHeight(); + + BufferedImage resultImage = ImageIO.read(new ByteArrayInputStream(image.getImageData())); + + if (shouldScaleOriginalSize(origWidth, origHeight)) { + Size scaledSize = getScaledSize(origWidth, origHeight); + resultImage = resizeImage(resultImage, (int) scaledSize.width, + (int) scaledSize.height); + } + + try (ByteArrayOutputStream resultStream = new ByteArrayOutputStream()) { + ImageIO.write(resultImage, resultImageType, resultStream); + resultStream.flush(); + return new Picture(resultStream.toByteArray(), image.getMimeType()); + } } private static class Size { diff --git a/backend/src/main/java/com/wire/bots/roman/resources/Picture.java b/backend/src/main/java/com/wire/bots/roman/resources/Picture.java index 475191b..33eb2b5 100644 --- a/backend/src/main/java/com/wire/bots/roman/resources/Picture.java +++ b/backend/src/main/java/com/wire/bots/roman/resources/Picture.java @@ -8,6 +8,7 @@ public class Picture extends ImagePreview { private boolean aPublic; private final byte[] imageData; + private final String mimeType; private int width; private int height; private String retention; @@ -16,6 +17,7 @@ public Picture(byte[] image, String mimeType) { super(UUID.randomUUID(), mimeType); this.imageData = image; + this.mimeType = mimeType; } public void setPublic(boolean aPublic) { @@ -45,4 +47,8 @@ public int getHeight() { public void setHeight(int height) { this.height = height; } + + public String getMimeType() { + return mimeType; + } }