Skip to content

Commit a382d7e

Browse files
authored
Merge pull request #886 from adfinis/fix-filename
fix(merge): fix extension in filename when merging without convert
2 parents 6405fd3 + 9676dab commit a382d7e

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

document_merge_service/api/tests/test_template.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import io
22
import json
33
import os
4+
import re
45
from collections import namedtuple
56

67
import openpyxl
@@ -17,6 +18,10 @@
1718
from .. import models, serializers
1819

1920

21+
def get_filename_from_response(response):
22+
return re.search(r'filename="(.*)"', response["Content-Disposition"])[1]
23+
24+
2025
@pytest.mark.parametrize("template__description", ["test description"])
2126
@pytest.mark.parametrize(
2227
"query_params,size",
@@ -628,6 +633,7 @@ def test_template_merge_docx(
628633
response.get("content-type")
629634
== "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
630635
)
636+
assert get_filename_from_response(response) == f"{template.slug}.docx"
631637

632638
docx = Document(io.BytesIO(response.getvalue()))
633639
xml = etree.tostring(docx._element.body, encoding="unicode", pretty_print=True)
@@ -735,7 +741,7 @@ def test_template_merge_as_pdf(
735741
)
736742
assert response.status_code == status.HTTP_200_OK
737743
assert response["Content-Type"] == "application/pdf"
738-
assert f"{template.pk}.pdf" in response["Content-Disposition"]
744+
assert get_filename_from_response(response) == f"{template.slug}.pdf"
739745
assert response.content[0:4] == b"%PDF"
740746

741747

document_merge_service/api/views.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ def merge(self, request, pk=None):
5858
if convert:
5959
response = FileConverter.convert(response.content, convert)
6060

61-
filename = f"{template.slug}.{convert}"
61+
extension = mimetypes.guess_extension(response.headers["Content-Type"])
62+
filename = f"{template.slug}{extension}"
6263
response["Content-Disposition"] = f'attachment; filename="{filename}"'
6364
return response
6465

0 commit comments

Comments
 (0)