-
Notifications
You must be signed in to change notification settings - Fork 173
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
Changes from 17 commits
a586f1e
4716737
c40102c
4f3ea96
fdb6512
8bf64cf
46aa398
e18586b
998b34f
d0eb91f
03a3d42
8c1a236
3f86d70
1cdce27
614b334
0de005e
c1e92b2
a76d31c
a24a5da
f6e54ff
4fe033a
55c8dfd
309f52b
27c14a5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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,49 @@ 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; | ||
private $_info; | ||
private $_file_type; | ||
private $_content_type; | ||
|
||
/** | ||
* 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) | ||
public function __construct(\LORIS\Image $image, string $format) | ||
{ | ||
$this->_image = $image; | ||
$this->_image = $image; | ||
xlecours marked this conversation as resolved.
Show resolved
Hide resolved
|
||
$this->_format = $format; | ||
|
||
switch ($this->_format) { | ||
case 'thumbnail': | ||
$this->_info = $this->_image->getThumbnailFileInfo(); | ||
xlecours marked this conversation as resolved.
Show resolved
Hide resolved
|
||
$this->_file_type = "Thumbnail"; | ||
$this->_content_type = 'image/jpeg'; | ||
break; | ||
case 'nifti': | ||
$this->_info = $this->_image->getNiiFileInfo(); | ||
xlecours marked this conversation as resolved.
Show resolved
Hide resolved
|
||
$this->_file_type = "NIfTI"; | ||
$this->_content_type = 'application/octet-stream'; | ||
break; | ||
case 'bval': | ||
$this->_info = $this->_image->getBvalFileInfo(); | ||
xlecours marked this conversation as resolved.
Show resolved
Hide resolved
|
||
$this->_file_type = "NIfTI BVAL"; | ||
$this->_content_type = 'application/text'; | ||
break; | ||
case 'bvec': | ||
$this->_info = $this->_image->getBvecFileInfo(); | ||
xlecours marked this conversation as resolved.
Show resolved
Hide resolved
|
||
$this->_file_type = "NIfTI BVEC"; | ||
$this->_content_type = 'application/text'; | ||
break; | ||
case 'bidsjson': | ||
$this->_info = $this->_image->getBidsJsonFileInfo(); | ||
xlecours marked this conversation as resolved.
Show resolved
Hide resolved
|
||
$this->_file_type = "BIDS JSON"; | ||
$this->_content_type = 'application/json'; | ||
break; | ||
} | ||
} | ||
|
||
/** | ||
|
@@ -100,23 +134,22 @@ class Thumbnail extends Endpoint implements \LORIS\Middleware\ETagCalculator | |
*/ | ||
private function _handleGET(): ResponseInterface | ||
{ | ||
$info = $this->_image->getThumbnailFileInfo(); | ||
|
||
if (!$info->isFile()) { | ||
error_log('Thumbnail not found'); | ||
if (!$this->_info->isFile()) { | ||
error_log("$this->_file_type file not found"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be using the PSR logger, not There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @driusan could you elaborate on how to do that? Sorry I am not very versed in the PSR realm apparently... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. NDB_Page includes a $this->logger which implements PSR-3 (https://www.php-fig.org/psr/psr-3/) which honours the config settings from the LORIS config. I think this endpoint bypasses NDB_Page, so you might need to copy the initialization from the NDB_Page constructor and then use ie There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That would involve changing src/Http/Endpoint to
|
||
return new \LORIS\Http\Response\JSON\NotFound(); | ||
} | ||
|
||
if (!$info->isReadable()) { | ||
error_log('Thumbnail exists but is not readable by webserver'); | ||
if (!$this->_info->isReadable()) { | ||
error_log( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same |
||
"$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'); | ||
|
@@ -125,7 +158,7 @@ class Thumbnail extends Endpoint implements \LORIS\Middleware\ETagCalculator | |
$body, | ||
200, | ||
[ | ||
'Content-Type' => 'image/jpeg', | ||
'Content-Type' => $this->_content_type, | ||
'Content-Disposition' => 'attachment; filename=' . $filename, | ||
] | ||
); | ||
|
@@ -140,16 +173,15 @@ class Thumbnail extends Endpoint implements \LORIS\Middleware\ETagCalculator | |
*/ | ||
public function ETag(ServerRequestInterface $request) : string | ||
{ | ||
$info = $this->_image->getThumbnailFileInfo(); | ||
|
||
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)); | ||
|
There was a problem hiding this comment.
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..)
There was a problem hiding this comment.
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?