diff --git a/modules/brainbrowser/js/brainbrowser.loris.js b/modules/brainbrowser/js/brainbrowser.loris.js index a7a15d0b07f..232fc658f97 100644 --- a/modules/brainbrowser/js/brainbrowser.loris.js +++ b/modules/brainbrowser/js/brainbrowser.loris.js @@ -5,17 +5,17 @@ * @param variable */ function getQueryVariable(variable) { - 'use strict'; - let query = window.location.search.substring(1); - let vars = query.split('&'); - let i; - let pair; - for (i = 0; i < vars.length; i += 1) { - pair = vars[i].split('='); - if (pair[0] === variable) { - return unescape(pair[1]); - } + 'use strict'; + let query = window.location.search.substring(1); + let vars = query.split('&'); + let i; + let pair; + for (i = 0; i < vars.length; i += 1) { + pair = vars[i].split('='); + if (pair[0] === variable) { + return unescape(pair[1]); } + } } @@ -34,6 +34,7 @@ $(function() { BrainBrowser.VolumeViewer.start('brainbrowser', function(viewer) { let loadingDiv = $('#loading'); let mincIDs; + let fileUrls; let mincVolumes = []; let mincFilenames = []; let bboptions = {}; @@ -775,7 +776,8 @@ $(function() { } }); // Should cursors in all panels be synchronized? - mincIDs = getQueryVariable('minc_id'); + mincIDs = getQueryVariable('minc_id') || []; + fileUrls = getQueryVariable('file_url') || []; if (getQueryVariable('overlay') === 'true') { bboptions.overlay = { template: { @@ -809,7 +811,7 @@ $(function() { viewer.setDefaultPanelSize(panelSize, panelSize); fetch( - 'imageinfo?files=' + mincIDs, + 'imageinfo?fileids=' + mincIDs + '&fileurls=' + fileUrls, {credentials: 'same-origin', method: 'GET'} ) .then((resp) => resp.json()) diff --git a/modules/brainbrowser/php/imageinfo.class.inc b/modules/brainbrowser/php/imageinfo.class.inc index 96883dc5ac4..71e56f112e7 100644 --- a/modules/brainbrowser/php/imageinfo.class.inc +++ b/modules/brainbrowser/php/imageinfo.class.inc @@ -32,40 +32,84 @@ class ImageInfo extends \NDB_Page */ public function handle(ServerRequestInterface $request) : ResponseInterface { - $files = $this->_parseFileIDs($request->getQueryParams()['files'] ?? ""); - if (count($files) < 1) { - return new \LORIS\Http\Response\JSON\OK([]); - } - - $db = $this->loris->getDatabaseConnection(); + $fileids = $this->_parseFileIDs( + $request->getQueryParams()['fileids'] ?? "" + ); + $fileurls = $this->_parseParams( + $request->getQueryParams()['fileurls'] ?? "" + ); + $response = []; - $queryparams = []; + if (count($fileids) > 0) { + $db = $this->loris->getDatabaseConnection(); - $query = $this->_buildQuery($files, $queryparams); - $dbfiles = $db->pselect( - $query, - $queryparams - ); + $queryparams = []; + $query = $this->_buildQuery($fileids, $queryparams); + $dbfiles = $db->pselect( + $query, + $queryparams + ); - if (count($files) != count($dbfiles)) { - $missing = count($files) - count($dbfiles); - if ($missing == 1) { + if (count($fileids) != count($dbfiles)) { + $missing = count($fileids) - count($dbfiles); return new \LORIS\Http\Response\JSON\NotFound( - "File not found" + "$missing file(s) not found" ); } - return new \LORIS\Http\Response\JSON\NotFound( - "$missing files not found" - ); + + $response = $this->_generateResults($dbfiles); + } + + if (count($fileurls) > 0) { + foreach ($fileurls as $fileurl) { + if (preg_match('/nii(\.gz)?/i', $fileurl)) { + $type = 'nifti1'; + } else if (preg_match('/mnc/', $fileurl)) { + $type = 'minc'; + } else { + $type = 'unknown'; + } + + $file = [ + 'type' => $type, + 'URL' => $fileurl, + 'Filename' => basename($fileurl), + 'FileID' => null, + ]; + if ($type == 'nifti1' && strpos($fileurl, '.gz') !== false) { + $file['compressed'] = true; + } + $response[] = $file; + } } - return $this->_generateResults($dbfiles); + return new \LORIS\Http\Response\JSON\OK($response); } /** * The incoming request sends the list of files ID in the files - * query parameter, as a string that looks like an array bounded - * by '[]'. The IDs have an unusual encoding where FileIDs from + * query parameter, as a comma-separated string. + * + * This converts it from a string into an array. + * + * @param string $files_str The raw value of the query param + * + * @return array + */ + private function _parseParams(string $files_str) : array + { + if ($files_str === "") { + return []; + } + + // Split on ',' if case an array was passed. + return explode(',', $files_str); + } + + /** + * The incoming request sends the list of files ID in the files + * query parameter, as a comma-separated string. + * The IDs have an unusual encoding where FileIDs from * the imaging browser are normal ideas, while IDs from the * mri violations module are encoded as a number followed by "l" * followed by the id. The number indicates the source within @@ -87,21 +131,13 @@ class ImageInfo extends \NDB_Page */ private function _parseFileIDs(string $files_str) : array { - if ($files_str === "") { - return []; - } - - // Split on ',' if case an array was passed. - $files = explode(',', $files_str); - + $files = $this->_parseParams($files_str); $parsedIDs = []; foreach ($files as $file) { if (strpos($file, 'l') == 1) { - // MRI Violation lookup, 3 cases come from - // old ajax script. + // MRI Violation lookup, 3 cases come from old ajax script. $type = $file[0]; $id = substr($file, 2); - // list($l, $id) = explode('l', $file); switch ($type) { case 1: $parsedIDs[] = ['Type' => 'ProtocolViolation', 'FileID' => $id]; @@ -207,9 +243,9 @@ class ImageInfo extends \NDB_Page * * @param iterable $dbfiles The results of the database query * - * @return ResponseInterface + * @return Array */ - private function _generateResults(iterable $dbfiles) : ResponseInterface + private function _generateResults(iterable $dbfiles) : Array { $baseurl = \NDB_Factory::singleton()->settings()->getBaseURL(); $response = []; @@ -254,7 +290,7 @@ class ImageInfo extends \NDB_Page } $response[] = $file; } - return new \LORIS\Http\Response\JSON\OK($response); + return $response; } }