Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[imaging browser] Fix download of JSON, Bval and Bvec files when they are on s3 #8354

Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,12 @@ class Format extends Endpoint
$handler = new Format\Brainbrowser($this->_image);
break;
case 'thumbnail':
$handler = new Format\Thumbnail($this->_image);
case 'nifti':
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

aren't thumbnails intended to be displayed inline?

(It looks like the code already had an attachment Content-Disposition, so I guess it doesn't matter..)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, the thumbnail is mainly used to be displayed in the imaging browser but it was already available for download in the API and that is the file I used to base all the other classes so I kept it. @driusan does it answer your question?

case 'bval':
case 'bvec':
case 'bidsjson':
error_log(print_r($format, true));
xlecours marked this conversation as resolved.
Show resolved Hide resolved
$handler = new Format\DownloadFile($this->_image, $format);
break;
default:
return new \LORIS\Http\Response\JSON\UnsupportedMediaType();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{

/**
Expand All @@ -32,15 +32,17 @@ class Thumbnail extends Endpoint implements \LORIS\Middleware\ETagCalculator
* @var \LORIS\Image
*/
private $_image;
xlecours marked this conversation as resolved.
Show resolved Hide resolved
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;
}

/**
Expand Down Expand Up @@ -100,20 +102,47 @@ class Thumbnail extends Endpoint implements \LORIS\Middleware\ETagCalculator
*/
private function _handleGET(): ResponseInterface
{
$info = $this->_image->getThumbnailFileInfo();
switch ($this->_format) {
xlecours marked this conversation as resolved.
Show resolved Hide resolved
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();
Expand All @@ -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,
]
);
Expand All @@ -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 '';
Expand Down
41 changes: 25 additions & 16 deletions modules/imaging_browser/jsx/ImagePanel.js
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,7 @@ DownloadButton.propTypes = {
FileName: PropTypes.string,
BaseURL: PropTypes.string,
Label: PropTypes.string,
URL: PropTypes.string,
};


Expand Down Expand Up @@ -826,22 +827,30 @@ class ImageDownloadButtons extends Component {
BaseURL={this.props.BaseURL}
Label="Download NRRD"
/>
<DownloadButton FileName={this.props.NiiFile}
BaseURL={this.props.BaseURL}
Label="Download NIfTI"
/>
<DownloadButton FileName={this.props.BvalFile}
BaseURL={this.props.BaseURL}
Label="Download BVAL"
/>
<DownloadButton FileName={this.props.BvecFile}
BaseURL={this.props.BaseURL}
Label="Download BVEC"
/>
<DownloadButton FileName={this.props.JsonFile}
BaseURL={this.props.BaseURL}
Label="Download BIDS JSON"
/>
{ this.props.NiiFile ?
<DownloadButton URL={this.props.APIFile + '/format/nifti'}
Label="Download NIfTI"
/> :
null
}
{this.props.BvalFile ?
<DownloadButton URL={this.props.APIFile + '/format/bval'}
Label="Download BVAL"
/> :
null
}
{this.props.BvecFile ?
<DownloadButton URL={this.props.APIFile + '/format/bvec'}
Label="Download BVEC"
/> :
null
}
{this.props.JsonFile ?
<DownloadButton URL={this.props.APIFile + '/format/bidsjson'}
Label="Download BIDS JSON"
/> :
null
}
<LongitudinalViewButton FileID={this.props.FileID}
BaseURL={this.props.BaseURL}
OtherTimepoints={this.props.OtherTimepoints}
Expand Down
1 change: 1 addition & 0 deletions modules/imaging_browser/php/viewsession.class.inc
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,7 @@ class ViewSession extends \NDB_Form
'OtherTimepoints' => $OtherTimepoints,
'CaveatViolationsResolvedID' => $caveatViolationsResolvedID,
];

$this->tpl_data['files'][] = $file;
}
}
Expand Down
66 changes: 61 additions & 5 deletions php/libraries/Image.class.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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
Expand All @@ -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');
xlecours marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand All @@ -100,7 +112,7 @@ class Image
SELECT
Value
FROM
parameter_file pf
parameter_file pf
JOIN parameter_type pt
USING (ParameterTypeID)
JOIN files f
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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
);
}
}