Skip to content

Commit 933c15c

Browse files
committed
use new class instead of existing one
1 parent 25e8a85 commit 933c15c

3 files changed

Lines changed: 70 additions & 57 deletions

File tree

modules/api/php/endpoints/candidate/visit/electrophysiology/recording.class.inc

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,8 @@ class Recording extends Endpoint implements \LORIS\Middleware\ETagCalculator
170170
return new \LORIS\Http\Response\JSON\NotFound($e->getMessage());
171171
}
172172

173-
$info = $recording->getFileInfo();
173+
$info = $recording->getFileInfo();
174+
$filepath = $info->getRealPath();
174175

175176
if (!$info->isFile() && !$info->isDir()) {
176177
error_log('file in database but not in file system');
@@ -187,35 +188,15 @@ class Recording extends Endpoint implements \LORIS\Middleware\ETagCalculator
187188
if ($info->isDir()) {
188189
$filename = $this->_filename . '.tar';
189190
$mimetype = 'application/x-tar';
190-
191-
$tarfile = sys_get_temp_dir() . '/recording_' . uniqid() . '.tar';
192-
193-
try {
194-
$phar = new \PharData($tarfile);
195-
$phar->buildFromDirectory($info->getRealPath());
196-
} catch (\Exception $e) {
197-
$this->logger->error(
198-
'Failed to create tar archive: ' . $e->getMessage()
199-
);
200-
201-
if (file_exists($tarfile)) {
202-
unlink($tarfile);
203-
}
204-
205-
return new \LORIS\Http\Response\JSON\InternalServerError();
206-
}
207-
208-
$filepath = $tarfile;
191+
$body = new \LORIS\Http\ArchiveStream($filepath);
209192
} else {
210193
$filename = $this->_filename;
211-
$filepath = $info->getRealPath();
212194
$mimetype = substr($recording->getMetadata('header'), 0, 4) === 'hdf5'
213195
? 'application/x.minc2'
214196
: 'application/octet-stream';
197+
$body = new \LORIS\Http\FileStream($filepath, 'r');
215198
}
216199

217-
$body = new \LORIS\Http\FileStream($filepath, 'r', true);
218-
219200
return (new \LORIS\Http\Response())
220201
->withHeader('Content-Type', $mimetype)
221202
->withHeader(

src/Http/ArchiveStream.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php declare(strict_types=1);
2+
3+
/**
4+
* This file implements an ArchiveStream, which creates a temporary tar archive
5+
* from a directory and provides it as a PSR7 StreamInterface.
6+
*
7+
* @category PSR7
8+
* @package Http
9+
* @license http://www.gnu.org/licenses/gpl-3.0.txt GPLv3
10+
* @link https://www.github.com/aces/Loris/
11+
*
12+
* @see https://www.php-fig.org/psr/psr-7/
13+
*/
14+
namespace LORIS\Http;
15+
16+
/**
17+
* An ArchiveStream creates a temporary tar archive from a directory
18+
* and provides it as a PSR7 StreamInterface. The temporary archive file
19+
* is deleted when the stream is closed.
20+
*
21+
* @category PSR7
22+
* @package Http
23+
* @license http://www.gnu.org/licenses/gpl-3.0.txt GPLv3
24+
* @link https://www.github.com/aces/Loris/
25+
*/
26+
class ArchiveStream extends \Laminas\Diactoros\Stream implements \Psr\Http\Message\StreamInterface
27+
{
28+
/**
29+
* The path to the temporary archive file
30+
*
31+
* @var string
32+
*/
33+
private string $archivePath;
34+
35+
/**
36+
* Constructor
37+
*
38+
* Creates a tar archive from the given directory and opens it as a stream.
39+
* The archive file is deleted when the stream is closed.
40+
*
41+
* @param string $directoryPath The path to the directory to archive
42+
*
43+
* @throws \Exception if archive creation fails
44+
*/
45+
public function __construct(string $directoryPath)
46+
{
47+
$this->archivePath = sys_get_temp_dir() . '/' . uniqid() . '.tar';
48+
49+
$phar = new \PharData($this->archivePath);
50+
$phar->buildFromDirectory($directoryPath);
51+
52+
parent::__construct($this->archivePath, 'r');
53+
}
54+
55+
/**
56+
* {@inheritdoc}
57+
*/
58+
public function close(): void
59+
{
60+
parent::close();
61+
62+
if (file_exists($this->archivePath)) {
63+
unlink($this->archivePath);
64+
}
65+
}
66+
}

src/Http/FileStream.php

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -27,38 +27,4 @@
2727
*/
2828
class FileStream extends \Laminas\Diactoros\Stream implements \Psr\Http\Message\StreamInterface
2929
{
30-
/**
31-
* @var bool Whether the file be deleted when the stream is closed, used for
32-
* temporary files.
33-
*/
34-
private bool $deleteOnClose;
35-
36-
/**
37-
* Constructor
38-
*
39-
* @param string $stream The path to the file or a stream resource
40-
* @param string $mode The mode to open the stream with
41-
* @param bool $deleteOnClose If true, delete the file when the stream is closed
42-
*/
43-
public function __construct(
44-
string $stream,
45-
string $mode = 'r',
46-
bool $deleteOnClose = false,
47-
) {
48-
parent::__construct($stream, $mode);
49-
$this->deleteOnClose = $deleteOnClose;
50-
}
51-
52-
/**
53-
* {@inheritdoc}
54-
*/
55-
public function close(): void
56-
{
57-
if ($this->deleteOnClose
58-
&& is_string($this->resource)
59-
&& file_exists($this->resource)
60-
) {
61-
unlink($this->resource);
62-
}
63-
}
6430
}

0 commit comments

Comments
 (0)