From aeb04bcf2fc381f6f6b71f992100fc08822c9ee1 Mon Sep 17 00:00:00 2001 From: Ross Date: Tue, 28 Apr 2026 15:01:47 +0100 Subject: [PATCH] Refactor URL normalization to ensure valid wadouri: prefix for DICOM entries Co-authored-by: Copilot --- dicom-viewer/src/main.tsx | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/dicom-viewer/src/main.tsx b/dicom-viewer/src/main.tsx index 8280e2b..fd556fe 100644 --- a/dicom-viewer/src/main.tsx +++ b/dicom-viewer/src/main.tsx @@ -6,9 +6,28 @@ import Nifti from './Nifti.tsx' import './index.css' import { createImageIds, availableImageIds } from "./lib/createImageIds.ts" -// Helper to convert URLs to wadouri -const toWadouri = (arr: string[]) => - arr.map(url => url.startsWith("wadouri:") ? url : `wadouri:${url}`); +// Normalize incoming image ids/URLs and ensure DICOM entries use a valid wadouri: prefix. +const normalizeImageId = (value: string) => { + const trimmed = value.trim(); + if (!trimmed) return trimmed; + + // Keep non-wadouri schemes untouched. + if (trimmed.startsWith("external:") || trimmed.startsWith("wadors:")) { + return trimmed; + } + + // Fix malformed values like "wadouri::https://..." by removing extra leading colons. + if (trimmed.startsWith("wadouri:")) { + const rest = trimmed.slice("wadouri:".length).replace(/^:+/, ""); + return `wadouri:${rest}`; + } + + // Also handle malformed absolute URLs such as ":https://...". + const sanitized = trimmed.replace(/^:+/, ""); + return `wadouri:${sanitized}`; +}; + +const toWadouri = (arr: string[]) => arr.map(normalizeImageId); // Normalize a simple array -> descriptor object const toDescriptor = (arr: string[] | undefined) => {