-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Microscopy bulkdata and image retrieve (#3894)
- Loading branch information
1 parent
7014e6c
commit 7fac49b
Showing
6 changed files
with
187 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
extensions/default/src/DicomWebDataSource/utils/findIndexOfString.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
function checkToken(token, data, dataOffset): boolean { | ||
if (dataOffset + token.length > data.length) { | ||
return false; | ||
} | ||
|
||
let endIndex = dataOffset; | ||
|
||
for (let i = 0; i < token.length; i++) { | ||
if (token[i] !== data[endIndex++]) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
function stringToUint8Array(str: string): Uint8Array { | ||
const uint = new Uint8Array(str.length); | ||
|
||
for (let i = 0, j = str.length; i < j; i++) { | ||
uint[i] = str.charCodeAt(i); | ||
} | ||
|
||
return uint; | ||
} | ||
|
||
function findIndexOfString( | ||
data: Uint8Array, | ||
str: string, | ||
offset?: number | ||
): number { | ||
offset = offset || 0; | ||
|
||
const token = stringToUint8Array(str); | ||
|
||
for (let i = offset; i < data.length; i++) { | ||
if (token[0] === data[i]) { | ||
// console.log('match @', i); | ||
if (checkToken(token, data, i)) { | ||
return i; | ||
} | ||
} | ||
} | ||
|
||
return -1; | ||
} | ||
export default findIndexOfString; |
70 changes: 70 additions & 0 deletions
70
extensions/default/src/DicomWebDataSource/utils/fixMultipart.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import findIndexOfString from './findIndexOfString'; | ||
|
||
/** | ||
* Fix multipart data coming back from the retrieve bulkdata request, but | ||
* incorrectly tagged as application/octet-stream. Some servers don't handle | ||
* the response type correctly, and this method is relatively robust about | ||
* detecting multipart data correctly. It will only extract one value. | ||
*/ | ||
export default function fixMultipart(arrayData) { | ||
const data = new Uint8Array(arrayData[0]); | ||
// Don't know the exact minimum length, but it is at least 25 to encode multipart | ||
if (data.length < 25) { | ||
return arrayData; | ||
} | ||
const dashIndex = findIndexOfString(data, '--'); | ||
if (dashIndex > 6) { | ||
return arrayData; | ||
} | ||
const tokenIndex = findIndexOfString(data, '\r\n\r\n', dashIndex); | ||
if (tokenIndex > 512) { | ||
// Allow for 512 characters in the header - there is no apriori limit, but | ||
// this seems ok for now as we only expect it to have content type in it. | ||
return arrayData; | ||
} | ||
const header = uint8ArrayToString(data, 0, tokenIndex); | ||
// Now find the boundary marker | ||
const responseHeaders = header.split('\r\n'); | ||
const boundary = findBoundary(responseHeaders); | ||
|
||
if (!boundary) { | ||
return arrayData; | ||
} | ||
// Start of actual data is 4 characters after the token | ||
const offset = tokenIndex + 4; | ||
|
||
const endIndex = findIndexOfString(data, boundary, offset); | ||
if (endIndex === -1) { | ||
return arrayData; | ||
} | ||
|
||
return [data.slice(offset, endIndex - 2).buffer]; | ||
} | ||
|
||
export function findBoundary(header: string[]): string { | ||
for (let i = 0; i < header.length; i++) { | ||
if (header[i].substr(0, 2) === '--') { | ||
return header[i]; | ||
} | ||
} | ||
} | ||
|
||
export function findContentType(header: string[]): string { | ||
for (let i = 0; i < header.length; i++) { | ||
if (header[i].substr(0, 13) === 'Content-Type:') { | ||
return header[i].substr(13).trim(); | ||
} | ||
} | ||
} | ||
|
||
export function uint8ArrayToString(data, offset, length) { | ||
offset = offset || 0; | ||
length = length || data.length - offset; | ||
let str = ''; | ||
|
||
for (let i = offset; i < offset + length; i++) { | ||
str += String.fromCharCode(data[i]); | ||
} | ||
|
||
return str; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters