mirror of
https://github.com/home-assistant/core.git
synced 2025-07-18 02:37:08 +00:00
Added capability to pass a filename to the downloader component (#10059)
* Added capability to pass the filename to the downloader component * Simplified filename conditions
This commit is contained in:
parent
5c168ab551
commit
f060dcc0aa
@ -17,6 +17,7 @@ from homeassistant.util import sanitize_filename
|
|||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
ATTR_FILENAME = 'filename'
|
||||||
ATTR_SUBDIR = 'subdir'
|
ATTR_SUBDIR = 'subdir'
|
||||||
ATTR_URL = 'url'
|
ATTR_URL = 'url'
|
||||||
|
|
||||||
@ -29,6 +30,7 @@ SERVICE_DOWNLOAD_FILE = 'download_file'
|
|||||||
SERVICE_DOWNLOAD_FILE_SCHEMA = vol.Schema({
|
SERVICE_DOWNLOAD_FILE_SCHEMA = vol.Schema({
|
||||||
vol.Required(ATTR_URL): cv.url,
|
vol.Required(ATTR_URL): cv.url,
|
||||||
vol.Optional(ATTR_SUBDIR): cv.string,
|
vol.Optional(ATTR_SUBDIR): cv.string,
|
||||||
|
vol.Optional(ATTR_FILENAME): cv.string,
|
||||||
})
|
})
|
||||||
|
|
||||||
CONFIG_SCHEMA = vol.Schema({
|
CONFIG_SCHEMA = vol.Schema({
|
||||||
@ -62,6 +64,8 @@ def setup(hass, config):
|
|||||||
|
|
||||||
subdir = service.data.get(ATTR_SUBDIR)
|
subdir = service.data.get(ATTR_SUBDIR)
|
||||||
|
|
||||||
|
filename = service.data.get(ATTR_FILENAME)
|
||||||
|
|
||||||
if subdir:
|
if subdir:
|
||||||
subdir = sanitize_filename(subdir)
|
subdir = sanitize_filename(subdir)
|
||||||
|
|
||||||
@ -70,9 +74,9 @@ def setup(hass, config):
|
|||||||
req = requests.get(url, stream=True, timeout=10)
|
req = requests.get(url, stream=True, timeout=10)
|
||||||
|
|
||||||
if req.status_code == 200:
|
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+)",
|
match = re.findall(r"filename=(\S+)",
|
||||||
req.headers['content-disposition'])
|
req.headers['content-disposition'])
|
||||||
|
|
||||||
@ -80,8 +84,7 @@ def setup(hass, config):
|
|||||||
filename = match[0].strip("'\" ")
|
filename = match[0].strip("'\" ")
|
||||||
|
|
||||||
if not filename:
|
if not filename:
|
||||||
filename = os.path.basename(
|
filename = os.path.basename(url).strip()
|
||||||
url).strip()
|
|
||||||
|
|
||||||
if not filename:
|
if not filename:
|
||||||
filename = 'ha_download'
|
filename = 'ha_download'
|
||||||
|
Loading…
x
Reference in New Issue
Block a user