Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(convert): handle file names with multiple dots #674

Merged
merged 1 commit into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading