From 394e0a2a6dced88204d21fea6ea4bf4f8f82068f Mon Sep 17 00:00:00 2001 From: Christoph Weitkamp Date: Tue, 20 Jul 2021 14:20:39 +0200 Subject: [PATCH] Add support for data URI scheme Signed-off-by: Christoph Weitkamp --- .../org.openhab.binding.pushover/README.md | 12 +++- .../connection/PushoverMessageBuilder.java | 58 ++++++++++++------- 2 files changed, 47 insertions(+), 23 deletions(-) diff --git a/bundles/org.openhab.binding.pushover/README.md b/bundles/org.openhab.binding.pushover/README.md index cda14becd3a43..312f9de43d6be 100644 --- a/bundles/org.openhab.binding.pushover/README.md +++ b/bundles/org.openhab.binding.pushover/README.md @@ -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 or URL 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 World!", "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:[][;base64],", 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 diff --git a/bundles/org.openhab.binding.pushover/src/main/java/org/openhab/binding/pushover/internal/connection/PushoverMessageBuilder.java b/bundles/org.openhab.binding.pushover/src/main/java/org/openhab/binding/pushover/internal/connection/PushoverMessageBuilder.java index 5c77316e31273..e00c61bdb68a2 100644 --- a/bundles/org.openhab.binding.pushover/src/main/java/org/openhab/binding/pushover/internal/connection/PushoverMessageBuilder.java +++ b/bundles/org.openhab.binding.pushover/src/main/java/org/openhab/binding/pushover/internal/connection/PushoverMessageBuilder.java @@ -79,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; @@ -242,36 +242,26 @@ public ContentProvider build() throws PushoverCommunicationException { } if (attachment != null) { - if (attachment.startsWith("http")) { - RawType content = HttpUtil.downloadImage(attachment, 10000); - if (content == null) { + 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)); } - try { - Path tmpFile = Files.createTempFile("pushover-", ".tmp"); - Files.write(tmpFile, content.getBytes()); - body.addFilePart(MESSAGE_KEY_ATTACHMENT, tmpFile.toFile().getName(), - new PathContentProvider(contentType, tmpFile), 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); - } + addFilePart(createTempFile(rawImage.getBytes()), + contentType == null ? rawImage.getMimeType() : contentType); + } else if (localAttachment.startsWith("data:")) { // support data URI scheme + RawType rawImage = RawType.valueOf(localAttachment); + addFilePart(createTempFile(rawImage.getBytes()), + contentType == null ? rawImage.getMimeType() : contentType); } 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)); } - 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); - } + addFilePart(file.toPath(), contentType); } } @@ -283,4 +273,28 @@ public ContentProvider build() throws PushoverCommunicationException { 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); + } + } }