private IMultipartReader SetupMultipartReader(Func <CallInfo, MultipartBodyPart> returnThis, params Func <CallInfo, MultipartBodyPart>[] returnThese) { IMultipartReader multipartReader = Substitute.For <IMultipartReader>(); multipartReader.ReadNextBodyPartAsync(DefaultCancellationToken).Returns(returnThis, returnThese); _multipartReaderFactory.Create(DefaultContentType, _stream) .Returns(multipartReader); return(multipartReader); }
/// <inheritdoc /> public async Task <IReadOnlyList <IDicomInstanceEntry> > ReadAsync(string contentType, Stream stream, CancellationToken cancellationToken) { EnsureArg.IsNotNullOrWhiteSpace(contentType, nameof(contentType)); EnsureArg.IsNotNull(stream, nameof(stream)); IMultipartReader multipartReader = _multipartReaderFactory.Create(contentType, stream); var dicomInstanceEntries = new List <StreamOriginatedDicomInstanceEntry>(); MultipartBodyPart bodyPart; try { while ((bodyPart = await multipartReader.ReadNextBodyPartAsync(cancellationToken)) != null) { // Check the content type to make sure we can process. if (!KnownContentTypes.ApplicationDicom.Equals(bodyPart.ContentType, StringComparison.OrdinalIgnoreCase)) { // TODO: Currently, we only support application/dicom. Support for metadata + bulkdata is coming. throw new UnsupportedMediaTypeException( string.Format(CultureInfo.InvariantCulture, DicomCoreResource.UnsupportedContentType, bodyPart.ContentType)); } dicomInstanceEntries.Add(new StreamOriginatedDicomInstanceEntry(bodyPart.SeekableStream)); } } catch (Exception) { // Encountered an error while processing, release all resources. IEnumerable <Task> disposeTasks = dicomInstanceEntries.Select(DisposeResourceAsync); await Task.WhenAll(disposeTasks); throw; } return(dicomInstanceEntries); }