kopia lustrzana https://github.com/simonw/datasette
Fixed content-disposition header on DB download, closes #909
rodzic
c5c12a797f
commit
0748a65a22
|
@ -251,7 +251,7 @@ async def asgi_send_file(
|
||||||
):
|
):
|
||||||
headers = {}
|
headers = {}
|
||||||
if filename:
|
if filename:
|
||||||
headers["Content-Disposition"] = 'attachment; filename="{}"'.format(filename)
|
headers["content-disposition"] = 'attachment; filename="{}"'.format(filename)
|
||||||
first = True
|
first = True
|
||||||
headers["content-length"] = str((await aiofiles.os.stat(str(filepath))).st_size)
|
headers["content-length"] = str((await aiofiles.os.stat(str(filepath))).st_size)
|
||||||
async with aiofiles.open(str(filepath), mode="rb") as fp:
|
async with aiofiles.open(str(filepath), mode="rb") as fp:
|
||||||
|
@ -402,4 +402,6 @@ class AsgiFileDownload:
|
||||||
self.content_type = content_type
|
self.content_type = content_type
|
||||||
|
|
||||||
async def asgi_send(self, send):
|
async def asgi_send(self, send):
|
||||||
return await asgi_send_file(send, self.filepath, content_type=self.content_type)
|
return await asgi_send_file(
|
||||||
|
send, self.filepath, filename=self.filename, content_type=self.content_type
|
||||||
|
)
|
||||||
|
|
|
@ -337,7 +337,7 @@ class DataView(BaseView):
|
||||||
disposition = 'attachment; filename="{}.csv"'.format(
|
disposition = 'attachment; filename="{}.csv"'.format(
|
||||||
kwargs.get("table", database)
|
kwargs.get("table", database)
|
||||||
)
|
)
|
||||||
headers["Content-Disposition"] = disposition
|
headers["content-disposition"] = disposition
|
||||||
|
|
||||||
return AsgiStream(stream_fn, headers=headers, content_type=content_type)
|
return AsgiStream(stream_fn, headers=headers, content_type=content_type)
|
||||||
|
|
||||||
|
|
|
@ -92,7 +92,7 @@ def test_table_csv_download(app_client):
|
||||||
assert response.status == 200
|
assert response.status == 200
|
||||||
assert "text/csv; charset=utf-8" == response.headers["content-type"]
|
assert "text/csv; charset=utf-8" == response.headers["content-type"]
|
||||||
expected_disposition = 'attachment; filename="simple_primary_key.csv"'
|
expected_disposition = 'attachment; filename="simple_primary_key.csv"'
|
||||||
assert expected_disposition == response.headers["Content-Disposition"]
|
assert expected_disposition == response.headers["content-disposition"]
|
||||||
|
|
||||||
|
|
||||||
def test_csv_with_non_ascii_characters(app_client):
|
def test_csv_with_non_ascii_characters(app_client):
|
||||||
|
|
|
@ -913,6 +913,11 @@ def test_database_download_for_immutable():
|
||||||
content_length = download_response.headers["content-length"]
|
content_length = download_response.headers["content-length"]
|
||||||
assert content_length.isdigit()
|
assert content_length.isdigit()
|
||||||
assert int(content_length) > 100
|
assert int(content_length) > 100
|
||||||
|
assert "content-disposition" in download_response.headers
|
||||||
|
assert (
|
||||||
|
download_response.headers["content-disposition"]
|
||||||
|
== 'attachment; filename="fixtures.db"'
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def test_database_download_disallowed_for_mutable(app_client):
|
def test_database_download_disallowed_for_mutable(app_client):
|
||||||
|
|
Ładowanie…
Reference in New Issue