Skip to content

Commit b50fde6

Browse files
Fix get filename from content disposition in Slicer extension (#1324)
* fix get filename from content disposition Signed-off-by: Janis Vahldiek <[email protected]> * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Signed-off-by: Janis Vahldiek <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 3921f6d commit b50fde6

File tree

1 file changed

+17
-3
lines changed
  • plugins/slicer/MONAILabel/MONAILabelLib

1 file changed

+17
-3
lines changed

plugins/slicer/MONAILabel/MONAILabelLib/client.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import ssl
2020
import tempfile
2121
from pathlib import Path
22-
from urllib.parse import quote_plus, urlparse
22+
from urllib.parse import quote_plus, unquote, urlparse
2323

2424
import requests
2525

@@ -170,9 +170,11 @@ def download_label(self, label_id, tag):
170170
MONAILabelError.SERVER_ERROR, f"Status: {status}; Response: {response}", status, response
171171
)
172172

173-
if not headers.get("content-disposition"):
173+
content_disposition = headers.get("content-disposition")
174+
175+
if not content_disposition:
174176
logging.warning("Filename not found. Fall back to no loaded labels")
175-
file_name = re.findall('filename="(.+)"', headers.get("content-disposition"))[0]
177+
file_name = MONAILabelUtils.get_filename(content_disposition)
176178

177179
file_ext = "".join(Path(file_name).suffixes)
178180
local_filename = tempfile.NamedTemporaryFile(dir=self._tmpdir, suffix=file_ext).name
@@ -432,3 +434,15 @@ def parse_multipart(fp, headers):
432434
@staticmethod
433435
def urllib_quote_plus(s):
434436
return quote_plus(s)
437+
438+
@staticmethod
439+
def get_filename(content_disposition):
440+
file_name = re.findall(r"filename\*=([^;]+)", content_disposition, flags=re.IGNORECASE)
441+
if not file_name:
442+
file_name = re.findall('filename="(.+)"', content_disposition, flags=re.IGNORECASE)
443+
if "utf-8''" in file_name[0].lower():
444+
file_name = re.sub("utf-8''", "", file_name[0], flags=re.IGNORECASE)
445+
file_name = unquote(file_name)
446+
else:
447+
file_name = file_name[0]
448+
return file_name

0 commit comments

Comments
 (0)