Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -170,12 +170,10 @@ class Recording extends Endpoint implements \LORIS\Middleware\ETagCalculator
return new \LORIS\Http\Response\JSON\NotFound($e->getMessage());
}

$mimetype = substr($recording->getMetadata('header'), 0, 4) === 'hdf5' ?
'application/x.minc2' : 'application/octet-stream';

$info = $recording->getFileInfo();
$filepath = $info->getRealPath();

if (!$info->isFile()) {
if (!$info->isFile() && !$info->isDir()) {
error_log('file in database but not in file system');
return new \LORIS\Http\Response\JSON\NotFound();
}
Expand All @@ -185,13 +183,25 @@ class Recording extends Endpoint implements \LORIS\Middleware\ETagCalculator
return new \LORIS\Http\Response\JSON\NotFound();
}

$body = new \LORIS\Http\FileStream($info->getRealPath(), 'r');
// Tar the acquisition file if it is actually a directory (such as for MEG
// CTF acquisitions).
if ($info->isDir()) {
$filename = $this->_filename . '.tar';
$mimetype = 'application/x-tar';
$body = new \LORIS\Http\ArchiveStream($filepath);
} else {
$filename = $this->_filename;
$mimetype = substr($recording->getMetadata('header'), 0, 4) === 'hdf5'
? 'application/x.minc2'
: 'application/octet-stream';
$body = new \LORIS\Http\FileStream($filepath, 'r');
}

return (new \LORIS\Http\Response())
->withHeader('Content-Type', $mimetype)
->withHeader(
'Content-Disposition',
'attachment; filename=' . $this->_filename
'attachment; filename=' . $filename
)
->withBody($body);
}
Expand Down
37 changes: 29 additions & 8 deletions modules/electrophysiology_browser/jsx/components/DownloadPanel.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class DownloadPanel extends Component {
* @return {JSX} - React markup for the component
*/
render() {
const {t} = this.props;
const {t, dccid, visit, physioFileName} = this.props;
return (
<Panel
id={this.props.id}
Expand All @@ -59,6 +59,24 @@ class DownloadPanel extends Component {
{Object.entries(panel.links).map(([type, download], j) => {
const disabled = (download.file === '');

let recordingFileUrl = `/api/v0.0.3/candidates/${dccid}/`
+ `${visit}/recordings/${physioFileName}`;

switch (type) {
case 'physiological_event_files':
recordingFileUrl += '/bidsfiles/events';
break;
case 'all_files':
recordingFileUrl += '/bidsfiles/archive';
break;
case 'physiological_channel_file':
recordingFileUrl += '/bidsfiles/channels';
break;
case 'physiological_electrode_file':
recordingFileUrl += '/bidsfiles/electrodes';
break;
}

// Ignore physiological_coord_system_file
return type !== 'physiological_coord_system_file'
? (
Expand Down Expand Up @@ -93,13 +111,13 @@ class DownloadPanel extends Component {
: <a
className='btn btn-primary download col-xs-6'
href={
(type ==
'physiological_event_files' ||
type == 'all_files') ?
this.state.annotationsAction
+ '?physioFileID=' + this.state.physioFileID
+ '&filePath=' + download.file
: '/mri/jiv/get_file.php?file=' + download.file
(type == 'physiological_event_files')
? (
this.state.annotationsAction
+ '?physioFileID=' + this.state.physioFileID
+ '&filePath=' + download.file
)
: recordingFileUrl
}
target='_blank'
style={{
Expand Down Expand Up @@ -141,7 +159,10 @@ class DownloadPanel extends Component {

DownloadPanel.propTypes = {
downloads: PropTypes.array,
dccid: PropTypes.string,
visit: PropTypes.string,
physioFileID: PropTypes.number,
physioFileName: PropTypes.string,
outputType: PropTypes.string,
id: PropTypes.string,
t: PropTypes.func,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,10 @@ class ElectrophysiologySessionView extends Component {
<DownloadPanel
id={'file_download_' + i}
downloads={this.state.database[i].file.downloads}
dccid={this.state.patient.info.dccid}
visit={this.state.patient.info.visit_label}
physioFileID={this.state.database[i].file.id}
physioFileName={this.state.database[i].file.name}
outputType={this.state.database[i].file.output_type}
t={t}
/>
Expand Down
66 changes: 66 additions & 0 deletions src/Http/ArchiveStream.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php declare(strict_types=1);

/**
* This file implements an ArchiveStream, which creates a temporary tar archive
* from a directory and provides it as a PSR7 StreamInterface.
*
* @category PSR7
* @package Http
* @license http://www.gnu.org/licenses/gpl-3.0.txt GPLv3
* @link https://www.github.com/aces/Loris/
*
* @see https://www.php-fig.org/psr/psr-7/
*/
namespace LORIS\Http;

/**
* An ArchiveStream creates a temporary tar archive from a directory
* and provides it as a PSR7 StreamInterface. The temporary archive file
* is deleted when the stream is closed.
*
* @category PSR7
* @package Http
* @license http://www.gnu.org/licenses/gpl-3.0.txt GPLv3
* @link https://www.github.com/aces/Loris/
*/
class ArchiveStream extends FileStream
{
/**
* The path to the temporary archive file
*
* @var string
*/
private string $archivePath;

/**
* Constructor
*
* Creates a tar archive from the given directory and opens it as a stream.
* The archive file is deleted when the stream is closed.
*
* @param string $directoryPath The path to the directory to archive
*
* @throws \Exception if archive creation fails
*/
public function __construct(string $directoryPath)
{
$this->archivePath = sys_get_temp_dir() . '/' . uniqid() . '.tar';

$phar = new \PharData($this->archivePath);
$phar->buildFromDirectory($directoryPath);

parent::__construct($this->archivePath, 'r');
}

/**
* {@inheritdoc}
*/
public function close(): void
{
parent::close();

if (file_exists($this->archivePath)) {
unlink($this->archivePath);
}
}
}
Loading