Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 | import { HeadersInterface } from '@ohif/core/src/types/RequestHeaders';
type RetrieveApi = {
directURL: (params: unknown) => unknown;
};
type RenderedURLConfig = {
wadoRoot?: string;
};
type UserAuthenticationService = {
handleUnauthenticated?: () => unknown;
};
type GetRenderedURLDeps = {
config: RenderedURLConfig;
getAuthorizationHeader: () => HeadersInterface;
retrieve: RetrieveApi;
userAuthenticationService?: UserAuthenticationService;
};
type RenderedURLOptions = {
signal?: AbortSignal;
};
type FetchRenderedURLOptions = RenderedURLOptions & {
url?: string | null;
wadoRoot?: string;
headers?: HeadersInterface;
userAuthenticationService?: UserAuthenticationService;
};
export type RenderedURLResult = {
url: string | null;
revoke?: () => void;
};
export function getRenderedURL({
config,
getAuthorizationHeader,
retrieve,
userAuthenticationService,
}: GetRenderedURLDeps) {
return async function renderedURL(
params: unknown,
options: RenderedURLOptions = {}
): Promise<RenderedURLResult> {
const resolvedUrl = (await retrieve.directURL(params)) as string | undefined | null;
return fetchRenderedURL({
url: resolvedUrl,
wadoRoot: config.wadoRoot,
headers: getAuthorizationHeader(),
signal: options.signal,
userAuthenticationService,
});
};
}
export async function fetchRenderedURL({
url,
wadoRoot,
headers,
signal,
userAuthenticationService,
}: FetchRenderedURLOptions): Promise<RenderedURLResult> {
Iif (!url || signal?.aborted) {
return { url: null };
}
Iif (!headers?.Authorization || !isTrustedWadoURL(url, wadoRoot)) {
return { url };
}
try {
const response = await fetch(url, {
method: 'GET',
headers: headers as Record<string, string>,
signal,
});
Iif (!response.ok) {
Iif (response.status === 401 || response.status === 403) {
userAuthenticationService?.handleUnauthenticated?.();
}
console.warn(`rendered media fetch failed with status ${response.status}`);
return { url: null };
}
const blob = await response.blob();
const objectUrl = URL.createObjectURL(blob);
Iif (signal?.aborted) {
URL.revokeObjectURL(objectUrl);
return { url: null };
}
return {
url: objectUrl,
revoke: createRevokeOnce(objectUrl),
};
} catch (error) {
Iif ((error as { name?: string })?.name === 'AbortError') {
return { url: null };
}
console.warn('rendered media fetch failed', error);
return { url: null };
}
}
export function isTrustedWadoURL(url: string, wadoRoot?: string): boolean {
Iif (!wadoRoot) {
return false;
}
try {
const parsedUrl = new URL(url, window.location.href);
const parsedWadoRoot = new URL(wadoRoot, window.location.href);
const isHttpUrl = parsedUrl.protocol === 'http:' || parsedUrl.protocol === 'https:';
const isHttpWadoRoot =
parsedWadoRoot.protocol === 'http:' || parsedWadoRoot.protocol === 'https:';
return isHttpUrl && isHttpWadoRoot && parsedUrl.origin === parsedWadoRoot.origin;
} catch {
return false;
}
}
function createRevokeOnce(objectUrl: string) {
let revoked = false;
return () => {
Iif (revoked) {
return;
}
URL.revokeObjectURL(objectUrl);
revoked = true;
};
}
|