From beb678e259ae4e052a015f2e6e7bc5533ddb2d28 Mon Sep 17 00:00:00 2001 From: Cyro Date: Thu, 2 May 2019 01:00:17 +0200 Subject: [PATCH] Move I/O to executor thread pool (#23589) * Move I/O to executor thread pool * Check if image is in whitelist_external_dir * Move import to top of file * Fix bad indentation --- homeassistant/components/discord/notify.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/discord/notify.py b/homeassistant/components/discord/notify.py index 42f5408614..5a9cb77877 100644 --- a/homeassistant/components/discord/notify.py +++ b/homeassistant/components/discord/notify.py @@ -1,5 +1,6 @@ """Discord platform for notify component.""" import logging +import os.path import voluptuous as vol @@ -33,6 +34,13 @@ class DiscordNotificationService(BaseNotificationService): self.token = token self.hass = hass + def file_exists(self, filename): + """Check if a file exists on disk and is in authorized path.""" + if not self.hass.config.is_allowed_path(filename): + return False + + return os.path.isfile(filename) + async def async_send_message(self, message, **kwargs): """Login to Discord, send message to channel(s) and log out.""" import discord @@ -49,11 +57,14 @@ class DiscordNotificationService(BaseNotificationService): data = kwargs.get(ATTR_DATA) if ATTR_IMAGES in data: - import os.path images = list() for image in data.get(ATTR_IMAGES): - if os.path.isfile(image): + image_exists = await self.hass.async_add_executor_job( + self.file_exists, + image) + + if image_exists: images.append(image) else: _LOGGER.warning("Image not found: %s", image)