Skip to content

Commit

Permalink
[Brainbrowser] Add support for filePath query param
Browse files Browse the repository at this point in the history
  • Loading branch information
laemtl committed Jan 23, 2023
1 parent 45bd5cc commit 9b19a57
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 47 deletions.
26 changes: 14 additions & 12 deletions modules/brainbrowser/js/brainbrowser.loris.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
}
}


Expand All @@ -34,6 +34,7 @@ $(function() {
BrainBrowser.VolumeViewer.start('brainbrowser', function(viewer) {
let loadingDiv = $('#loading');
let mincIDs;
let fileUrls;
let mincVolumes = [];
let mincFilenames = [];
let bboptions = {};
Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -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())
Expand Down
106 changes: 71 additions & 35 deletions modules/brainbrowser/php/imageinfo.class.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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];
Expand Down Expand Up @@ -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 = [];
Expand Down Expand Up @@ -254,7 +290,7 @@ class ImageInfo extends \NDB_Page
}
$response[] = $file;
}
return new \LORIS\Http\Response\JSON\OK($response);
return $response;
}
}

0 comments on commit 9b19a57

Please # to comment.