diff --git a/backend/Controllers/DownloadController.php b/backend/Controllers/DownloadController.php index e312a31..9acdd2c 100644 --- a/backend/Controllers/DownloadController.php +++ b/backend/Controllers/DownloadController.php @@ -20,6 +20,7 @@ use Filegator\Services\Session\SessionStorageInterface as Session; use Filegator\Services\Storage\Filesystem; use Filegator\Services\Tmpfs\TmpfsInterface; use Symfony\Component\HttpFoundation\HeaderUtils; +use Symfony\Component\Mime\MimeTypes; class DownloadController { @@ -65,14 +66,17 @@ class DownloadController // @codeCoverageIgnoreEnd }); - $contentDisposition = HeaderUtils::makeDisposition(HeaderUtils::DISPOSITION_ATTACHMENT, $file['filename'], 'file'); - $contentType = 'application/octet-stream'; + $mimes = (new MimeTypes())->getMimeTypes(pathinfo($file['filename'], PATHINFO_EXTENSION)); + $contentType = !empty($mimes) ? $mimes[0] : 'application/octet-stream'; - if (pathinfo($file['filename'], PATHINFO_EXTENSION) == 'pdf') { - $contentDisposition = HeaderUtils::makeDisposition(HeaderUtils::DISPOSITION_INLINE, $file['filename'], 'file'); - $contentType = 'application/pdf'; + $disposition = HeaderUtils::DISPOSITION_ATTACHMENT; + + if ($contentType == 'application/pdf') { + $disposition = HeaderUtils::DISPOSITION_INLINE; } + $contentDisposition = HeaderUtils::makeDisposition($disposition, $file['filename'], 'file'); + $streamedResponse->headers->set( 'Content-Disposition', $contentDisposition diff --git a/frontend/mixins/shared.js b/frontend/mixins/shared.js index 293523c..df92943 100644 --- a/frontend/mixins/shared.js +++ b/frontend/mixins/shared.js @@ -135,7 +135,7 @@ const funcs = { return this.hasExtension(name, store.state.config.editable ? store.state.config.editable : ['.txt']) }, isImage(name) { - return this.hasExtension(name, ['.jpg', '.jpeg', '.gif', '.png', '.bmp', '.tiff', '.tif']) + return this.hasExtension(name, ['.jpg', '.jpeg', '.gif', '.png', '.bmp', '.svg', '.tiff', '.tif']) }, hasExtension(name, exts) { return (new RegExp('(' + exts.join('|').replace(/\./g, '\\.') + ')$', 'i')).test(name) diff --git a/tests/backend/Feature/FilesTest.php b/tests/backend/Feature/FilesTest.php index b17e514..2461113 100644 --- a/tests/backend/Feature/FilesTest.php +++ b/tests/backend/Feature/FilesTest.php @@ -155,15 +155,40 @@ class FilesTest extends TestCase mkdir(TEST_REPOSITORY.'/john'); touch(TEST_REPOSITORY.'/john/john.txt', $this->timestamp); + touch(TEST_REPOSITORY.'/john/image.jpg', $this->timestamp); + touch(TEST_REPOSITORY.'/john/vector.svg', $this->timestamp); + touch(TEST_REPOSITORY.'/john/inlinedoc.pdf', $this->timestamp); $path_encoded = base64_encode('john.txt'); $this->sendRequest('GET', '/download&path='.$path_encoded); - $headers = $this->streamedResponse->headers; $this->assertEquals($headers->get('content-disposition'), "attachment; filename=file; filename*=utf-8''john.txt"); - $this->assertEquals($headers->get('content-type'), 'application/octet-stream'); + $this->assertEquals($headers->get('content-type'), 'text/plain'); $this->assertEquals($headers->get('content-transfer-encoding'), 'binary'); + $this->assertOk(); + $path_encoded = base64_encode('image.jpg'); + $this->sendRequest('GET', '/download&path='.$path_encoded); + $headers = $this->streamedResponse->headers; + $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->assertOk(); + + $path_encoded = base64_encode('vector.svg'); + $this->sendRequest('GET', '/download&path='.$path_encoded); + $headers = $this->streamedResponse->headers; + $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->assertOk(); + + $path_encoded = base64_encode('inlinedoc.pdf'); + $this->sendRequest('GET', '/download&path='.$path_encoded); + $headers = $this->streamedResponse->headers; + $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->assertOk(); }