From 91cfd6a8451eef9e825f3bab7db72cb030f48ab9 Mon Sep 17 00:00:00 2001 From: ahaenggli Date: Tue, 2 Feb 2021 16:01:52 +0100 Subject: [PATCH] show filesize and remaining time on download (#181) * show filesize on download --- backend/Controllers/DownloadController.php | 18 ++++++++++++++---- backend/Services/Storage/Filesystem.php | 1 + backend/Services/Tmpfs/Adapters/Tmpfs.php | 2 ++ tests/backend/Feature/FilesTest.php | 14 ++++++++++++++ 4 files changed, 31 insertions(+), 4 deletions(-) diff --git a/backend/Controllers/DownloadController.php b/backend/Controllers/DownloadController.php index 14306c4..b5df448 100644 --- a/backend/Controllers/DownloadController.php +++ b/backend/Controllers/DownloadController.php @@ -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(); diff --git a/backend/Services/Storage/Filesystem.php b/backend/Services/Storage/Filesystem.php index d27b841..36c4823 100644 --- a/backend/Services/Storage/Filesystem.php +++ b/backend/Services/Storage/Filesystem.php @@ -134,6 +134,7 @@ class Filesystem implements Service return [ 'filename' => $this->getBaseName($path), 'stream' => $this->storage->readStream($path), + 'filesize' => $this->storage->getSize($path), ]; } diff --git a/backend/Services/Tmpfs/Adapters/Tmpfs.php b/backend/Services/Tmpfs/Adapters/Tmpfs.php index f5c9a3a..0a2b60c 100644 --- a/backend/Services/Tmpfs/Adapters/Tmpfs.php +++ b/backend/Services/Tmpfs/Adapters/Tmpfs.php @@ -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, ]; } diff --git a/tests/backend/Feature/FilesTest.php b/tests/backend/Feature/FilesTest.php index 2461113..2311b8f 100644 --- a/tests/backend/Feature/FilesTest.php +++ b/tests/backend/Feature/FilesTest.php @@ -155,6 +155,7 @@ class FilesTest extends TestCase mkdir(TEST_REPOSITORY.'/john'); touch(TEST_REPOSITORY.'/john/john.txt', $this->timestamp); + file_put_contents(TEST_REPOSITORY.'/john/john.txt', '123456'); touch(TEST_REPOSITORY.'/john/image.jpg', $this->timestamp); touch(TEST_REPOSITORY.'/john/vector.svg', $this->timestamp); touch(TEST_REPOSITORY.'/john/inlinedoc.pdf', $this->timestamp); @@ -165,6 +166,7 @@ class FilesTest extends TestCase $this->assertEquals($headers->get('content-disposition'), "attachment; filename=file; filename*=utf-8''john.txt"); $this->assertEquals($headers->get('content-type'), 'text/plain'); $this->assertEquals($headers->get('content-transfer-encoding'), 'binary'); + $this->assertEquals($headers->get('content-length'), 6); $this->assertOk(); $path_encoded = base64_encode('image.jpg'); @@ -173,6 +175,7 @@ class FilesTest extends TestCase $this->assertEquals($headers->get('content-disposition'), "attachment; filename=file; filename*=utf-8''image.jpg"); $this->assertEquals($headers->get('content-type'), 'image/jpeg'); $this->assertEquals($headers->get('content-transfer-encoding'), 'binary'); + $this->assertEquals($headers->get('content-length'), 0); $this->assertOk(); $path_encoded = base64_encode('vector.svg'); @@ -181,6 +184,7 @@ class FilesTest extends TestCase $this->assertEquals($headers->get('content-disposition'), "attachment; filename=file; filename*=utf-8''vector.svg"); $this->assertEquals($headers->get('content-type'), 'image/svg+xml'); $this->assertEquals($headers->get('content-transfer-encoding'), 'binary'); + $this->assertEquals($headers->get('content-length'), 0); $this->assertOk(); $path_encoded = base64_encode('inlinedoc.pdf'); @@ -189,6 +193,7 @@ class FilesTest extends TestCase $this->assertEquals($headers->get('content-disposition'), "inline; filename=file; filename*=utf-8''inlinedoc.pdf"); $this->assertEquals($headers->get('content-type'), 'application/pdf'); $this->assertEquals($headers->get('content-transfer-encoding'), 'binary'); + $this->assertEquals($headers->get('content-length'), 0); $this->assertOk(); } @@ -207,6 +212,7 @@ class FilesTest extends TestCase $this->assertEquals($headers->get('content-disposition'), "inline; filename=file; filename*=utf-8''john.pdf"); $this->assertEquals($headers->get('content-type'), 'application/pdf'); $this->assertEquals($headers->get('content-transfer-encoding'), 'binary'); + $this->assertEquals($headers->get('content-length'), 0); $this->assertOk(); } @@ -634,6 +640,14 @@ class FilesTest extends TestCase ]); $this->assertOk(); + + // test headers + $this->response->getContent(); + $headers = $this->streamedResponse->headers; + $this->assertEquals($headers->get('content-type'), 'application/octet-stream'); + $this->assertEquals($headers->get('content-disposition'), 'attachment; filename=archive.zip'); + $this->assertEquals($headers->get('content-transfer-encoding'), 'binary'); + $this->assertEquals($headers->get('content-length'), 414); } public function testUpdateFileContent()