Add content-disposition header to snapshot download (#2116)

* Add content-disposition header to snapshot download

* compile it
This commit is contained in:
Joakim Sørensen 2020-10-12 11:11:32 +02:00 committed by GitHub
parent ba576d8748
commit ccb8e5fe06
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2,9 +2,11 @@
import asyncio import asyncio
import logging import logging
from pathlib import Path from pathlib import Path
import re
from tempfile import TemporaryDirectory from tempfile import TemporaryDirectory
from aiohttp import web from aiohttp import web
from aiohttp.hdrs import CONTENT_DISPOSITION
import voluptuous as vol import voluptuous as vol
from ..const import ( from ..const import (
@ -30,6 +32,7 @@ from .utils import api_process, api_validate
_LOGGER: logging.Logger = logging.getLogger(__name__) _LOGGER: logging.Logger = logging.getLogger(__name__)
RE_SLUGIFY_NAME = re.compile(r"[^A-Za-z0-9]+")
# pylint: disable=no-value-for-parameter # pylint: disable=no-value-for-parameter
SCHEMA_RESTORE_PARTIAL = vol.Schema( SCHEMA_RESTORE_PARTIAL = vol.Schema(
@ -175,6 +178,9 @@ class APISnapshots(CoreSysAttributes):
_LOGGER.info("Download snapshot %s", snapshot.slug) _LOGGER.info("Download snapshot %s", snapshot.slug)
response = web.FileResponse(snapshot.tarfile) response = web.FileResponse(snapshot.tarfile)
response.content_type = CONTENT_TYPE_TAR response.content_type = CONTENT_TYPE_TAR
response.headers[
CONTENT_DISPOSITION
] = f"attachment; filename={RE_SLUGIFY_NAME.sub('_', snapshot.name)}.tar"
return response return response
@api_process @api_process