Skip to content

Commit

Permalink
change naming
Browse files Browse the repository at this point in the history
  • Loading branch information
maximemuldermcgill committed Aug 29, 2024
1 parent f87e4c8 commit fd349f1
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 57 deletions.
17 changes: 9 additions & 8 deletions modules/api/docs/LorisRESTAPI_v0.0.4-dev.md
Original file line number Diff line number Diff line change
Expand Up @@ -897,14 +897,15 @@ scans, respectively.
POST /candidates/$CandID/$Visit/dicoms
```
In addition to the uploaded file, the body of the POST request should contain the
following keys and values:
In addition to the uploaded file, the body of the POST request should contain a
`json` form attribute containing the following keys and values:
```js
{
'CandID': $CandID,
'PSCID': string,
'Visit': $Visit,
'IsPhantom': true|false
"CandID": $CandID,
"PSCID": $PSCID,
"Visit": $Visit,
"IsPhantom": boolean
}
```
Expand Down Expand Up @@ -964,7 +965,7 @@ Response shape:
** An empty `processes` array means that there has never been a process launched on
that `mri_upload`.
To start an `mri_upload` process on a previously uploaded DICOM study, a POST request
containing the `mri_upload_id` in the request body should be sent.
containing the `MriUploadID` attribute in the request body should be sent.
```
POST /candidates/$CandID/$VisitLabel/dicoms/$Tarname/processes
Expand Down Expand Up @@ -995,7 +996,7 @@ Response shape:
"EXIT_CODE": "0",
"ID": "1",
"PID": "24971",
"PROGRESS": "text"
"PROGRESS": "text",
"STATE": "SUCCESS|RUNNING|ERROR"
}
]
Expand Down
79 changes: 44 additions & 35 deletions modules/api/php/endpoints/candidate/visit/dicoms.class.inc
Original file line number Diff line number Diff line change
Expand Up @@ -195,87 +195,96 @@ class Dicoms extends Endpoint implements \LORIS\Middleware\ETagCalculator
$files = $request->getUploadedFiles();
if (count($files) !== 1) {
return new \LORIS\Http\Response\JSON\BadRequest(
'There should be 1 and only 1 file uploaded per POST request.'
'There should be one file uploaded per POST request.'
);
}
if (!isset($files['mriFile'])) {

if (!isset($files['MriFile'])) {
return new \LORIS\Http\Response\JSON\BadRequest(
'The input key for the uploaded file should be MriFile.'
);
}

$uploaded_file = $files['MriFile'];

$request_body = $request->getParsedBody();
if (!isset($request_body['json'])) {
return new \LORIS\Http\Response\JSON\BadRequest(
'The input key for the uploaded file should be mriFile.'
'The request should have a "json" form attribute containing the request data.'
);
}
$uploadedfile = $files['mriFile'];

$data = $request->getParsedBody();
$data = json_decode($request_body['json'], true);

$inputcandid = $data['CandID'] ?? '';
$visitcandid = $this->_visit->getCandID();
if ($inputcandid != (string) $visitcandid) {
$input_candid = $data['CandID'];
$visit_candid = $this->_visit->getCandID();
if ((string) $input_candid !== (string) $visit_candid) {
return new \LORIS\Http\Response\JSON\BadRequest(
'CandID in POST data do not match URL.'
);
}

$pscid = $data['PSCID'] ?? '';
if (!\Candidate::candidateExists($visitcandid, $pscid)) {
$pscid = $data['PSCID'];
if (!\Candidate::candidateExists($visit_candid, $pscid)) {
return new \LORIS\Http\Response\JSON\BadRequest(
'PSCID and CandID in POST data do not refer to the same candidate.'
);
}

$visitlabel = $data['Visit'] ?? null;
if ($visitlabel !== $this->_visit->getVisitLabel()) {
$visit_label = $data['Visit'];
if ($visit_label !== $this->_visit->getVisitLabel()) {
return new \LORIS\Http\Response\JSON\BadRequest(
'Visit label in URL and POST data do not match.'
);
}

$isphantom = $data['IsPhantom'] ?? null;
if (!in_array($isphantom, [true, false])) {
$is_phantom = $data['IsPhantom'];
if (!in_array($is_phantom, [true, false])) {
return new \LORIS\Http\Response\JSON\BadRequest(
'IsPhantom must be a the string "true" or "false".'
'IsPhantom must be either `true` or `false`.'
);
}

$filename = $uploadedfile->getClientFilename();
$patientname = $pscid. "_" . $inputcandid . "_" . $visitlabel;
if (!preg_match("/^{$patientname}/", $filename) && $isphantom !== 'true') {
$file_name = $uploaded_file->getClientFilename();
$patient_name = $pscid . "_" . $input_candid . "_" . $visit_label;
if (!preg_match("/^{$patient_name}/", $file_name) && !$is_phantom) {
return new \LORIS\Http\Response\JSON\BadRequest(
"Filename must start with $patientname."
"Filename must start with $patient_name."
);
}

// *************************
// *** Upload file *********
// *************************
$incomingpath = \NDB_factory::singleton()
$incoming_path = \NDB_factory::singleton()
->config()
->getSetting('MRIUploadIncomingPath');

$incomingdir = new \SplFileInfo($incomingpath);
$incoming_dir = new \SplFileInfo($incoming_path);

$uploader = (new \LORIS\FilesUploadHandler($incomingdir))
$uploader = (new \LORIS\FilesUploadHandler($incoming_dir))
->withPermittedMIMETypes(
'application/gzip',
'application/tar',
'application/tar+gzip',
'application/x-tar'
);

if ($request->getHeaderLine('LORIS-Overwrite') == 'overwrite') {
if ($request->getHeaderLine('LORIS-Overwrite') === 'overwrite') {
$uploader = $uploader->withOverwrite(true);
}

$uploadresponse = $uploader->handle($request);
if ($uploadresponse->getStatusCode() !== 201) {
return $uploadresponse;
$upload_response = $uploader->handle($request);
if ($upload_response->getStatusCode() !== 201) {
return $upload_response;
}

// *************************
// *** Update Database *****
// *************************
$filepath = $incomingdir->getPathname() . '/' . $filename;
$fileinfo = new \SplFileInfo($filepath);
if (!$fileinfo->isFile()) {
$file_path = $incoming_dir->getPathname() . '/' . $file_name;
$file_info = new \SplFileInfo($file_path);
if (!$file_info->isFile()) {
return new \LORIS\Http\Response\JSON\InternalServerError(
'Upload failed (see error_log.)'
);
Expand All @@ -284,9 +293,9 @@ class Dicoms extends Endpoint implements \LORIS\Middleware\ETagCalculator
try {
$this->_visit->addMRIUpload(
$user,
$fileinfo,
$patientname,
$isphantom,
$file_info,
$patient_name,
$is_phantom,
);
} catch (\Throwable $e) {
error_log($e->getMessage());
Expand All @@ -298,13 +307,13 @@ class Dicoms extends Endpoint implements \LORIS\Middleware\ETagCalculator
// This concatenation is here because the API part is removed from the URL by
// the module, which may not be a good idea.
$path = '/api/v0.0.4-dev/' . $request->getUri()->getPath() . '/'
. $filename . '/processes';
. $file_name . '/processes';

$processeslocation = $request->getUri()
$processes_location = $request->getUri()
->withPath($path)
->withQuery('');

return new \LORIS\Http\Response\JSON\SeeOther($processeslocation);
return new \LORIS\Http\Response\JSON\SeeOther($processes_location);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,29 +142,29 @@ class ServerProcessLauncher
/**
* Launch an MRI upload process
*
* @param int $mriUploadId ID of the MRI upload in the mri_upload table.
* @param string $sourceLocation location of the MRI file
* @param int $upload_id ID of the MRI upload in the mri_upload table.
* @param string $source_location location of the MRI file
*
* @return ?int ID (in the database) of the launched process or null
* if the process could not be run
*/
public function mriUpload($mriUploadId, $sourceLocation)
public function mriUpload($upload_id, $source_location)
{
$mriUploadProcess = new MriUploadServerProcess(
$mriUploadId,
$sourceLocation
$upload_process = new MriUploadServerProcess(
$upload_id,
$source_location
);

$dbprocessid = $this->_launch($mriUploadProcess);
$process_id = $this->_launch($upload_process);

$this->getDatabaseProvider()->getDatabase()->insert(
'mri_upload_server_processes_rel',
[
'mri_upload_id' => $mriUploadId,
'process_id' => $dbprocessid,
'UploadID' => $upload_id,
'ProcessID' => $process_id,
]
);

return $dbprocessid;
return $process_id;
}
}
6 changes: 3 additions & 3 deletions php/libraries/MRIUpload.class.inc
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ class MRIUpload
server_processes sp
JOIN
mri_upload_server_processes_rel muspr
ON (sp.id = muspr.process_id)
ON (sp.id = muspr.ProcessID)
WHERE
muspr.mri_upload_id = :v_mri_upload_id
muspr.UploadID = :UploadID
',
['v_mri_upload_id' => $this->_mriuploadid]
['UploadID' => $this->_mriuploadid]
);

if (empty($rows)) {
Expand Down
16 changes: 15 additions & 1 deletion php/libraries/Utility.class.inc
Original file line number Diff line number Diff line change
Expand Up @@ -1056,7 +1056,7 @@ class Utility

$dxToUpdate = $DB->pselect(
"SELECT cde.CandID, cde.DxEvolutionID
FROM candidate_diagnosis_evolution_rel cde
FROM candidate_diagnosis_evolution_rel cde
JOIN diagnosis_evolution de USING (DxEvolutionID)
JOIN session s ON (cde.CandID=s.CandID AND s.Visit_label=de.visitLabel)
WHERE s.ID=:sid",
Expand All @@ -1074,4 +1074,18 @@ class Utility
);
}
}

/**
* Convert a nullable integer to a nullable string.
*
* @param $int The nullable integer
*
*/
static function nullableIntToString(?int $int): ?string {
if ($int === null) {
return null;
}

return (string) $int;
}
}

0 comments on commit fd349f1

Please sign in to comment.