show filesize and remaining time on download (#181)

* show filesize on download
This commit is contained in:
ahaenggli
2021-02-02 16:01:52 +01:00
committed by GitHub
parent 28aa39ccdb
commit 91cfd6a845
4 changed files with 31 additions and 4 deletions

View File

@@ -91,7 +91,12 @@ class DownloadController
'Content-Transfer-Encoding',
'binary'
);
if (isset($file['filesize'])) {
$streamedResponse->headers->set(
'Content-Length',
$file['filesize']
);
}
// @codeCoverageIgnoreStart
if (APP_ENV == 'development') {
$streamedResponse->headers->set(
@@ -137,11 +142,11 @@ class DownloadController
public function batchDownloadStart(Request $request, StreamedResponse $streamedResponse, TmpfsInterface $tmpfs)
{
$uniqid = (string) preg_replace('/[^0-9a-zA-Z_]/', '', (string) $request->input('uniqid'));
$file = $tmpfs->readStream($uniqid);
$streamedResponse->setCallback(function () use ($tmpfs, $uniqid) {
$streamedResponse->setCallback(function () use ($file, $tmpfs, $uniqid) {
// @codeCoverageIgnoreStart
set_time_limit(0);
$file = $tmpfs->readStream($uniqid);
if ($file['stream']) {
while (! feof($file['stream'])) {
echo fread($file['stream'], 1024 * 8);
@@ -170,7 +175,12 @@ class DownloadController
'Content-Transfer-Encoding',
'binary'
);
if (isset($file['filesize'])) {
$streamedResponse->headers->set(
'Content-Length',
$file['filesize']
);
}
// close session so we can continue streaming, note: dev is single-threaded
$this->session->save();

View File

@@ -134,6 +134,7 @@ class Filesystem implements Service
return [
'filename' => $this->getBaseName($path),
'stream' => $this->storage->readStream($path),
'filesize' => $this->storage->getSize($path),
];
}

View File

@@ -62,10 +62,12 @@ class Tmpfs implements Service, TmpfsInterface
$filename = $this->sanitizeFilename($filename);
$stream = fopen($this->getPath().$filename, 'r');
$filesize = filesize($this->getPath().$filename);
return [
'filename' => $filename,
'stream' => $stream,
'filesize' => $filesize,
];
}