From a586f1e206ff732900bc96127f472b241eae5fb3 Mon Sep 17 00:00:00 2001 From: cmadjar Date: Thu, 2 Feb 2023 11:10:48 -0500 Subject: [PATCH 01/24] add download of BIDS JSON, NIfTI + Bval + Bvec to the API so it can be used by the imaging browser --- .../candidate/visit/image/format.class.inc | 12 ++ .../visit/image/format/bidsjson.class.inc | 157 ++++++++++++++++++ .../visit/image/format/bval.class.inc | 157 ++++++++++++++++++ .../visit/image/format/bvec.class.inc | 157 ++++++++++++++++++ .../visit/image/format/nifti.class.inc | 157 ++++++++++++++++++ php/libraries/Image.class.inc | 66 +++++++- src/Data/Models/ImageDTO.php | 66 +++++++- 7 files changed, 765 insertions(+), 7 deletions(-) create mode 100644 modules/api/php/endpoints/candidate/visit/image/format/bidsjson.class.inc create mode 100644 modules/api/php/endpoints/candidate/visit/image/format/bval.class.inc create mode 100644 modules/api/php/endpoints/candidate/visit/image/format/bvec.class.inc create mode 100644 modules/api/php/endpoints/candidate/visit/image/format/nifti.class.inc diff --git a/modules/api/php/endpoints/candidate/visit/image/format.class.inc b/modules/api/php/endpoints/candidate/visit/image/format.class.inc index d211146fb25..58f0d2ccb80 100644 --- a/modules/api/php/endpoints/candidate/visit/image/format.class.inc +++ b/modules/api/php/endpoints/candidate/visit/image/format.class.inc @@ -92,6 +92,18 @@ class Format extends Endpoint case 'thumbnail': $handler = new Format\Thumbnail($this->_image); break; + case 'nifti': + $handler = new Format\NIfTI($this->_image); + break; + case 'bval': + $handler = new Format\Bval($this->_image); + break; + case 'bvec': + $handler = new Format\Bvec($this->_image); + break; + case 'bidsjson': + $handler = new Format\BidsJson($this->_image); + break; default: return new \LORIS\Http\Response\JSON\UnsupportedMediaType(); } diff --git a/modules/api/php/endpoints/candidate/visit/image/format/bidsjson.class.inc b/modules/api/php/endpoints/candidate/visit/image/format/bidsjson.class.inc new file mode 100644 index 00000000000..f51f61b2424 --- /dev/null +++ b/modules/api/php/endpoints/candidate/visit/image/format/bidsjson.class.inc @@ -0,0 +1,157 @@ + + * @license http://www.gnu.org/licenses/gpl-3.0.txt GPLv3 + * @link https://github.com/aces/Loris + */ +namespace LORIS\api\Endpoints\Candidate\Visit\Image\Format; + +use \Psr\Http\Message\ServerRequestInterface; +use \Psr\Http\Message\ResponseInterface; +use \LORIS\api\Endpoint; + +/** + * This class handles request to an image thumbnail format. + * + * @category API + * @package Loris + * @author Xavier Lecours Boucher + * @license http://www.gnu.org/licenses/gpl-3.0.txt GPLv3 + * @link https://github.com/aces/Loris + */ +class BidsJson extends Endpoint implements \LORIS\Middleware\ETagCalculator +{ + + /** + * The requested Image + * + * @var \LORIS\Image + */ + private $_image; + + /** + * Contructor + * + * @param \LORIS\Image $image The requested image + */ + public function __construct(\LORIS\Image $image) + { + $this->_image = $image; + } + + /** + * Return which methods are supported by this endpoint. + * + * @return array supported HTTP methods + */ + protected function allowedMethods() : array + { + return ['GET']; + } + + /** + * Versions of the LORIS API which are supported by this + * endpoint. + * + * @return array a list of supported API versions. + */ + protected function supportedVersions() : array + { + return [ + 'v0.0.3', + 'v0.0.4-dev', + ]; + } + + /** + * Delegates the request to a private handler appropriate to the request's + * method. + * + * @param ServerRequestInterface $request The incoming PSR7 request + * + * @return ResponseInterface The outgoing PSR7 response + */ + public function handle(ServerRequestInterface $request) : ResponseInterface + { + switch ($request->getMethod()) { + case 'GET': + return $this->_handleGET($request); + + case 'OPTIONS': + return (new \LORIS\Http\Response()) + ->withHeader('Allow', $this->allowedMethods()); + + default: + return new \LORIS\Http\Response\JSON\MethodNotAllowed( + $this->allowedMethods() + ); + + } + } + + /** + * Creates a response with the thumbnail as body + * + * @return ResponseInterface The outgoing PSR7 response + */ + private function _handleGET(): ResponseInterface + { + $info = $this->_image->getBidsJsonFileInfo(); + + if (!$info->isFile()) { + error_log('BIDS JSON file not found'); + return new \LORIS\Http\Response\JSON\NotFound(); + } + + if (!$info->isReadable()) { + error_log('BIDS JSON file exists but is not readable by webserver'); + return new \LORIS\Http\Response\JSON\NotFound(); + } + + $filename = $info->getFilename(); + $realpath = $info->getRealPath(); + if ($realpath === false) { + $realpath = $info->getPath() . "/" . $info->getFilename(); + } + + $body = new \LORIS\Http\FileStream($realpath, 'r'); + + return new \LORIS\Http\Response( + $body, + 200, + [ + 'Content-Type' => 'application/json', + 'Content-Disposition' => 'attachment; filename=' . $filename, + ] + ); + } + + /** + * Implements the ETagCalculator interface + * + * @param ServerRequestInterface $request The PSR7 incoming request. + * + * @return string etag summarizing value of this request. + */ + public function ETag(ServerRequestInterface $request) : string + { + $info = $this->_image->getBidsJsonFileInfo(); + + if (!$info->isFile() || !$info->isReadable()) { + return ''; + } + + $signature = [ + 'filename' => $info->getFilename(), + 'size' => $info->getSize(), + 'mtime' => $info->getMTime(), + ]; + + return md5(json_encode($signature)); + } +} + diff --git a/modules/api/php/endpoints/candidate/visit/image/format/bval.class.inc b/modules/api/php/endpoints/candidate/visit/image/format/bval.class.inc new file mode 100644 index 00000000000..abf9ca01c50 --- /dev/null +++ b/modules/api/php/endpoints/candidate/visit/image/format/bval.class.inc @@ -0,0 +1,157 @@ + + * @license http://www.gnu.org/licenses/gpl-3.0.txt GPLv3 + * @link https://github.com/aces/Loris + */ +namespace LORIS\api\Endpoints\Candidate\Visit\Image\Format; + +use \Psr\Http\Message\ServerRequestInterface; +use \Psr\Http\Message\ResponseInterface; +use \LORIS\api\Endpoint; + +/** + * This class handles request to an image thumbnail format. + * + * @category API + * @package Loris + * @author Xavier Lecours Boucher + * @license http://www.gnu.org/licenses/gpl-3.0.txt GPLv3 + * @link https://github.com/aces/Loris + */ +class Bval extends Endpoint implements \LORIS\Middleware\ETagCalculator +{ + + /** + * The requested Image + * + * @var \LORIS\Image + */ + private $_image; + + /** + * Contructor + * + * @param \LORIS\Image $image The requested image + */ + public function __construct(\LORIS\Image $image) + { + $this->_image = $image; + } + + /** + * Return which methods are supported by this endpoint. + * + * @return array supported HTTP methods + */ + protected function allowedMethods() : array + { + return ['GET']; + } + + /** + * Versions of the LORIS API which are supported by this + * endpoint. + * + * @return array a list of supported API versions. + */ + protected function supportedVersions() : array + { + return [ + 'v0.0.3', + 'v0.0.4-dev', + ]; + } + + /** + * Delegates the request to a private handler appropriate to the request's + * method. + * + * @param ServerRequestInterface $request The incoming PSR7 request + * + * @return ResponseInterface The outgoing PSR7 response + */ + public function handle(ServerRequestInterface $request) : ResponseInterface + { + switch ($request->getMethod()) { + case 'GET': + return $this->_handleGET($request); + + case 'OPTIONS': + return (new \LORIS\Http\Response()) + ->withHeader('Allow', $this->allowedMethods()); + + default: + return new \LORIS\Http\Response\JSON\MethodNotAllowed( + $this->allowedMethods() + ); + + } + } + + /** + * Creates a response with the thumbnail as body + * + * @return ResponseInterface The outgoing PSR7 response + */ + private function _handleGET(): ResponseInterface + { + $info = $this->_image->getBvalFileInfo(); + + if (!$info->isFile()) { + error_log('NIfTI BVAL file not found'); + return new \LORIS\Http\Response\JSON\NotFound(); + } + + if (!$info->isReadable()) { + error_log('NIfTI BVAL file exists but is not readable by webserver'); + return new \LORIS\Http\Response\JSON\NotFound(); + } + + $filename = $info->getFilename(); + $realpath = $info->getRealPath(); + if ($realpath === false) { + $realpath = $info->getPath() . "/" . $info->getFilename(); + } + + $body = new \LORIS\Http\FileStream($realpath, 'r'); + + return new \LORIS\Http\Response( + $body, + 200, + [ + 'Content-Type' => 'application/text', + 'Content-Disposition' => 'attachment; filename=' . $filename, + ] + ); + } + + /** + * Implements the ETagCalculator interface + * + * @param ServerRequestInterface $request The PSR7 incoming request. + * + * @return string etag summarizing value of this request. + */ + public function ETag(ServerRequestInterface $request) : string + { + $info = $this->_image->getBvalFileInfo(); + + if (!$info->isFile() || !$info->isReadable()) { + return ''; + } + + $signature = [ + 'filename' => $info->getFilename(), + 'size' => $info->getSize(), + 'mtime' => $info->getMTime(), + ]; + + return md5(json_encode($signature)); + } +} + diff --git a/modules/api/php/endpoints/candidate/visit/image/format/bvec.class.inc b/modules/api/php/endpoints/candidate/visit/image/format/bvec.class.inc new file mode 100644 index 00000000000..c0937323648 --- /dev/null +++ b/modules/api/php/endpoints/candidate/visit/image/format/bvec.class.inc @@ -0,0 +1,157 @@ + + * @license http://www.gnu.org/licenses/gpl-3.0.txt GPLv3 + * @link https://github.com/aces/Loris + */ +namespace LORIS\api\Endpoints\Candidate\Visit\Image\Format; + +use \Psr\Http\Message\ServerRequestInterface; +use \Psr\Http\Message\ResponseInterface; +use \LORIS\api\Endpoint; + +/** + * This class handles request to an image thumbnail format. + * + * @category API + * @package Loris + * @author Xavier Lecours Boucher + * @license http://www.gnu.org/licenses/gpl-3.0.txt GPLv3 + * @link https://github.com/aces/Loris + */ +class Bvec extends Endpoint implements \LORIS\Middleware\ETagCalculator +{ + + /** + * The requested Image + * + * @var \LORIS\Image + */ + private $_image; + + /** + * Contructor + * + * @param \LORIS\Image $image The requested image + */ + public function __construct(\LORIS\Image $image) + { + $this->_image = $image; + } + + /** + * Return which methods are supported by this endpoint. + * + * @return array supported HTTP methods + */ + protected function allowedMethods() : array + { + return ['GET']; + } + + /** + * Versions of the LORIS API which are supported by this + * endpoint. + * + * @return array a list of supported API versions. + */ + protected function supportedVersions() : array + { + return [ + 'v0.0.3', + 'v0.0.4-dev', + ]; + } + + /** + * Delegates the request to a private handler appropriate to the request's + * method. + * + * @param ServerRequestInterface $request The incoming PSR7 request + * + * @return ResponseInterface The outgoing PSR7 response + */ + public function handle(ServerRequestInterface $request) : ResponseInterface + { + switch ($request->getMethod()) { + case 'GET': + return $this->_handleGET($request); + + case 'OPTIONS': + return (new \LORIS\Http\Response()) + ->withHeader('Allow', $this->allowedMethods()); + + default: + return new \LORIS\Http\Response\JSON\MethodNotAllowed( + $this->allowedMethods() + ); + + } + } + + /** + * Creates a response with the thumbnail as body + * + * @return ResponseInterface The outgoing PSR7 response + */ + private function _handleGET(): ResponseInterface + { + $info = $this->_image->getBvecFileInfo(); + + if (!$info->isFile()) { + error_log('NIfTI BVEC file not found'); + return new \LORIS\Http\Response\JSON\NotFound(); + } + + if (!$info->isReadable()) { + error_log('NIfTI BVEC file exists but is not readable by webserver'); + return new \LORIS\Http\Response\JSON\NotFound(); + } + + $filename = $info->getFilename(); + $realpath = $info->getRealPath(); + if ($realpath === false) { + $realpath = $info->getPath() . "/" . $info->getFilename(); + } + + $body = new \LORIS\Http\FileStream($realpath, 'r'); + + return new \LORIS\Http\Response( + $body, + 200, + [ + 'Content-Type' => 'application/text', + 'Content-Disposition' => 'attachment; filename=' . $filename, + ] + ); + } + + /** + * Implements the ETagCalculator interface + * + * @param ServerRequestInterface $request The PSR7 incoming request. + * + * @return string etag summarizing value of this request. + */ + public function ETag(ServerRequestInterface $request) : string + { + $info = $this->_image->getBvecFileInfo(); + + if (!$info->isFile() || !$info->isReadable()) { + return ''; + } + + $signature = [ + 'filename' => $info->getFilename(), + 'size' => $info->getSize(), + 'mtime' => $info->getMTime(), + ]; + + return md5(json_encode($signature)); + } +} + diff --git a/modules/api/php/endpoints/candidate/visit/image/format/nifti.class.inc b/modules/api/php/endpoints/candidate/visit/image/format/nifti.class.inc new file mode 100644 index 00000000000..df57a64ae3a --- /dev/null +++ b/modules/api/php/endpoints/candidate/visit/image/format/nifti.class.inc @@ -0,0 +1,157 @@ + + * @license http://www.gnu.org/licenses/gpl-3.0.txt GPLv3 + * @link https://github.com/aces/Loris + */ +namespace LORIS\api\Endpoints\Candidate\Visit\Image\Format; + +use \Psr\Http\Message\ServerRequestInterface; +use \Psr\Http\Message\ResponseInterface; +use \LORIS\api\Endpoint; + +/** + * This class handles request to an image thumbnail format. + * + * @category API + * @package Loris + * @author Xavier Lecours Boucher + * @license http://www.gnu.org/licenses/gpl-3.0.txt GPLv3 + * @link https://github.com/aces/Loris + */ +class NIfTI extends Endpoint implements \LORIS\Middleware\ETagCalculator +{ + + /** + * The requested Image + * + * @var \LORIS\Image + */ + private $_image; + + /** + * Contructor + * + * @param \LORIS\Image $image The requested image + */ + public function __construct(\LORIS\Image $image) + { + $this->_image = $image; + } + + /** + * Return which methods are supported by this endpoint. + * + * @return array supported HTTP methods + */ + protected function allowedMethods() : array + { + return ['GET']; + } + + /** + * Versions of the LORIS API which are supported by this + * endpoint. + * + * @return array a list of supported API versions. + */ + protected function supportedVersions() : array + { + return [ + 'v0.0.3', + 'v0.0.4-dev', + ]; + } + + /** + * Delegates the request to a private handler appropriate to the request's + * method. + * + * @param ServerRequestInterface $request The incoming PSR7 request + * + * @return ResponseInterface The outgoing PSR7 response + */ + public function handle(ServerRequestInterface $request) : ResponseInterface + { + switch ($request->getMethod()) { + case 'GET': + return $this->_handleGET($request); + + case 'OPTIONS': + return (new \LORIS\Http\Response()) + ->withHeader('Allow', $this->allowedMethods()); + + default: + return new \LORIS\Http\Response\JSON\MethodNotAllowed( + $this->allowedMethods() + ); + + } + } + + /** + * Creates a response with the thumbnail as body + * + * @return ResponseInterface The outgoing PSR7 response + */ + private function _handleGET(): ResponseInterface + { + $info = $this->_image->getNiiFileInfo(); + + if (!$info->isFile()) { + error_log('NIfTI file not found'); + return new \LORIS\Http\Response\JSON\NotFound(); + } + + if (!$info->isReadable()) { + error_log('NIfTI file exists but is not readable by webserver'); + return new \LORIS\Http\Response\JSON\NotFound(); + } + + $filename = $info->getFilename(); + $realpath = $info->getRealPath(); + if ($realpath === false) { + $realpath = $info->getPath() . "/" . $info->getFilename(); + } + + $body = new \LORIS\Http\FileStream($realpath, 'r'); + + return new \LORIS\Http\Response( + $body, + 200, + [ + 'Content-Type' => 'application/octet-stream', + 'Content-Disposition' => 'attachment; filename=' . $filename, + ] + ); + } + + /** + * Implements the ETagCalculator interface + * + * @param ServerRequestInterface $request The PSR7 incoming request. + * + * @return string etag summarizing value of this request. + */ + public function ETag(ServerRequestInterface $request) : string + { + $info = $this->_image->getNiiFileInfo(); + + if (!$info->isFile() || !$info->isReadable()) { + return ''; + } + + $signature = [ + 'filename' => $info->getFilename(), + 'size' => $info->getSize(), + 'mtime' => $info->getMTime(), + ]; + + return md5(json_encode($signature)); + } +} + diff --git a/php/libraries/Image.class.inc b/php/libraries/Image.class.inc index 7c6b128edea..4605c0663eb 100644 --- a/php/libraries/Image.class.inc +++ b/php/libraries/Image.class.inc @@ -38,6 +38,15 @@ class Image private \CenterID $_centerid; private $_entitytype; + + private $check_nii_filename; + + private $check_bval_filename; + + private $check_bvec_filename; + + private $bids_json_filename; + /** * Constructor * @@ -58,7 +67,7 @@ class Image s.CenterID as centerid, c.Entity_type as entitytype FROM - files f + files f LEFT JOIN session s ON (f.SessionID = s.ID) LEFT JOIN candidate c @@ -80,7 +89,10 @@ class Image $this->_filetype = $dbrow['filetype']; $this->_centerid = new \CenterID($dbrow['centerid']); $this->_entitytype = $dbrow['entitytype']; - + $this->_check_nii_filename = $this->getHeader('check_nii_filename'); + $this->_check_bval_filename = $this->getHeader('check_bval_filename'); + $this->_check_bvec_filename = $this->getHeader('check_bvec_filename'); + $this->_bids_json_filename = $this->getHeader('bids_json_file'); } } @@ -100,7 +112,7 @@ class Image SELECT Value FROM - parameter_file pf + parameter_file pf JOIN parameter_type pt USING (ParameterTypeID) JOIN files f @@ -132,7 +144,7 @@ class Image pt.Name as name, pf.Value as value FROM - parameter_file pf + parameter_file pf JOIN parameter_type pt USING (ParameterTypeID) JOIN files f @@ -253,6 +265,46 @@ class Image return $this->_getFullPath($this->_filelocation); } + /** + * Return a SPLFileInfo object based on this images's properties. + * + * @return \SplFileInfo + */ + public function getNiiFileInfo(): \SplFileInfo + { + return $this->_getFullPath($this->_check_nii_filename); + } + + /** + * Return a SPLFileInfo object based on this images's properties. + * + * @return \SplFileInfo + */ + public function getBvalFileInfo(): \SplFileInfo + { + return $this->_getFullPath($this->_check_bval_filename); + } + + /** + * Return a SPLFileInfo object based on this images's properties. + * + * @return \SplFileInfo + */ + public function getBvecFileInfo(): \SplFileInfo + { + return $this->_getFullPath($this->_check_bvec_filename); + } + + /** + * Return a SPLFileInfo object based on this images's properties. + * + * @return \SplFileInfo + */ + public function getBidsJsonFileInfo(): \SplFileInfo + { + return $this->_getFullPath($this->_bids_json_filename); + } + /** * Return a SPLFileInfo object based on this images's thumbnail properties. * @@ -306,7 +358,11 @@ class Image $this->_acquisitionprotocol, $this->_filetype, $this->_centerid, - $this->_entitytype + $this->_entitytype, + $this->_check_nii_filename, + $this->_check_bval_filename, + $this->_check_bvec_filename, + $this->_bids_json_filename ); } } diff --git a/src/Data/Models/ImageDTO.php b/src/Data/Models/ImageDTO.php index 65fd77b2c32..a48de74190a 100644 --- a/src/Data/Models/ImageDTO.php +++ b/src/Data/Models/ImageDTO.php @@ -12,6 +12,8 @@ */ namespace LORIS\Data\Models; +use http\Encoding\Stream\Inflate; + /** * This class defines an ImageDTO which is an immutable representation of a * Image object. Its purpose is to provide accessors to an Image properties. @@ -43,6 +45,14 @@ class ImageDTO implements private $entitytype; + private $check_nii_filename; + + private $check_bval_filename; + + private $check_bvec_filename; + + private $bids_json_filename; + /** * Constructor * @@ -52,8 +62,12 @@ class ImageDTO implements * @param ?string $outputtype The output type * @param ?string $acquisitionprotocol The aquisition protocol * @param ?string $filetype The file type - * @param \CenterID $centerid The image session's centerid + * @param \CenterID $centerid The image session's centerid * @param ?string $entitytype The image candidate's entity_type + * @param ?string $check_nii_filename The NIfTI file generated by mnc2nii + * @param ?string $check_bval_filename The NIfTI bval file for DWI + * @param ?string $check_bvec_filename The NIfTI bvec file for DWI + * @param ?string $bids_json_filename The BIDS JSON file */ public function __construct( ?int $fileid, @@ -63,7 +77,11 @@ public function __construct( ?string $acquisitionprotocol, ?string $filetype, \CenterID $centerid, - ?string $entitytype + ?string $entitytype, + ?string $check_nii_filename, + ?string $check_bval_filename, + ?string $check_bvec_filename, + ?string $bids_json_filename ) { $this->fileid = $fileid; $this->filename = $filename; @@ -73,6 +91,10 @@ public function __construct( $this->filetype = $filetype; $this->centerid = $centerid; $this->entitytype = $entitytype; + $this->check_nii_filename = $check_nii_filename; + $this->check_bval_filename = $check_bval_filename; + $this->check_bvec_filename = $check_bvec_filename; + $this->bids_json_filename = $bids_json_filename; } /** @@ -172,4 +194,44 @@ public function isPhantom(): bool { return $this->entitytype === 'Scanner'; } + + /** + * Accessor for NIfTI filename stored in parameter_file. + * + * @return ?string + */ + public function getCheckNiiFilename(): ?string + { + return $this->check_nii_filename; + } + + /** + * Accessor for NIfTI BVAL filename. + * + * @return ?string + */ + public function getCheckBvalFilename(): ?string + { + return $this->check_bval_filename; + } + + /** + * Accessor for NIfTI BVEC filename. + * + * @return ?string + */ + public function getCheckBvecFilename(): ?string + { + return $this->check_bvec_filename; + } + + /** + * Accessor for BIDS JSON filename. + * + * @return ?string + */ + public function getBidsJsonFilename(): ?string + { + return $this->bids_json_filename; + } } From 47167379108ebf02c8bcb3ab39a19338e259e891 Mon Sep 17 00:00:00 2001 From: cmadjar Date: Thu, 2 Feb 2023 12:39:07 -0500 Subject: [PATCH 02/24] use API links to download from view session imaging browser --- modules/imaging_browser/jsx/ImagePanel.js | 41 +++++++++++-------- .../imaging_browser/php/viewsession.class.inc | 1 + php/libraries/Image.class.inc | 8 ++-- 3 files changed, 30 insertions(+), 20 deletions(-) diff --git a/modules/imaging_browser/jsx/ImagePanel.js b/modules/imaging_browser/jsx/ImagePanel.js index ba47d4430e0..43b1d865f2f 100644 --- a/modules/imaging_browser/jsx/ImagePanel.js +++ b/modules/imaging_browser/jsx/ImagePanel.js @@ -671,6 +671,7 @@ DownloadButton.propTypes = { FileName: PropTypes.string, BaseURL: PropTypes.string, Label: PropTypes.string, + URL: PropTypes.string, }; @@ -826,22 +827,30 @@ class ImageDownloadButtons extends Component { BaseURL={this.props.BaseURL} Label="Download NRRD" /> - - - - + { this.props.NiiFile ? + : + null + } + {this.props.BvalFile ? + : + null + } + {this.props.BvecFile ? + : + null + } + {this.props.JsonFile ? + : + null + } $OtherTimepoints, 'CaveatViolationsResolvedID' => $caveatViolationsResolvedID, ]; + error_log(print_r($file['BvecFile'])); $this->tpl_data['files'][] = $file; } } diff --git a/php/libraries/Image.class.inc b/php/libraries/Image.class.inc index 4605c0663eb..9f503c4c730 100644 --- a/php/libraries/Image.class.inc +++ b/php/libraries/Image.class.inc @@ -39,13 +39,13 @@ class Image private $_entitytype; - private $check_nii_filename; + private $_check_nii_filename; - private $check_bval_filename; + private $_check_bval_filename; - private $check_bvec_filename; + private $_check_bvec_filename; - private $bids_json_filename; + private $_bids_json_filename; /** * Constructor From c40102c7f9fa9f3736d17ffcda1d3b24fa0c220b Mon Sep 17 00:00:00 2001 From: cmadjar Date: Thu, 2 Feb 2023 12:51:04 -0500 Subject: [PATCH 03/24] fix phan --- modules/imaging_browser/php/viewsession.class.inc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/imaging_browser/php/viewsession.class.inc b/modules/imaging_browser/php/viewsession.class.inc index ca2e4dd4ca5..f3ef14ba6d3 100644 --- a/modules/imaging_browser/php/viewsession.class.inc +++ b/modules/imaging_browser/php/viewsession.class.inc @@ -369,7 +369,7 @@ class ViewSession extends \NDB_Form 'OtherTimepoints' => $OtherTimepoints, 'CaveatViolationsResolvedID' => $caveatViolationsResolvedID, ]; - error_log(print_r($file['BvecFile'])); + $this->tpl_data['files'][] = $file; } } From 4f3ea9684e54cabf9b7d122033ffd525d8c5f9f2 Mon Sep 17 00:00:00 2001 From: cmadjar Date: Thu, 2 Feb 2023 12:57:08 -0500 Subject: [PATCH 04/24] fix phan --- src/Data/Models/ImageDTO.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Data/Models/ImageDTO.php b/src/Data/Models/ImageDTO.php index a48de74190a..c64cae52b01 100644 --- a/src/Data/Models/ImageDTO.php +++ b/src/Data/Models/ImageDTO.php @@ -12,8 +12,6 @@ */ namespace LORIS\Data\Models; -use http\Encoding\Stream\Inflate; - /** * This class defines an ImageDTO which is an immutable representation of a * Image object. Its purpose is to provide accessors to an Image properties. From fdb6512b3b6cbd60db06d9cf2a55910d21abe0d8 Mon Sep 17 00:00:00 2001 From: cmadjar Date: Thu, 2 Feb 2023 15:32:51 -0500 Subject: [PATCH 05/24] create one class to do them all --- .../candidate/visit/image/format.class.inc | 11 +- .../visit/image/format/bidsjson.class.inc | 157 ------------------ .../visit/image/format/bval.class.inc | 157 ------------------ .../visit/image/format/bvec.class.inc | 157 ------------------ ...bnail.class.inc => downloadfile.class.inc} | 63 ++++++- .../visit/image/format/nifti.class.inc | 157 ------------------ 6 files changed, 57 insertions(+), 645 deletions(-) delete mode 100644 modules/api/php/endpoints/candidate/visit/image/format/bidsjson.class.inc delete mode 100644 modules/api/php/endpoints/candidate/visit/image/format/bval.class.inc delete mode 100644 modules/api/php/endpoints/candidate/visit/image/format/bvec.class.inc rename modules/api/php/endpoints/candidate/visit/image/format/{thumbnail.class.inc => downloadfile.class.inc} (62%) delete mode 100644 modules/api/php/endpoints/candidate/visit/image/format/nifti.class.inc diff --git a/modules/api/php/endpoints/candidate/visit/image/format.class.inc b/modules/api/php/endpoints/candidate/visit/image/format.class.inc index 58f0d2ccb80..7f3bf2a98b7 100644 --- a/modules/api/php/endpoints/candidate/visit/image/format.class.inc +++ b/modules/api/php/endpoints/candidate/visit/image/format.class.inc @@ -90,19 +90,12 @@ class Format extends Endpoint $handler = new Format\Brainbrowser($this->_image); break; case 'thumbnail': - $handler = new Format\Thumbnail($this->_image); - break; case 'nifti': - $handler = new Format\NIfTI($this->_image); - break; case 'bval': - $handler = new Format\Bval($this->_image); - break; case 'bvec': - $handler = new Format\Bvec($this->_image); - break; case 'bidsjson': - $handler = new Format\BidsJson($this->_image); + error_log(print_r($format, true)); + $handler = new Format\DownloadFile($this->_image, $format); break; default: return new \LORIS\Http\Response\JSON\UnsupportedMediaType(); diff --git a/modules/api/php/endpoints/candidate/visit/image/format/bidsjson.class.inc b/modules/api/php/endpoints/candidate/visit/image/format/bidsjson.class.inc deleted file mode 100644 index f51f61b2424..00000000000 --- a/modules/api/php/endpoints/candidate/visit/image/format/bidsjson.class.inc +++ /dev/null @@ -1,157 +0,0 @@ - - * @license http://www.gnu.org/licenses/gpl-3.0.txt GPLv3 - * @link https://github.com/aces/Loris - */ -namespace LORIS\api\Endpoints\Candidate\Visit\Image\Format; - -use \Psr\Http\Message\ServerRequestInterface; -use \Psr\Http\Message\ResponseInterface; -use \LORIS\api\Endpoint; - -/** - * This class handles request to an image thumbnail format. - * - * @category API - * @package Loris - * @author Xavier Lecours Boucher - * @license http://www.gnu.org/licenses/gpl-3.0.txt GPLv3 - * @link https://github.com/aces/Loris - */ -class BidsJson extends Endpoint implements \LORIS\Middleware\ETagCalculator -{ - - /** - * The requested Image - * - * @var \LORIS\Image - */ - private $_image; - - /** - * Contructor - * - * @param \LORIS\Image $image The requested image - */ - public function __construct(\LORIS\Image $image) - { - $this->_image = $image; - } - - /** - * Return which methods are supported by this endpoint. - * - * @return array supported HTTP methods - */ - protected function allowedMethods() : array - { - return ['GET']; - } - - /** - * Versions of the LORIS API which are supported by this - * endpoint. - * - * @return array a list of supported API versions. - */ - protected function supportedVersions() : array - { - return [ - 'v0.0.3', - 'v0.0.4-dev', - ]; - } - - /** - * Delegates the request to a private handler appropriate to the request's - * method. - * - * @param ServerRequestInterface $request The incoming PSR7 request - * - * @return ResponseInterface The outgoing PSR7 response - */ - public function handle(ServerRequestInterface $request) : ResponseInterface - { - switch ($request->getMethod()) { - case 'GET': - return $this->_handleGET($request); - - case 'OPTIONS': - return (new \LORIS\Http\Response()) - ->withHeader('Allow', $this->allowedMethods()); - - default: - return new \LORIS\Http\Response\JSON\MethodNotAllowed( - $this->allowedMethods() - ); - - } - } - - /** - * Creates a response with the thumbnail as body - * - * @return ResponseInterface The outgoing PSR7 response - */ - private function _handleGET(): ResponseInterface - { - $info = $this->_image->getBidsJsonFileInfo(); - - if (!$info->isFile()) { - error_log('BIDS JSON file not found'); - return new \LORIS\Http\Response\JSON\NotFound(); - } - - if (!$info->isReadable()) { - error_log('BIDS JSON file exists but is not readable by webserver'); - return new \LORIS\Http\Response\JSON\NotFound(); - } - - $filename = $info->getFilename(); - $realpath = $info->getRealPath(); - if ($realpath === false) { - $realpath = $info->getPath() . "/" . $info->getFilename(); - } - - $body = new \LORIS\Http\FileStream($realpath, 'r'); - - return new \LORIS\Http\Response( - $body, - 200, - [ - 'Content-Type' => 'application/json', - 'Content-Disposition' => 'attachment; filename=' . $filename, - ] - ); - } - - /** - * Implements the ETagCalculator interface - * - * @param ServerRequestInterface $request The PSR7 incoming request. - * - * @return string etag summarizing value of this request. - */ - public function ETag(ServerRequestInterface $request) : string - { - $info = $this->_image->getBidsJsonFileInfo(); - - if (!$info->isFile() || !$info->isReadable()) { - return ''; - } - - $signature = [ - 'filename' => $info->getFilename(), - 'size' => $info->getSize(), - 'mtime' => $info->getMTime(), - ]; - - return md5(json_encode($signature)); - } -} - diff --git a/modules/api/php/endpoints/candidate/visit/image/format/bval.class.inc b/modules/api/php/endpoints/candidate/visit/image/format/bval.class.inc deleted file mode 100644 index abf9ca01c50..00000000000 --- a/modules/api/php/endpoints/candidate/visit/image/format/bval.class.inc +++ /dev/null @@ -1,157 +0,0 @@ - - * @license http://www.gnu.org/licenses/gpl-3.0.txt GPLv3 - * @link https://github.com/aces/Loris - */ -namespace LORIS\api\Endpoints\Candidate\Visit\Image\Format; - -use \Psr\Http\Message\ServerRequestInterface; -use \Psr\Http\Message\ResponseInterface; -use \LORIS\api\Endpoint; - -/** - * This class handles request to an image thumbnail format. - * - * @category API - * @package Loris - * @author Xavier Lecours Boucher - * @license http://www.gnu.org/licenses/gpl-3.0.txt GPLv3 - * @link https://github.com/aces/Loris - */ -class Bval extends Endpoint implements \LORIS\Middleware\ETagCalculator -{ - - /** - * The requested Image - * - * @var \LORIS\Image - */ - private $_image; - - /** - * Contructor - * - * @param \LORIS\Image $image The requested image - */ - public function __construct(\LORIS\Image $image) - { - $this->_image = $image; - } - - /** - * Return which methods are supported by this endpoint. - * - * @return array supported HTTP methods - */ - protected function allowedMethods() : array - { - return ['GET']; - } - - /** - * Versions of the LORIS API which are supported by this - * endpoint. - * - * @return array a list of supported API versions. - */ - protected function supportedVersions() : array - { - return [ - 'v0.0.3', - 'v0.0.4-dev', - ]; - } - - /** - * Delegates the request to a private handler appropriate to the request's - * method. - * - * @param ServerRequestInterface $request The incoming PSR7 request - * - * @return ResponseInterface The outgoing PSR7 response - */ - public function handle(ServerRequestInterface $request) : ResponseInterface - { - switch ($request->getMethod()) { - case 'GET': - return $this->_handleGET($request); - - case 'OPTIONS': - return (new \LORIS\Http\Response()) - ->withHeader('Allow', $this->allowedMethods()); - - default: - return new \LORIS\Http\Response\JSON\MethodNotAllowed( - $this->allowedMethods() - ); - - } - } - - /** - * Creates a response with the thumbnail as body - * - * @return ResponseInterface The outgoing PSR7 response - */ - private function _handleGET(): ResponseInterface - { - $info = $this->_image->getBvalFileInfo(); - - if (!$info->isFile()) { - error_log('NIfTI BVAL file not found'); - return new \LORIS\Http\Response\JSON\NotFound(); - } - - if (!$info->isReadable()) { - error_log('NIfTI BVAL file exists but is not readable by webserver'); - return new \LORIS\Http\Response\JSON\NotFound(); - } - - $filename = $info->getFilename(); - $realpath = $info->getRealPath(); - if ($realpath === false) { - $realpath = $info->getPath() . "/" . $info->getFilename(); - } - - $body = new \LORIS\Http\FileStream($realpath, 'r'); - - return new \LORIS\Http\Response( - $body, - 200, - [ - 'Content-Type' => 'application/text', - 'Content-Disposition' => 'attachment; filename=' . $filename, - ] - ); - } - - /** - * Implements the ETagCalculator interface - * - * @param ServerRequestInterface $request The PSR7 incoming request. - * - * @return string etag summarizing value of this request. - */ - public function ETag(ServerRequestInterface $request) : string - { - $info = $this->_image->getBvalFileInfo(); - - if (!$info->isFile() || !$info->isReadable()) { - return ''; - } - - $signature = [ - 'filename' => $info->getFilename(), - 'size' => $info->getSize(), - 'mtime' => $info->getMTime(), - ]; - - return md5(json_encode($signature)); - } -} - diff --git a/modules/api/php/endpoints/candidate/visit/image/format/bvec.class.inc b/modules/api/php/endpoints/candidate/visit/image/format/bvec.class.inc deleted file mode 100644 index c0937323648..00000000000 --- a/modules/api/php/endpoints/candidate/visit/image/format/bvec.class.inc +++ /dev/null @@ -1,157 +0,0 @@ - - * @license http://www.gnu.org/licenses/gpl-3.0.txt GPLv3 - * @link https://github.com/aces/Loris - */ -namespace LORIS\api\Endpoints\Candidate\Visit\Image\Format; - -use \Psr\Http\Message\ServerRequestInterface; -use \Psr\Http\Message\ResponseInterface; -use \LORIS\api\Endpoint; - -/** - * This class handles request to an image thumbnail format. - * - * @category API - * @package Loris - * @author Xavier Lecours Boucher - * @license http://www.gnu.org/licenses/gpl-3.0.txt GPLv3 - * @link https://github.com/aces/Loris - */ -class Bvec extends Endpoint implements \LORIS\Middleware\ETagCalculator -{ - - /** - * The requested Image - * - * @var \LORIS\Image - */ - private $_image; - - /** - * Contructor - * - * @param \LORIS\Image $image The requested image - */ - public function __construct(\LORIS\Image $image) - { - $this->_image = $image; - } - - /** - * Return which methods are supported by this endpoint. - * - * @return array supported HTTP methods - */ - protected function allowedMethods() : array - { - return ['GET']; - } - - /** - * Versions of the LORIS API which are supported by this - * endpoint. - * - * @return array a list of supported API versions. - */ - protected function supportedVersions() : array - { - return [ - 'v0.0.3', - 'v0.0.4-dev', - ]; - } - - /** - * Delegates the request to a private handler appropriate to the request's - * method. - * - * @param ServerRequestInterface $request The incoming PSR7 request - * - * @return ResponseInterface The outgoing PSR7 response - */ - public function handle(ServerRequestInterface $request) : ResponseInterface - { - switch ($request->getMethod()) { - case 'GET': - return $this->_handleGET($request); - - case 'OPTIONS': - return (new \LORIS\Http\Response()) - ->withHeader('Allow', $this->allowedMethods()); - - default: - return new \LORIS\Http\Response\JSON\MethodNotAllowed( - $this->allowedMethods() - ); - - } - } - - /** - * Creates a response with the thumbnail as body - * - * @return ResponseInterface The outgoing PSR7 response - */ - private function _handleGET(): ResponseInterface - { - $info = $this->_image->getBvecFileInfo(); - - if (!$info->isFile()) { - error_log('NIfTI BVEC file not found'); - return new \LORIS\Http\Response\JSON\NotFound(); - } - - if (!$info->isReadable()) { - error_log('NIfTI BVEC file exists but is not readable by webserver'); - return new \LORIS\Http\Response\JSON\NotFound(); - } - - $filename = $info->getFilename(); - $realpath = $info->getRealPath(); - if ($realpath === false) { - $realpath = $info->getPath() . "/" . $info->getFilename(); - } - - $body = new \LORIS\Http\FileStream($realpath, 'r'); - - return new \LORIS\Http\Response( - $body, - 200, - [ - 'Content-Type' => 'application/text', - 'Content-Disposition' => 'attachment; filename=' . $filename, - ] - ); - } - - /** - * Implements the ETagCalculator interface - * - * @param ServerRequestInterface $request The PSR7 incoming request. - * - * @return string etag summarizing value of this request. - */ - public function ETag(ServerRequestInterface $request) : string - { - $info = $this->_image->getBvecFileInfo(); - - if (!$info->isFile() || !$info->isReadable()) { - return ''; - } - - $signature = [ - 'filename' => $info->getFilename(), - 'size' => $info->getSize(), - 'mtime' => $info->getMTime(), - ]; - - return md5(json_encode($signature)); - } -} - diff --git a/modules/api/php/endpoints/candidate/visit/image/format/thumbnail.class.inc b/modules/api/php/endpoints/candidate/visit/image/format/downloadfile.class.inc similarity index 62% rename from modules/api/php/endpoints/candidate/visit/image/format/thumbnail.class.inc rename to modules/api/php/endpoints/candidate/visit/image/format/downloadfile.class.inc index 3bc3e122447..9569f7ef433 100644 --- a/modules/api/php/endpoints/candidate/visit/image/format/thumbnail.class.inc +++ b/modules/api/php/endpoints/candidate/visit/image/format/downloadfile.class.inc @@ -23,7 +23,7 @@ use \LORIS\api\Endpoint; * @license http://www.gnu.org/licenses/gpl-3.0.txt GPLv3 * @link https://github.com/aces/Loris */ -class Thumbnail extends Endpoint implements \LORIS\Middleware\ETagCalculator +class DownloadFile extends Endpoint implements \LORIS\Middleware\ETagCalculator { /** @@ -32,15 +32,17 @@ class Thumbnail extends Endpoint implements \LORIS\Middleware\ETagCalculator * @var \LORIS\Image */ private $_image; + private $_format; /** * Contructor * * @param \LORIS\Image $image The requested image */ - public function __construct(\LORIS\Image $image) + public function __construct(\LORIS\Image $image, string $format) { $this->_image = $image; + $this->_format = $format; } /** @@ -100,20 +102,47 @@ class Thumbnail extends Endpoint implements \LORIS\Middleware\ETagCalculator */ private function _handleGET(): ResponseInterface { - $info = $this->_image->getThumbnailFileInfo(); + switch ($this->_format) { + case 'thumbnail': + $info = $this->_image->getThumbnailFileInfo(); + $file_type = "Thumbnail"; + $content_type = 'image/jpeg'; + break; + case 'nifti': + $info = $this->_image->getNiiFileInfo(); + $file_type = "NIfTI"; + $content_type = 'application/octet-stream'; + break; + case 'bval': + $info = $this->_image->getBvalFileInfo(); + $file_type = "NIfTI BVAL"; + $content_type = 'application/text'; + break; + case 'bvec': + $info = $this->_image->getBvecFileInfo(); + $file_type = "NIfTI BVEC"; + $content_type = 'application/text'; + break; + case 'bidsjson': + $info = $this->_image->getBidsJsonFileInfo(); + $file_type = "BIDS JSON"; + $content_type = 'application/json'; + break; + default: + return new \LORIS\Http\Response\JSON\UnsupportedMediaType(); + } if (!$info->isFile()) { - error_log('Thumbnail not found'); + error_log("$file_type file not found"); return new \LORIS\Http\Response\JSON\NotFound(); } if (!$info->isReadable()) { - error_log('Thumbnail exists but is not readable by webserver'); + error_log("$file_type file exists but is not readable by webserver"); return new \LORIS\Http\Response\JSON\NotFound(); } $filename = $info->getFilename(); - $realpath = $info->getRealPath(); if ($realpath === false) { $realpath = $info->getPath() . "/" . $info->getFilename(); @@ -125,7 +154,7 @@ class Thumbnail extends Endpoint implements \LORIS\Middleware\ETagCalculator $body, 200, [ - 'Content-Type' => 'image/jpeg', + 'Content-Type' => $content_type, 'Content-Disposition' => 'attachment; filename=' . $filename, ] ); @@ -140,7 +169,25 @@ class Thumbnail extends Endpoint implements \LORIS\Middleware\ETagCalculator */ public function ETag(ServerRequestInterface $request) : string { - $info = $this->_image->getThumbnailFileInfo(); + switch ($this->_format) { + case 'thumbnail': + $info = $this->_image->getThumbnailFileInfo(); + break; + case 'nifti': + $info = $this->_image->getNiiFileInfo(); + break; + case 'bval': + $info = $this->_image->getBvalFileInfo(); + break; + case 'bvec': + $info = $this->_image->getBvecFileInfo(); + break; + case 'bidsjson': + $info = $this->_image->getBidsJsonFileInfo(); + break; + default: + return ''; + } if (!$info->isFile() || !$info->isReadable()) { return ''; diff --git a/modules/api/php/endpoints/candidate/visit/image/format/nifti.class.inc b/modules/api/php/endpoints/candidate/visit/image/format/nifti.class.inc deleted file mode 100644 index df57a64ae3a..00000000000 --- a/modules/api/php/endpoints/candidate/visit/image/format/nifti.class.inc +++ /dev/null @@ -1,157 +0,0 @@ - - * @license http://www.gnu.org/licenses/gpl-3.0.txt GPLv3 - * @link https://github.com/aces/Loris - */ -namespace LORIS\api\Endpoints\Candidate\Visit\Image\Format; - -use \Psr\Http\Message\ServerRequestInterface; -use \Psr\Http\Message\ResponseInterface; -use \LORIS\api\Endpoint; - -/** - * This class handles request to an image thumbnail format. - * - * @category API - * @package Loris - * @author Xavier Lecours Boucher - * @license http://www.gnu.org/licenses/gpl-3.0.txt GPLv3 - * @link https://github.com/aces/Loris - */ -class NIfTI extends Endpoint implements \LORIS\Middleware\ETagCalculator -{ - - /** - * The requested Image - * - * @var \LORIS\Image - */ - private $_image; - - /** - * Contructor - * - * @param \LORIS\Image $image The requested image - */ - public function __construct(\LORIS\Image $image) - { - $this->_image = $image; - } - - /** - * Return which methods are supported by this endpoint. - * - * @return array supported HTTP methods - */ - protected function allowedMethods() : array - { - return ['GET']; - } - - /** - * Versions of the LORIS API which are supported by this - * endpoint. - * - * @return array a list of supported API versions. - */ - protected function supportedVersions() : array - { - return [ - 'v0.0.3', - 'v0.0.4-dev', - ]; - } - - /** - * Delegates the request to a private handler appropriate to the request's - * method. - * - * @param ServerRequestInterface $request The incoming PSR7 request - * - * @return ResponseInterface The outgoing PSR7 response - */ - public function handle(ServerRequestInterface $request) : ResponseInterface - { - switch ($request->getMethod()) { - case 'GET': - return $this->_handleGET($request); - - case 'OPTIONS': - return (new \LORIS\Http\Response()) - ->withHeader('Allow', $this->allowedMethods()); - - default: - return new \LORIS\Http\Response\JSON\MethodNotAllowed( - $this->allowedMethods() - ); - - } - } - - /** - * Creates a response with the thumbnail as body - * - * @return ResponseInterface The outgoing PSR7 response - */ - private function _handleGET(): ResponseInterface - { - $info = $this->_image->getNiiFileInfo(); - - if (!$info->isFile()) { - error_log('NIfTI file not found'); - return new \LORIS\Http\Response\JSON\NotFound(); - } - - if (!$info->isReadable()) { - error_log('NIfTI file exists but is not readable by webserver'); - return new \LORIS\Http\Response\JSON\NotFound(); - } - - $filename = $info->getFilename(); - $realpath = $info->getRealPath(); - if ($realpath === false) { - $realpath = $info->getPath() . "/" . $info->getFilename(); - } - - $body = new \LORIS\Http\FileStream($realpath, 'r'); - - return new \LORIS\Http\Response( - $body, - 200, - [ - 'Content-Type' => 'application/octet-stream', - 'Content-Disposition' => 'attachment; filename=' . $filename, - ] - ); - } - - /** - * Implements the ETagCalculator interface - * - * @param ServerRequestInterface $request The PSR7 incoming request. - * - * @return string etag summarizing value of this request. - */ - public function ETag(ServerRequestInterface $request) : string - { - $info = $this->_image->getNiiFileInfo(); - - if (!$info->isFile() || !$info->isReadable()) { - return ''; - } - - $signature = [ - 'filename' => $info->getFilename(), - 'size' => $info->getSize(), - 'mtime' => $info->getMTime(), - ]; - - return md5(json_encode($signature)); - } -} - From 8bf64cf6278c9fa57fc585ce5a36f01e122e72ec Mon Sep 17 00:00:00 2001 From: cmadjar Date: Thu, 2 Feb 2023 16:12:55 -0500 Subject: [PATCH 06/24] move switch to the constructor of the class --- .../candidate/visit/image/format.class.inc | 1 - .../visit/image/format/downloadfile.class.inc | 106 ++++++++---------- 2 files changed, 46 insertions(+), 61 deletions(-) diff --git a/modules/api/php/endpoints/candidate/visit/image/format.class.inc b/modules/api/php/endpoints/candidate/visit/image/format.class.inc index 7f3bf2a98b7..781df707249 100644 --- a/modules/api/php/endpoints/candidate/visit/image/format.class.inc +++ b/modules/api/php/endpoints/candidate/visit/image/format.class.inc @@ -94,7 +94,6 @@ class Format extends Endpoint case 'bval': case 'bvec': case 'bidsjson': - error_log(print_r($format, true)); $handler = new Format\DownloadFile($this->_image, $format); break; default: diff --git a/modules/api/php/endpoints/candidate/visit/image/format/downloadfile.class.inc b/modules/api/php/endpoints/candidate/visit/image/format/downloadfile.class.inc index 9569f7ef433..58fbf3d9a27 100644 --- a/modules/api/php/endpoints/candidate/visit/image/format/downloadfile.class.inc +++ b/modules/api/php/endpoints/candidate/visit/image/format/downloadfile.class.inc @@ -33,6 +33,9 @@ class DownloadFile extends Endpoint implements \LORIS\Middleware\ETagCalculator */ private $_image; private $_format; + private $_info; + private $_file_type; + private $_content_type; /** * Contructor @@ -43,6 +46,36 @@ class DownloadFile extends Endpoint implements \LORIS\Middleware\ETagCalculator { $this->_image = $image; $this->_format = $format; + + switch ($this->_format) { + case 'thumbnail': + $this->_info = $this->_image->getThumbnailFileInfo(); + $this->_file_type = "Thumbnail"; + $this->_content_type = 'image/jpeg'; + break; + case 'nifti': + $this->_info = $this->_image->getNiiFileInfo(); + $this->_file_type = "NIfTI"; + $this->_content_type = 'application/octet-stream'; + break; + case 'bval': + $this->_info = $this->_image->getBvalFileInfo(); + $this->_file_type = "NIfTI BVAL"; + $this->_content_type = 'application/text'; + break; + case 'bvec': + $this->_info = $this->_image->getBvecFileInfo(); + $this->_file_type = "NIfTI BVEC"; + $this->_content_type = 'application/text'; + break; + case 'bidsjson': + $this->_info = $this->_image->getBidsJsonFileInfo(); + $this->_file_type = "BIDS JSON"; + $this->_content_type = 'application/json'; + break; + default: + return new \LORIS\Http\Response\JSON\UnsupportedMediaType(); + } } /** @@ -102,50 +135,22 @@ class DownloadFile extends Endpoint implements \LORIS\Middleware\ETagCalculator */ private function _handleGET(): ResponseInterface { - switch ($this->_format) { - case 'thumbnail': - $info = $this->_image->getThumbnailFileInfo(); - $file_type = "Thumbnail"; - $content_type = 'image/jpeg'; - break; - case 'nifti': - $info = $this->_image->getNiiFileInfo(); - $file_type = "NIfTI"; - $content_type = 'application/octet-stream'; - break; - case 'bval': - $info = $this->_image->getBvalFileInfo(); - $file_type = "NIfTI BVAL"; - $content_type = 'application/text'; - break; - case 'bvec': - $info = $this->_image->getBvecFileInfo(); - $file_type = "NIfTI BVEC"; - $content_type = 'application/text'; - break; - case 'bidsjson': - $info = $this->_image->getBidsJsonFileInfo(); - $file_type = "BIDS JSON"; - $content_type = 'application/json'; - break; - default: - return new \LORIS\Http\Response\JSON\UnsupportedMediaType(); - } - if (!$info->isFile()) { - error_log("$file_type file not found"); + + if (!$this->_info->isFile()) { + error_log("$this->_file_type file not found"); return new \LORIS\Http\Response\JSON\NotFound(); } - if (!$info->isReadable()) { - error_log("$file_type file exists but is not readable by webserver"); + if (!$this->_info->isReadable()) { + error_log("$this->_file_type file exists but is not readable by webserver"); return new \LORIS\Http\Response\JSON\NotFound(); } - $filename = $info->getFilename(); - $realpath = $info->getRealPath(); + $filename = $this->_info->getFilename(); + $realpath = $this->_info->getRealPath(); if ($realpath === false) { - $realpath = $info->getPath() . "/" . $info->getFilename(); + $realpath = $this->_info->getPath() . "/" . $this->_info->getFilename(); } $body = new \LORIS\Http\FileStream($realpath, 'r'); @@ -154,7 +159,7 @@ class DownloadFile extends Endpoint implements \LORIS\Middleware\ETagCalculator $body, 200, [ - 'Content-Type' => $content_type, + 'Content-Type' => $this->_content_type, 'Content-Disposition' => 'attachment; filename=' . $filename, ] ); @@ -169,34 +174,15 @@ class DownloadFile extends Endpoint implements \LORIS\Middleware\ETagCalculator */ public function ETag(ServerRequestInterface $request) : string { - switch ($this->_format) { - case 'thumbnail': - $info = $this->_image->getThumbnailFileInfo(); - break; - case 'nifti': - $info = $this->_image->getNiiFileInfo(); - break; - case 'bval': - $info = $this->_image->getBvalFileInfo(); - break; - case 'bvec': - $info = $this->_image->getBvecFileInfo(); - break; - case 'bidsjson': - $info = $this->_image->getBidsJsonFileInfo(); - break; - default: - return ''; - } - if (!$info->isFile() || !$info->isReadable()) { + if (!$this->_info->isFile() || !$this->_info->isReadable()) { return ''; } $signature = [ - 'filename' => $info->getFilename(), - 'size' => $info->getSize(), - 'mtime' => $info->getMTime(), + 'filename' => $this->_info->getFilename(), + 'size' => $this->_info->getSize(), + 'mtime' => $this->_info->getMTime(), ]; return md5(json_encode($signature)); From 46aa3988f00e0360a2bad253a819ed350257178e Mon Sep 17 00:00:00 2001 From: cmadjar Date: Thu, 2 Feb 2023 16:27:58 -0500 Subject: [PATCH 07/24] fix phpcbf --- .../visit/image/format/downloadfile.class.inc | 57 ++++++++++--------- 1 file changed, 29 insertions(+), 28 deletions(-) diff --git a/modules/api/php/endpoints/candidate/visit/image/format/downloadfile.class.inc b/modules/api/php/endpoints/candidate/visit/image/format/downloadfile.class.inc index 58fbf3d9a27..4c6dd9a0886 100644 --- a/modules/api/php/endpoints/candidate/visit/image/format/downloadfile.class.inc +++ b/modules/api/php/endpoints/candidate/visit/image/format/downloadfile.class.inc @@ -41,40 +41,41 @@ class DownloadFile extends Endpoint implements \LORIS\Middleware\ETagCalculator * Contructor * * @param \LORIS\Image $image The requested image + * @param string $format The format user wishes to download */ public function __construct(\LORIS\Image $image, string $format) { - $this->_image = $image; + $this->_image = $image; $this->_format = $format; switch ($this->_format) { - case 'thumbnail': - $this->_info = $this->_image->getThumbnailFileInfo(); - $this->_file_type = "Thumbnail"; - $this->_content_type = 'image/jpeg'; - break; - case 'nifti': - $this->_info = $this->_image->getNiiFileInfo(); - $this->_file_type = "NIfTI"; - $this->_content_type = 'application/octet-stream'; - break; - case 'bval': - $this->_info = $this->_image->getBvalFileInfo(); - $this->_file_type = "NIfTI BVAL"; - $this->_content_type = 'application/text'; - break; - case 'bvec': - $this->_info = $this->_image->getBvecFileInfo(); - $this->_file_type = "NIfTI BVEC"; - $this->_content_type = 'application/text'; - break; - case 'bidsjson': - $this->_info = $this->_image->getBidsJsonFileInfo(); - $this->_file_type = "BIDS JSON"; - $this->_content_type = 'application/json'; - break; - default: - return new \LORIS\Http\Response\JSON\UnsupportedMediaType(); + case 'thumbnail': + $this->_info = $this->_image->getThumbnailFileInfo(); + $this->_file_type = "Thumbnail"; + $this->_content_type = 'image/jpeg'; + break; + case 'nifti': + $this->_info = $this->_image->getNiiFileInfo(); + $this->_file_type = "NIfTI"; + $this->_content_type = 'application/octet-stream'; + break; + case 'bval': + $this->_info = $this->_image->getBvalFileInfo(); + $this->_file_type = "NIfTI BVAL"; + $this->_content_type = 'application/text'; + break; + case 'bvec': + $this->_info = $this->_image->getBvecFileInfo(); + $this->_file_type = "NIfTI BVEC"; + $this->_content_type = 'application/text'; + break; + case 'bidsjson': + $this->_info = $this->_image->getBidsJsonFileInfo(); + $this->_file_type = "BIDS JSON"; + $this->_content_type = 'application/json'; + break; + default: + return new \LORIS\Http\Response\JSON\UnsupportedMediaType(); } } From e18586b9a6340f1e663022b2d181346853de9d72 Mon Sep 17 00:00:00 2001 From: cmadjar Date: Thu, 2 Feb 2023 16:36:18 -0500 Subject: [PATCH 08/24] fix phpcbf --- .../visit/image/format/downloadfile.class.inc | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/modules/api/php/endpoints/candidate/visit/image/format/downloadfile.class.inc b/modules/api/php/endpoints/candidate/visit/image/format/downloadfile.class.inc index 4c6dd9a0886..6d9df7660d0 100644 --- a/modules/api/php/endpoints/candidate/visit/image/format/downloadfile.class.inc +++ b/modules/api/php/endpoints/candidate/visit/image/format/downloadfile.class.inc @@ -40,8 +40,8 @@ class DownloadFile extends Endpoint implements \LORIS\Middleware\ETagCalculator /** * Contructor * - * @param \LORIS\Image $image The requested image - * @param string $format The format user wishes to download + * @param \LORIS\Image $image The requested image + * @param string $format The format user wishes to download */ public function __construct(\LORIS\Image $image, string $format) { @@ -136,15 +136,15 @@ class DownloadFile extends Endpoint implements \LORIS\Middleware\ETagCalculator */ private function _handleGET(): ResponseInterface { - - if (!$this->_info->isFile()) { error_log("$this->_file_type file not found"); return new \LORIS\Http\Response\JSON\NotFound(); } if (!$this->_info->isReadable()) { - error_log("$this->_file_type file exists but is not readable by webserver"); + error_log( + "$this->_file_type file exists but is not readable by webserver" + ); return new \LORIS\Http\Response\JSON\NotFound(); } From 998b34f6776f72c49de083f7e5c3ee54f0ce423e Mon Sep 17 00:00:00 2001 From: cmadjar Date: Thu, 2 Feb 2023 16:42:42 -0500 Subject: [PATCH 09/24] fix phpcbf --- .../candidate/visit/image/format/downloadfile.class.inc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/api/php/endpoints/candidate/visit/image/format/downloadfile.class.inc b/modules/api/php/endpoints/candidate/visit/image/format/downloadfile.class.inc index 6d9df7660d0..f74df138d5d 100644 --- a/modules/api/php/endpoints/candidate/visit/image/format/downloadfile.class.inc +++ b/modules/api/php/endpoints/candidate/visit/image/format/downloadfile.class.inc @@ -40,8 +40,8 @@ class DownloadFile extends Endpoint implements \LORIS\Middleware\ETagCalculator /** * Contructor * - * @param \LORIS\Image $image The requested image - * @param string $format The format user wishes to download + * @param \LORIS\Image $image The requested image + * @param string $format The format user wishes to download */ public function __construct(\LORIS\Image $image, string $format) { From d0eb91ff55df5923f346a7e70520c173431ff5c4 Mon Sep 17 00:00:00 2001 From: cmadjar Date: Thu, 2 Feb 2023 16:55:03 -0500 Subject: [PATCH 10/24] fix phpcbf --- .../candidate/visit/image/format/downloadfile.class.inc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/api/php/endpoints/candidate/visit/image/format/downloadfile.class.inc b/modules/api/php/endpoints/candidate/visit/image/format/downloadfile.class.inc index f74df138d5d..7bb1a1df972 100644 --- a/modules/api/php/endpoints/candidate/visit/image/format/downloadfile.class.inc +++ b/modules/api/php/endpoints/candidate/visit/image/format/downloadfile.class.inc @@ -40,7 +40,7 @@ class DownloadFile extends Endpoint implements \LORIS\Middleware\ETagCalculator /** * Contructor * - * @param \LORIS\Image $image The requested image + * @param \LORIS\Image $image The requested image * @param string $format The format user wishes to download */ public function __construct(\LORIS\Image $image, string $format) From 03a3d421495dc49b56db182ed40eca60bfb17462 Mon Sep 17 00:00:00 2001 From: cmadjar Date: Thu, 2 Feb 2023 17:00:41 -0500 Subject: [PATCH 11/24] fix phpcbf --- .../candidate/visit/image/format/downloadfile.class.inc | 2 -- 1 file changed, 2 deletions(-) diff --git a/modules/api/php/endpoints/candidate/visit/image/format/downloadfile.class.inc b/modules/api/php/endpoints/candidate/visit/image/format/downloadfile.class.inc index 7bb1a1df972..1274da6760c 100644 --- a/modules/api/php/endpoints/candidate/visit/image/format/downloadfile.class.inc +++ b/modules/api/php/endpoints/candidate/visit/image/format/downloadfile.class.inc @@ -74,8 +74,6 @@ class DownloadFile extends Endpoint implements \LORIS\Middleware\ETagCalculator $this->_file_type = "BIDS JSON"; $this->_content_type = 'application/json'; break; - default: - return new \LORIS\Http\Response\JSON\UnsupportedMediaType(); } } From 8c1a23660c18ef71c5ef1c26cf2dc031a2bc1d99 Mon Sep 17 00:00:00 2001 From: cmadjar Date: Thu, 2 Feb 2023 18:27:16 -0500 Subject: [PATCH 12/24] Xavier's feedback I missed --- php/libraries/Image.class.inc | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/php/libraries/Image.class.inc b/php/libraries/Image.class.inc index 9f503c4c730..57239948690 100644 --- a/php/libraries/Image.class.inc +++ b/php/libraries/Image.class.inc @@ -89,10 +89,6 @@ class Image $this->_filetype = $dbrow['filetype']; $this->_centerid = new \CenterID($dbrow['centerid']); $this->_entitytype = $dbrow['entitytype']; - $this->_check_nii_filename = $this->getHeader('check_nii_filename'); - $this->_check_bval_filename = $this->getHeader('check_bval_filename'); - $this->_check_bvec_filename = $this->getHeader('check_bvec_filename'); - $this->_bids_json_filename = $this->getHeader('bids_json_file'); } } @@ -350,6 +346,11 @@ class Image */ public function asDTO(): \LORIS\Data\Models\ImageDTO { + $this->_check_nii_filename = $this->getHeader('check_nii_filename'); + $this->_check_bval_filename = $this->getHeader('check_bval_filename'); + $this->_check_bvec_filename = $this->getHeader('check_bvec_filename'); + $this->_bids_json_filename = $this->getHeader('bids_json_file'); + return new \LORIS\Data\Models\ImageDTO( $this->_fileid, $this->_filename, From 3f86d7050dd7049c49afaf69cf36109910d5cbeb Mon Sep 17 00:00:00 2001 From: cmadjar Date: Thu, 2 Feb 2023 18:29:23 -0500 Subject: [PATCH 13/24] revert last commit --- php/libraries/Image.class.inc | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/php/libraries/Image.class.inc b/php/libraries/Image.class.inc index 57239948690..9f503c4c730 100644 --- a/php/libraries/Image.class.inc +++ b/php/libraries/Image.class.inc @@ -89,6 +89,10 @@ class Image $this->_filetype = $dbrow['filetype']; $this->_centerid = new \CenterID($dbrow['centerid']); $this->_entitytype = $dbrow['entitytype']; + $this->_check_nii_filename = $this->getHeader('check_nii_filename'); + $this->_check_bval_filename = $this->getHeader('check_bval_filename'); + $this->_check_bvec_filename = $this->getHeader('check_bvec_filename'); + $this->_bids_json_filename = $this->getHeader('bids_json_file'); } } @@ -346,11 +350,6 @@ class Image */ public function asDTO(): \LORIS\Data\Models\ImageDTO { - $this->_check_nii_filename = $this->getHeader('check_nii_filename'); - $this->_check_bval_filename = $this->getHeader('check_bval_filename'); - $this->_check_bvec_filename = $this->getHeader('check_bvec_filename'); - $this->_bids_json_filename = $this->getHeader('bids_json_file'); - return new \LORIS\Data\Models\ImageDTO( $this->_fileid, $this->_filename, From 1cdce2729abe6d8a1c7027c1e58e0993fa1cbc77 Mon Sep 17 00:00:00 2001 From: cmadjar Date: Fri, 3 Feb 2023 08:36:01 -0500 Subject: [PATCH 14/24] Xavier's feedback after a good night sleep --- php/libraries/Image.class.inc | 20 ++++---------------- 1 file changed, 4 insertions(+), 16 deletions(-) diff --git a/php/libraries/Image.class.inc b/php/libraries/Image.class.inc index 9f503c4c730..21ab849a155 100644 --- a/php/libraries/Image.class.inc +++ b/php/libraries/Image.class.inc @@ -39,14 +39,6 @@ class Image private $_entitytype; - private $_check_nii_filename; - - private $_check_bval_filename; - - private $_check_bvec_filename; - - private $_bids_json_filename; - /** * Constructor * @@ -89,10 +81,6 @@ class Image $this->_filetype = $dbrow['filetype']; $this->_centerid = new \CenterID($dbrow['centerid']); $this->_entitytype = $dbrow['entitytype']; - $this->_check_nii_filename = $this->getHeader('check_nii_filename'); - $this->_check_bval_filename = $this->getHeader('check_bval_filename'); - $this->_check_bvec_filename = $this->getHeader('check_bvec_filename'); - $this->_bids_json_filename = $this->getHeader('bids_json_file'); } } @@ -272,7 +260,7 @@ class Image */ public function getNiiFileInfo(): \SplFileInfo { - return $this->_getFullPath($this->_check_nii_filename); + return $this->_getFullPath($this->getHeader('check_nii_filename')); } /** @@ -282,7 +270,7 @@ class Image */ public function getBvalFileInfo(): \SplFileInfo { - return $this->_getFullPath($this->_check_bval_filename); + return $this->_getFullPath($this->getHeader('check_bval_filename')); } /** @@ -292,7 +280,7 @@ class Image */ public function getBvecFileInfo(): \SplFileInfo { - return $this->_getFullPath($this->_check_bvec_filename); + return $this->_getFullPath($this->getHeader('check_bvec_filename')); } /** @@ -302,7 +290,7 @@ class Image */ public function getBidsJsonFileInfo(): \SplFileInfo { - return $this->_getFullPath($this->_bids_json_filename); + return $this->_getFullPath($this->getHeader('bids_json_file')); } /** From 614b334551c15cc443bd072479e9003456809a17 Mon Sep 17 00:00:00 2001 From: cmadjar Date: Fri, 3 Feb 2023 08:39:28 -0500 Subject: [PATCH 15/24] remove all ->_check variables --- php/libraries/Image.class.inc | 6 +--- src/Data/Models/ImageDTO.php | 60 ----------------------------------- 2 files changed, 1 insertion(+), 65 deletions(-) diff --git a/php/libraries/Image.class.inc b/php/libraries/Image.class.inc index 21ab849a155..0c564144281 100644 --- a/php/libraries/Image.class.inc +++ b/php/libraries/Image.class.inc @@ -346,11 +346,7 @@ class Image $this->_acquisitionprotocol, $this->_filetype, $this->_centerid, - $this->_entitytype, - $this->_check_nii_filename, - $this->_check_bval_filename, - $this->_check_bvec_filename, - $this->_bids_json_filename + $this->_entitytype ); } } diff --git a/src/Data/Models/ImageDTO.php b/src/Data/Models/ImageDTO.php index c64cae52b01..94b184121b2 100644 --- a/src/Data/Models/ImageDTO.php +++ b/src/Data/Models/ImageDTO.php @@ -43,14 +43,6 @@ class ImageDTO implements private $entitytype; - private $check_nii_filename; - - private $check_bval_filename; - - private $check_bvec_filename; - - private $bids_json_filename; - /** * Constructor * @@ -62,10 +54,6 @@ class ImageDTO implements * @param ?string $filetype The file type * @param \CenterID $centerid The image session's centerid * @param ?string $entitytype The image candidate's entity_type - * @param ?string $check_nii_filename The NIfTI file generated by mnc2nii - * @param ?string $check_bval_filename The NIfTI bval file for DWI - * @param ?string $check_bvec_filename The NIfTI bvec file for DWI - * @param ?string $bids_json_filename The BIDS JSON file */ public function __construct( ?int $fileid, @@ -76,10 +64,6 @@ public function __construct( ?string $filetype, \CenterID $centerid, ?string $entitytype, - ?string $check_nii_filename, - ?string $check_bval_filename, - ?string $check_bvec_filename, - ?string $bids_json_filename ) { $this->fileid = $fileid; $this->filename = $filename; @@ -89,10 +73,6 @@ public function __construct( $this->filetype = $filetype; $this->centerid = $centerid; $this->entitytype = $entitytype; - $this->check_nii_filename = $check_nii_filename; - $this->check_bval_filename = $check_bval_filename; - $this->check_bvec_filename = $check_bvec_filename; - $this->bids_json_filename = $bids_json_filename; } /** @@ -192,44 +172,4 @@ public function isPhantom(): bool { return $this->entitytype === 'Scanner'; } - - /** - * Accessor for NIfTI filename stored in parameter_file. - * - * @return ?string - */ - public function getCheckNiiFilename(): ?string - { - return $this->check_nii_filename; - } - - /** - * Accessor for NIfTI BVAL filename. - * - * @return ?string - */ - public function getCheckBvalFilename(): ?string - { - return $this->check_bval_filename; - } - - /** - * Accessor for NIfTI BVEC filename. - * - * @return ?string - */ - public function getCheckBvecFilename(): ?string - { - return $this->check_bvec_filename; - } - - /** - * Accessor for BIDS JSON filename. - * - * @return ?string - */ - public function getBidsJsonFilename(): ?string - { - return $this->bids_json_filename; - } } From 0de005ec81c39ec03e9c7a56ebc984d0e4443db0 Mon Sep 17 00:00:00 2001 From: cmadjar Date: Fri, 3 Feb 2023 08:40:38 -0500 Subject: [PATCH 16/24] remove unecessary extra comma --- src/Data/Models/ImageDTO.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Data/Models/ImageDTO.php b/src/Data/Models/ImageDTO.php index 94b184121b2..c1b7f77ba86 100644 --- a/src/Data/Models/ImageDTO.php +++ b/src/Data/Models/ImageDTO.php @@ -148,7 +148,7 @@ public function jsonSerialize() : array 'filelocation' => $this->filelocation, 'outputtype' => $this->outputtype, 'acquisitionprotocol' => $this->acquisitionprotocol, - 'filetype' => $this->filetype, + 'filetype' => $this->filetype ]; } From c1e92b2182826e6ffa95d4db8105f47729e9f60f Mon Sep 17 00:00:00 2001 From: cmadjar Date: Fri, 3 Feb 2023 08:41:30 -0500 Subject: [PATCH 17/24] remove unecessary extra comma --- src/Data/Models/ImageDTO.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Data/Models/ImageDTO.php b/src/Data/Models/ImageDTO.php index c1b7f77ba86..65fd77b2c32 100644 --- a/src/Data/Models/ImageDTO.php +++ b/src/Data/Models/ImageDTO.php @@ -52,7 +52,7 @@ class ImageDTO implements * @param ?string $outputtype The output type * @param ?string $acquisitionprotocol The aquisition protocol * @param ?string $filetype The file type - * @param \CenterID $centerid The image session's centerid + * @param \CenterID $centerid The image session's centerid * @param ?string $entitytype The image candidate's entity_type */ public function __construct( @@ -63,7 +63,7 @@ public function __construct( ?string $acquisitionprotocol, ?string $filetype, \CenterID $centerid, - ?string $entitytype, + ?string $entitytype ) { $this->fileid = $fileid; $this->filename = $filename; @@ -148,7 +148,7 @@ public function jsonSerialize() : array 'filelocation' => $this->filelocation, 'outputtype' => $this->outputtype, 'acquisitionprotocol' => $this->acquisitionprotocol, - 'filetype' => $this->filetype + 'filetype' => $this->filetype, ]; } From a76d31cdb3d2449120290aeb0b53d2bd813a900c Mon Sep 17 00:00:00 2001 From: cmadjar Date: Fri, 10 Feb 2023 10:22:07 -0500 Subject: [PATCH 18/24] Xavier's feedback --- .../visit/image/format/downloadfile.class.inc | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/modules/api/php/endpoints/candidate/visit/image/format/downloadfile.class.inc b/modules/api/php/endpoints/candidate/visit/image/format/downloadfile.class.inc index 1274da6760c..87694bd4bca 100644 --- a/modules/api/php/endpoints/candidate/visit/image/format/downloadfile.class.inc +++ b/modules/api/php/endpoints/candidate/visit/image/format/downloadfile.class.inc @@ -31,7 +31,6 @@ class DownloadFile extends Endpoint implements \LORIS\Middleware\ETagCalculator * * @var \LORIS\Image */ - private $_image; private $_format; private $_info; private $_file_type; @@ -45,32 +44,31 @@ class DownloadFile extends Endpoint implements \LORIS\Middleware\ETagCalculator */ public function __construct(\LORIS\Image $image, string $format) { - $this->_image = $image; $this->_format = $format; switch ($this->_format) { case 'thumbnail': - $this->_info = $this->_image->getThumbnailFileInfo(); + $this->_info = $image->getThumbnailFileInfo(); $this->_file_type = "Thumbnail"; $this->_content_type = 'image/jpeg'; break; case 'nifti': - $this->_info = $this->_image->getNiiFileInfo(); + $this->_info = $image->getNiiFileInfo(); $this->_file_type = "NIfTI"; $this->_content_type = 'application/octet-stream'; break; case 'bval': - $this->_info = $this->_image->getBvalFileInfo(); + $this->_info = $image->getBvalFileInfo(); $this->_file_type = "NIfTI BVAL"; $this->_content_type = 'application/text'; break; case 'bvec': - $this->_info = $this->_image->getBvecFileInfo(); + $this->_info = $image->getBvecFileInfo(); $this->_file_type = "NIfTI BVEC"; $this->_content_type = 'application/text'; break; case 'bidsjson': - $this->_info = $this->_image->getBidsJsonFileInfo(); + $this->_info = $image->getBidsJsonFileInfo(); $this->_file_type = "BIDS JSON"; $this->_content_type = 'application/json'; break; From a24a5da6d7eead18aff260dc1ddf8a0315a70fd4 Mon Sep 17 00:00:00 2001 From: cmadjar Date: Fri, 10 Feb 2023 10:33:15 -0500 Subject: [PATCH 19/24] Make Phan happy --- .../candidate/visit/image/format/downloadfile.class.inc | 6 ------ 1 file changed, 6 deletions(-) diff --git a/modules/api/php/endpoints/candidate/visit/image/format/downloadfile.class.inc b/modules/api/php/endpoints/candidate/visit/image/format/downloadfile.class.inc index 87694bd4bca..4f9f528457a 100644 --- a/modules/api/php/endpoints/candidate/visit/image/format/downloadfile.class.inc +++ b/modules/api/php/endpoints/candidate/visit/image/format/downloadfile.class.inc @@ -25,12 +25,6 @@ use \LORIS\api\Endpoint; */ class DownloadFile extends Endpoint implements \LORIS\Middleware\ETagCalculator { - - /** - * The requested Image - * - * @var \LORIS\Image - */ private $_format; private $_info; private $_file_type; From f6e54ff020ff57eaa3334afbff6ad138905b2498 Mon Sep 17 00:00:00 2001 From: cmadjar Date: Mon, 13 Feb 2023 13:01:49 -0500 Subject: [PATCH 20/24] PSR --- .../visit/image/format/downloadfile.class.inc | 4 ++-- src/Http/Endpoint.php | 10 ++++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/modules/api/php/endpoints/candidate/visit/image/format/downloadfile.class.inc b/modules/api/php/endpoints/candidate/visit/image/format/downloadfile.class.inc index 4f9f528457a..8c457f50550 100644 --- a/modules/api/php/endpoints/candidate/visit/image/format/downloadfile.class.inc +++ b/modules/api/php/endpoints/candidate/visit/image/format/downloadfile.class.inc @@ -127,12 +127,12 @@ class DownloadFile extends Endpoint implements \LORIS\Middleware\ETagCalculator private function _handleGET(): ResponseInterface { if (!$this->_info->isFile()) { - error_log("$this->_file_type file not found"); + $this->logger("$this->_file_type file not found"); return new \LORIS\Http\Response\JSON\NotFound(); } if (!$this->_info->isReadable()) { - error_log( + $this->logger( "$this->_file_type file exists but is not readable by webserver" ); return new \LORIS\Http\Response\JSON\NotFound(); diff --git a/src/Http/Endpoint.php b/src/Http/Endpoint.php index b0c61682422..d2a7b9ae585 100644 --- a/src/Http/Endpoint.php +++ b/src/Http/Endpoint.php @@ -16,6 +16,7 @@ use \Psr\Http\Message\ServerRequestInterface; use \Psr\Http\Server\RequestHandlerInterface; use \Psr\Http\Message\ResponseInterface; +use \Psr\Log\LoggerAwareTrait; /** * An abstract class for common concerns of different API endpoints. @@ -28,6 +29,8 @@ */ abstract class Endpoint implements RequestHandlerInterface { + private \LORIS\Log\ErrorLogLogger $logger; + /** * An Endpoint overrides the default LORIS middleware to remove the * PageDecorationMiddleware. @@ -46,6 +49,13 @@ public function process( return (new \LORIS\Middleware\ETag())->process($request, $handler); } + $loris = $request->getAttribute('loris'); + $loglevel = $loris->getConfiguration() + ->getLogSettings() + ->getRequestLogLevel(); + + $this->logger = new \LORIS\Log\ErrorLogLogger($loglevel); + return $handler->handle($request); } From 4fe033a9c309090d7829a05434367a875ecc4cc4 Mon Sep 17 00:00:00 2001 From: cmadjar Date: Mon, 13 Feb 2023 13:15:50 -0500 Subject: [PATCH 21/24] Phan --- src/Http/Endpoint.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Http/Endpoint.php b/src/Http/Endpoint.php index d2a7b9ae585..640115b7d9d 100644 --- a/src/Http/Endpoint.php +++ b/src/Http/Endpoint.php @@ -16,7 +16,6 @@ use \Psr\Http\Message\ServerRequestInterface; use \Psr\Http\Server\RequestHandlerInterface; use \Psr\Http\Message\ResponseInterface; -use \Psr\Log\LoggerAwareTrait; /** * An abstract class for common concerns of different API endpoints. @@ -29,7 +28,7 @@ */ abstract class Endpoint implements RequestHandlerInterface { - private \LORIS\Log\ErrorLogLogger $logger; + use \PSR\Log\LoggerAwareTrait; /** * An Endpoint overrides the default LORIS middleware to remove the From 55c8dfdb83671fde4a07b5373e3bee9127823a0a Mon Sep 17 00:00:00 2001 From: cmadjar Date: Mon, 13 Feb 2023 13:36:57 -0500 Subject: [PATCH 22/24] Phan --- .../candidate/visit/image/format/downloadfile.class.inc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/api/php/endpoints/candidate/visit/image/format/downloadfile.class.inc b/modules/api/php/endpoints/candidate/visit/image/format/downloadfile.class.inc index 8c457f50550..f70dc7bcb2e 100644 --- a/modules/api/php/endpoints/candidate/visit/image/format/downloadfile.class.inc +++ b/modules/api/php/endpoints/candidate/visit/image/format/downloadfile.class.inc @@ -127,12 +127,12 @@ class DownloadFile extends Endpoint implements \LORIS\Middleware\ETagCalculator private function _handleGET(): ResponseInterface { if (!$this->_info->isFile()) { - $this->logger("$this->_file_type file not found"); + $this->logger->notice("$this->_file_type file not found"); return new \LORIS\Http\Response\JSON\NotFound(); } if (!$this->_info->isReadable()) { - $this->logger( + $this->logger->notice( "$this->_file_type file exists but is not readable by webserver" ); return new \LORIS\Http\Response\JSON\NotFound(); From 309f52be40abdcce62e6b5c1ec8a3215c86a5313 Mon Sep 17 00:00:00 2001 From: cmadjar Date: Mon, 13 Feb 2023 13:43:24 -0500 Subject: [PATCH 23/24] Dave's comment --- src/Http/Endpoint.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Http/Endpoint.php b/src/Http/Endpoint.php index 640115b7d9d..856256daa2f 100644 --- a/src/Http/Endpoint.php +++ b/src/Http/Endpoint.php @@ -43,11 +43,6 @@ public function process( ServerRequestInterface $request, RequestHandlerInterface $handler ): ResponseInterface { - $interfaces = class_implements($handler); - if (in_array('LORIS\Middleware\ETagCalculator', $interfaces)) { - return (new \LORIS\Middleware\ETag())->process($request, $handler); - } - $loris = $request->getAttribute('loris'); $loglevel = $loris->getConfiguration() ->getLogSettings() @@ -55,6 +50,11 @@ public function process( $this->logger = new \LORIS\Log\ErrorLogLogger($loglevel); + $interfaces = class_implements($handler); + if (in_array('LORIS\Middleware\ETagCalculator', $interfaces)) { + return (new \LORIS\Middleware\ETag())->process($request, $handler); + } + return $handler->handle($request); } From 27c14a571c01a56fcf18a09ed9fb65da6a545e40 Mon Sep 17 00:00:00 2001 From: cmadjar Date: Mon, 13 Feb 2023 13:45:28 -0500 Subject: [PATCH 24/24] Dave's comment --- .../candidate/visit/image/format/downloadfile.class.inc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/api/php/endpoints/candidate/visit/image/format/downloadfile.class.inc b/modules/api/php/endpoints/candidate/visit/image/format/downloadfile.class.inc index f70dc7bcb2e..3ebd104d33a 100644 --- a/modules/api/php/endpoints/candidate/visit/image/format/downloadfile.class.inc +++ b/modules/api/php/endpoints/candidate/visit/image/format/downloadfile.class.inc @@ -127,12 +127,12 @@ class DownloadFile extends Endpoint implements \LORIS\Middleware\ETagCalculator private function _handleGET(): ResponseInterface { if (!$this->_info->isFile()) { - $this->logger->notice("$this->_file_type file not found"); + $this->logger->error("$this->_file_type file not found"); return new \LORIS\Http\Response\JSON\NotFound(); } if (!$this->_info->isReadable()) { - $this->logger->notice( + $this->logger->error( "$this->_file_type file exists but is not readable by webserver" ); return new \LORIS\Http\Response\JSON\NotFound();