Skip to content

Commit

Permalink
fix(convert): handle file names with multiple dots
Browse files Browse the repository at this point in the history
Ensure files with names containing dots in addition
to the dot before the extension are handled correctly.
The converted file name should correspond to the
original file name replacing only the extension.
  • Loading branch information
luytena committed Oct 23, 2023
1 parent 28ee2e3 commit 3ae90f1
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
Binary file not shown.
25 changes: 21 additions & 4 deletions document_merge_service/api/tests/test_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,37 @@


@pytest.mark.parametrize(
"target_format,response_content_type",
"filename,target_filename,target_format,response_content_type",
[
("pdf", "application/pdf"),
(
"docx-template.docx",
"docx-template.pdf",
"pdf",
"application/pdf",
),
(
"2023.test.test.docx-template.docx",
"2023.test.test.docx-template.pdf",
"pdf",
"application/pdf",
),
],
)
def test_convert(db, client, target_format, response_content_type):
def test_convert(
db, client, filename, target_filename, target_format, response_content_type
):
url = reverse("convert")
file_to_convert = django_file("docx-template.docx")
file_to_convert = django_file(filename)

data = {"file": file_to_convert.file, "target_format": target_format}
response = client.post(url, data=data, format="multipart")

assert response.status_code == status.HTTP_200_OK
assert response.headers.get("Content-Type") == response_content_type
assert (
response.headers.get("Content-Disposition")
== f'attachment; filename="{target_filename}"'
)


def test_incorrect_file_type(db, client):
Expand Down
3 changes: 2 additions & 1 deletion document_merge_service/api/views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import mimetypes
from os.path import splitext

import jinja2
from django.http import HttpResponse
Expand Down Expand Up @@ -102,6 +103,6 @@ def post(self, request, **kwargs):

response = FileConverter.convert(file.read(), target_format)

filename = f"{file.name.split('.')[0]}.{target_format}"
filename = f"{splitext(file.name)[0]}.{target_format}"
response["Content-Disposition"] = f'attachment; filename="{filename}"'
return response

0 comments on commit 3ae90f1

Please sign in to comment.