From f060dcc0aa385e8176e320d9f87988311efb7c22 Mon Sep 17 00:00:00 2001 From: Marcelo Moreira de Mello Date: Fri, 27 Oct 2017 16:50:02 -0400 Subject: [PATCH] Added capability to pass a filename to the downloader component (#10059) * Added capability to pass the filename to the downloader component * Simplified filename conditions --- homeassistant/components/downloader.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/downloader.py b/homeassistant/components/downloader.py index 0450ba175ee..07f432a5218 100644 --- a/homeassistant/components/downloader.py +++ b/homeassistant/components/downloader.py @@ -17,6 +17,7 @@ from homeassistant.util import sanitize_filename _LOGGER = logging.getLogger(__name__) +ATTR_FILENAME = 'filename' ATTR_SUBDIR = 'subdir' ATTR_URL = 'url' @@ -29,6 +30,7 @@ SERVICE_DOWNLOAD_FILE = 'download_file' SERVICE_DOWNLOAD_FILE_SCHEMA = vol.Schema({ vol.Required(ATTR_URL): cv.url, vol.Optional(ATTR_SUBDIR): cv.string, + vol.Optional(ATTR_FILENAME): cv.string, }) CONFIG_SCHEMA = vol.Schema({ @@ -62,6 +64,8 @@ def setup(hass, config): subdir = service.data.get(ATTR_SUBDIR) + filename = service.data.get(ATTR_FILENAME) + if subdir: subdir = sanitize_filename(subdir) @@ -70,9 +74,9 @@ def setup(hass, config): req = requests.get(url, stream=True, timeout=10) if req.status_code == 200: - filename = None - if 'content-disposition' in req.headers: + if filename is None and \ + 'content-disposition' in req.headers: match = re.findall(r"filename=(\S+)", req.headers['content-disposition']) @@ -80,8 +84,7 @@ def setup(hass, config): filename = match[0].strip("'\" ") if not filename: - filename = os.path.basename( - url).strip() + filename = os.path.basename(url).strip() if not filename: filename = 'ha_download'