]> git.basschouten.com Git - openhab-addons.git/commitdiff
[pushover] Add support to send an Image URL directly (#11027)
authorChristoph Weitkamp <github@christophweitkamp.de>
Sun, 25 Jul 2021 07:34:04 +0000 (09:34 +0200)
committerGitHub <noreply@github.com>
Sun, 25 Jul 2021 07:34:04 +0000 (09:34 +0200)
* Added support to send an Image URL directly
* Add support for data URI scheme

Signed-off-by: Christoph Weitkamp <github@christophweitkamp.de>
bundles/org.openhab.binding.pushover/README.md
bundles/org.openhab.binding.pushover/src/main/java/org/openhab/binding/pushover/internal/connection/PushoverConfigurationException.java
bundles/org.openhab.binding.pushover/src/main/java/org/openhab/binding/pushover/internal/connection/PushoverMessageBuilder.java
bundles/org.openhab.binding.pushover/src/main/resources/OH-INF/i18n/pushover.properties
bundles/org.openhab.binding.pushover/src/main/resources/OH-INF/i18n/pushover_de.properties

index 7ae9fd4543654f59a350c217a25ed161214c1dcd..312f9de43d6be1c8793783927b82d4e5b565f48c 100644 (file)
@@ -46,7 +46,7 @@ One has to pass a `null` value if it should be skipped or the default value for
 
 - `sendMonospaceMessage(String message, @Nullable String title)` - This method is used to send a monospace message.
 
-- `sendAttachmentMessage(String message, @Nullable String title, String attachment, @Nullable String contentType)` - This method is used to send a message with an attachment. It takes a (local) path to the attachment (parameter `attachment` **mandatory**) and an optional `contentType` to define the content-type of the attachment (default: `image/jpeg`).
+- `sendAttachmentMessage(String message, @Nullable String title, String attachment, @Nullable String contentType)` - This method is used to send a message with an attachment. It takes a local path or URL to the attachment (parameter `attachment` **mandatory**). Additionally you can pass a data URI scheme to this parameter. Optionally pass a `contentType` to define the content-type of the attachment (default: `image/jpeg` or guessed from image data).
 
 - `sendURLMessage(String message, @Nullable String title, String url, @Nullable String urlTitle)` - This method is used to send a message with an URL. A supplementary `url` to show with the message and a `urlTitle` for the URL, otherwise just the URL is shown.
 
@@ -76,6 +76,16 @@ val actions = getActions("pushover", "pushover:pushover-account:account")
 actions.sendHtmlMessage("Hello <font color='green'>World</font>!", "openHAB")
 ```
 
+```java
+val actions = getActions("pushover", "pushover:pushover-account:account")
+// send message with attachment
+actions.sendAttachmentMessage("Hello World!", "openHAB", "/path/to/my-local-image.png", "image/png")
+actions.sendAttachmentMessage("Hello World!", "openHAB", "https://www.openhab.org/openhab-logo-square.png", null)
+actions.sendAttachmentMessage("Hello World!", "openHAB", "data:[<media type>][;base64],<data>", null)
+// in case you want to send the content of an Image Item (RawType)
+actions.sendAttachmentMessage("Hello World!", "openHAB", myImageItem.state.toFullString, null)
+```
+
 ```java
 val actions = getActions("pushover", "pushover:pushover-account:account")
 // send priority message
index 698b83df9458b5b479e1947f0ae9dee32f44123a..f0d1ed8b9c36b8cac56fb59aec1288615f490045 100644 (file)
@@ -20,7 +20,7 @@ import org.eclipse.jdt.annotation.NonNullByDefault;
  * @author Christoph Weitkamp - Initial contribution
  */
 @NonNullByDefault
-public class PushoverConfigurationException extends RuntimeException {
+public class PushoverConfigurationException extends IllegalArgumentException {
 
     private static final long serialVersionUID = 1L;
 
index 29424cc40d107d609d4b4981e168d5b57b8d3612..1d72dc172cb7ba3afc97b8e4abb94d2c410eb616 100644 (file)
@@ -14,7 +14,8 @@ package org.openhab.binding.pushover.internal.connection;
 
 import java.io.File;
 import java.io.IOException;
-import java.util.Arrays;
+import java.nio.file.Files;
+import java.nio.file.Path;
 import java.util.List;
 import java.util.stream.Collectors;
 
@@ -24,6 +25,8 @@ import org.eclipse.jetty.client.api.ContentProvider;
 import org.eclipse.jetty.client.util.MultiPartContentProvider;
 import org.eclipse.jetty.client.util.PathContentProvider;
 import org.eclipse.jetty.client.util.StringContentProvider;
+import org.openhab.core.io.net.http.HttpUtil;
+import org.openhab.core.library.types.RawType;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -55,7 +58,7 @@ public class PushoverMessageBuilder {
     private static final int MAX_MESSAGE_LENGTH = 1024;
     private static final int MAX_TITLE_LENGTH = 250;
     private static final int MAX_DEVICE_LENGTH = 25;
-    private static final List<Integer> VALID_PRIORITY_LIST = Arrays.asList(-2, -1, 0, 1, 2);
+    private static final List<Integer> VALID_PRIORITY_LIST = List.of(-2, -1, 0, 1, 2);
     private static final int DEFAULT_PRIORITY = 0;
     public static final int EMERGENCY_PRIORITY = 2;
     private static final int MIN_RETRY_SECONDS = 30;
@@ -76,7 +79,7 @@ public class PushoverMessageBuilder {
     private @Nullable String urlTitle;
     private @Nullable String sound;
     private @Nullable String attachment;
-    private String contentType = DEFAULT_CONTENT_TYPE;
+    private @Nullable String contentType;
     private boolean html = false;
     private boolean monospace = false;
 
@@ -239,18 +242,31 @@ public class PushoverMessageBuilder {
         }
 
         if (attachment != null) {
-            File file = new File(attachment);
-            if (!file.exists()) {
-                throw new IllegalArgumentException(
-                        String.format("Skip sending the message as file '%s' does not exist.", attachment));
-            }
-            try {
-                body.addFilePart(MESSAGE_KEY_ATTACHMENT, file.getName(),
-                        new PathContentProvider(contentType, file.toPath()), null);
-            } catch (IOException e) {
-                logger.debug("IOException occurred - skip sending message: {}", e.getLocalizedMessage(), e);
-                throw new PushoverCommunicationException(
-                        String.format("Skip sending the message: %s", e.getLocalizedMessage()), e);
+            String localAttachment = attachment;
+            if (localAttachment.startsWith("http")) { // support data HTTP(S) scheme
+                RawType rawImage = HttpUtil.downloadImage(attachment, 10000);
+                if (rawImage == null) {
+                    throw new IllegalArgumentException(
+                            String.format("Skip sending the message as content '%s' does not exist.", attachment));
+                }
+                addFilePart(createTempFile(rawImage.getBytes()),
+                        contentType == null ? rawImage.getMimeType() : contentType);
+            } else if (localAttachment.startsWith("data:")) { // support data URI scheme
+                try {
+                    RawType rawImage = RawType.valueOf(localAttachment);
+                    addFilePart(createTempFile(rawImage.getBytes()),
+                            contentType == null ? rawImage.getMimeType() : contentType);
+                } catch (IllegalArgumentException e) {
+                    throw new IllegalArgumentException(String
+                            .format("Skip sending the message because data URI scheme is invalid: %s", e.getMessage()));
+                }
+            } else {
+                File file = new File(attachment);
+                if (!file.exists()) {
+                    throw new IllegalArgumentException(
+                            String.format("Skip sending the message as file '%s' does not exist.", attachment));
+                }
+                addFilePart(file.toPath(), contentType);
             }
         }
 
@@ -262,4 +278,28 @@ public class PushoverMessageBuilder {
 
         return body;
     }
+
+    private Path createTempFile(byte[] data) throws PushoverCommunicationException {
+        try {
+            Path tmpFile = Files.createTempFile("pushover-", ".tmp");
+            return Files.write(tmpFile, data);
+        } catch (IOException e) {
+            logger.debug("IOException occurred while creating temp file - skip sending message: {}",
+                    e.getLocalizedMessage(), e);
+            throw new PushoverCommunicationException(
+                    String.format("Skip sending the message: %s", e.getLocalizedMessage()), e);
+        }
+    }
+
+    private void addFilePart(Path path, @Nullable String contentType) throws PushoverCommunicationException {
+        try {
+            body.addFilePart(MESSAGE_KEY_ATTACHMENT, path.toFile().getName(),
+                    new PathContentProvider(contentType == null ? DEFAULT_CONTENT_TYPE : contentType, path), null);
+        } catch (IOException e) {
+            logger.debug("IOException occurred while adding content - skip sending message: {}",
+                    e.getLocalizedMessage(), e);
+            throw new PushoverCommunicationException(
+                    String.format("Skip sending the message: %s", e.getLocalizedMessage()), e);
+        }
+    }
 }
index 85e66740a898562c49c5aa2982d1e063f9643d2d..5bea92a85c85be15e66f610eaccb6ccee6cbf370 100644 (file)
@@ -32,7 +32,7 @@ sendMonospaceMessageActionDescription = This method is used to send a monospace
 sendAttachmentMessageActionLabel = send a plain text message with an attachment
 sendAttachmentMessageActionDescription = This method is used to send a message with an attachment.
 sendMessageActionInputAttachmentLabel = Attachment
-sendMessageActionInputAttachmentDescription = A (local) path to the attachment.
+sendMessageActionInputAttachmentDescription = Local path or URL to the attachment.
 sendMessageActionInputContentTypeLabel = Content Type
 sendMessageActionInputContentTypeDescription = The content type of the attachment. Defaults to "image/jpeg".
 
index 2ef1579529c770aa22fdb7176e872da223ee96fe..fd1822f07164898490f5a2639ee1b9af91167873 100644 (file)
@@ -57,7 +57,7 @@ sendMonospaceMessageActionDescription = Action zum Versenden einer monospace-Nac
 sendAttachmentMessageActionLabel = eine Textnachricht mit Anhang senden
 sendAttachmentMessageActionDescription = Action zum Versenden einer Textnachricht mit Anhang.
 sendMessageActionInputAttachmentLabel = Anhang
-sendMessageActionInputAttachmentDescription = Lokaler Pfad zum Anhang.
+sendMessageActionInputAttachmentDescription = Lokaler Pfad oder URL zum Anhang.
 sendMessageActionInputContentTypeLabel = Content-Type
 sendMessageActionInputContentTypeDescription = Der Content-Type für den Anhang. Default: "image/jpeg".