Skip to content

Commit

Permalink
fix(merge): fix extension in filename when merging without convert
Browse files Browse the repository at this point in the history
  • Loading branch information
anehx committed Nov 14, 2024
1 parent 6405fd3 commit 9676dab
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
8 changes: 7 additions & 1 deletion document_merge_service/api/tests/test_template.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import io
import json
import os
import re
from collections import namedtuple

import openpyxl
Expand All @@ -17,6 +18,10 @@
from .. import models, serializers


def get_filename_from_response(response):
return re.search(r'filename="(.*)"', response["Content-Disposition"])[1]


@pytest.mark.parametrize("template__description", ["test description"])
@pytest.mark.parametrize(
"query_params,size",
Expand Down Expand Up @@ -628,6 +633,7 @@ def test_template_merge_docx(
response.get("content-type")
== "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
)
assert get_filename_from_response(response) == f"{template.slug}.docx"

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


Expand Down
3 changes: 2 additions & 1 deletion document_merge_service/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ def merge(self, request, pk=None):
if convert:
response = FileConverter.convert(response.content, convert)

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

Expand Down

0 comments on commit 9676dab

Please # to comment.