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

Add support for downloading DICOMweb files #1429

Merged
merged 12 commits into from
Jan 22, 2024
Prev Previous commit
Set file size to be "None"
This prevents a misleading file size of 0 bytes being displayed in
girder.

This is working without issues so far.

Signed-off-by: Patrick Avery <patrick.avery@kitware.com>
psavery committed Jan 22, 2024
commit 15476f6d7a836d3d6a4e344d0302fcdfaa685a01
Original file line number Diff line number Diff line change
@@ -110,8 +110,8 @@

if offset != 0 or endByte is not None:
# FIXME: implement range requests
msg = 'Range requests are not yet implemented'
raise NotImplementedError(msg)

Check warning on line 114 in sources/dicom/large_image_source_dicom/assetstore/dicomweb_assetstore_adapter.py

Codecov / codecov/patch

sources/dicom/large_image_source_dicom/assetstore/dicomweb_assetstore_adapter.py#L113-L114

Added lines #L113 - L114 were not covered by tests

from dicomweb_client.web import _Transaction

@@ -181,8 +181,8 @@
# Split the content-type to find the media type and boundary.
media_type, boundary = self._extract_media_type_and_boundary(response)
if media_type.lower() != 'multipart/related':
msg = f'Unexpected media type: "{media_type}". Expected "multipart/related".'
raise ValueError(msg)

Check warning on line 185 in sources/dicom/large_image_source_dicom/assetstore/dicomweb_assetstore_adapter.py

Codecov / codecov/patch

sources/dicom/large_image_source_dicom/assetstore/dicomweb_assetstore_adapter.py#L184-L185

Added lines #L184 - L185 were not covered by tests

# Ensure we have the multipart/related boundary.
# The beginning boundary and end boundary look slightly different (in my
@@ -191,9 +191,9 @@
# since it is before the message body. An end boundary might look like this:
# \r\n--50d7ccd118978542c422543a7156abfce929e7615bc024e533c85801cd77--
if boundary is None:
content_type = response.headers['content-type']
msg = f'Failed to locate boundary in content-type: {content_type}'
raise ValueError(msg)

Check warning on line 196 in sources/dicom/large_image_source_dicom/assetstore/dicomweb_assetstore_adapter.py

Codecov / codecov/patch

sources/dicom/large_image_source_dicom/assetstore/dicomweb_assetstore_adapter.py#L194-L196

Added lines #L194 - L196 were not covered by tests

# Both dicomweb-client and requests-toolbelt check for
# the ending boundary exactly like so:
@@ -226,8 +226,8 @@
break

if not header_found:
msg = 'Failed to find header in response content'
raise ValueError(msg)

Check warning on line 230 in sources/dicom/large_image_source_dicom/assetstore/dicomweb_assetstore_adapter.py

Codecov / codecov/patch

sources/dicom/large_image_source_dicom/assetstore/dicomweb_assetstore_adapter.py#L229-L230

Added lines #L229 - L230 were not covered by tests

# Now the header has been finished. Stream the data until
# we encounter the ending boundary or finish the data.
@@ -236,17 +236,17 @@
# Ensure the chunk is large enough to contain the ending_size - 1, so
# we can be sure the ending won't be split across more than 2 chunks.
while len(chunk) < ending_size - 1:
try:
chunk += next(iterator)
except StopIteration:
break

Check warning on line 242 in sources/dicom/large_image_source_dicom/assetstore/dicomweb_assetstore_adapter.py

Codecov / codecov/patch

sources/dicom/large_image_source_dicom/assetstore/dicomweb_assetstore_adapter.py#L239-L242

Added lines #L239 - L242 were not covered by tests

# Check if the ending is split between the previous and current chunks.
if ending in prev_chunk + chunk[:ending_size - 1]:
# We found the ending! Remove the ending boundary and return.
data = prev_chunk + chunk[:ending_size - 1]
yield data.split(ending, maxsplit=1)[0]
return

Check warning on line 249 in sources/dicom/large_image_source_dicom/assetstore/dicomweb_assetstore_adapter.py

Codecov / codecov/patch

sources/dicom/large_image_source_dicom/assetstore/dicomweb_assetstore_adapter.py#L247-L249

Added lines #L247 - L249 were not covered by tests

if prev_chunk:
yield prev_chunk
@@ -261,7 +261,7 @@
return

# We should have encountered the ending earlier and returned
msg = 'Failed to find ending boundary in response content'

Check warning on line 264 in sources/dicom/large_image_source_dicom/assetstore/dicomweb_assetstore_adapter.py

Codecov / codecov/patch

sources/dicom/large_image_source_dicom/assetstore/dicomweb_assetstore_adapter.py#L264

Added line #L264 was not covered by tests
raise ValueError(msg)

def importData(self, parent, parentType, params, progress, user, **kwargs):
@@ -356,7 +356,7 @@
reuseExisting=True,
assetstore=self.assetstore,
mimeType='application/dicom',
size=0,
size=None,
saveFile=False,
)
file['dicom_uids'] = {