From 38b496102ff449d9f6ff513e7d8a5bc248693357 Mon Sep 17 00:00:00 2001 From: "c.lamboo" Date: Tue, 12 Nov 2024 21:33:01 +0100 Subject: [PATCH 1/2] Make url schemes more robust against alternate formats CURA-12282 --- cura/CuraApplication.py | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/cura/CuraApplication.py b/cura/CuraApplication.py index 4d2f4f964b..94f6a17f97 100755 --- a/cura/CuraApplication.py +++ b/cura/CuraApplication.py @@ -1895,23 +1895,20 @@ class CuraApplication(QtApplication): def on_finish(response): content_disposition_header_key = QByteArray("content-disposition".encode()) - if not response.hasRawHeader(content_disposition_header_key): - Logger.log("w", "Could not find Content-Disposition header in response from {0}".format( - model_url.url())) - # Use the last part of the url as the filename, and assume it is an STL file - filename = model_url.path().split("/")[-1] + ".stl" - else: + filename = model_url.path().split("/")[-1] + ".stl" + + if response.hasRawHeader(content_disposition_header_key): # content_disposition is in the format # ``` - # content_disposition attachment; "filename=[FILENAME]" + # content_disposition attachment; filename="[FILENAME]" # ``` # Use a regex to extract the filename content_disposition = str(response.rawHeader(content_disposition_header_key).data(), encoding='utf-8') - content_disposition_match = re.match(r'attachment; filename="(?P.*)"', + content_disposition_match = re.match(r'attachment; filename="?(?P.*)"?', content_disposition) - assert content_disposition_match is not None - filename = content_disposition_match.group("filename") + if content_disposition_match is not None: + filename = content_disposition_match.group("filename") tmp = tempfile.NamedTemporaryFile(suffix=filename, delete=False) with open(tmp.name, "wb") as f: From 7507ebad6ab41120f90ad434ce25914cd7e89464 Mon Sep 17 00:00:00 2001 From: "c.lamboo" Date: Tue, 12 Nov 2024 23:09:23 +0100 Subject: [PATCH 2/2] Fix for url-schemes with quotes Last quote would not be matched due to the optional last quote in the regex, resulting in an `.stl"` extension CURA-12282 --- cura/CuraApplication.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cura/CuraApplication.py b/cura/CuraApplication.py index 94f6a17f97..97c4c7e2fc 100755 --- a/cura/CuraApplication.py +++ b/cura/CuraApplication.py @@ -1905,10 +1905,10 @@ class CuraApplication(QtApplication): # Use a regex to extract the filename content_disposition = str(response.rawHeader(content_disposition_header_key).data(), encoding='utf-8') - content_disposition_match = re.match(r'attachment; filename="?(?P.*)"?', + content_disposition_match = re.match(r'attachment; filename=(?P.*)', content_disposition) if content_disposition_match is not None: - filename = content_disposition_match.group("filename") + filename = content_disposition_match.group("filename").strip("\"") tmp = tempfile.NamedTemporaryFile(suffix=filename, delete=False) with open(tmp.name, "wb") as f: