From 43ef3e4e190b3b2b86f3ea1e5689e06f4ff61337 Mon Sep 17 00:00:00 2001 From: ivan Date: Wed, 2 May 2018 21:40:44 -0300 Subject: [PATCH] [Fix issue #104] - Show image attachements on browser --- server/libs/FileDownloader.php | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/server/libs/FileDownloader.php b/server/libs/FileDownloader.php index 6f49ca64..f8bc7435 100755 --- a/server/libs/FileDownloader.php +++ b/server/libs/FileDownloader.php @@ -19,9 +19,9 @@ class FileDownloader extends FileManager { if(file_exists($fullFilePath) && is_file($fullFilePath)) { header('Cache-control: private'); - header('Content-Type: application/octet-stream'); - header('Content-Length: '.filesize($fullFilePath)); - header('Content-Disposition: filename='. $this->getFileName()); + header('Content-Type: ' . $this->getFileContentType()); + header('Content-Length: ' . filesize($fullFilePath)); + header('Content-Disposition: filename=' . $this->getFileName()); flush(); $file = fopen($fullFilePath, 'r'); @@ -37,4 +37,18 @@ class FileDownloader extends FileManager { public function eraseFile() { unlink($this->getLocalPath() . $this->getFileName()); } -} \ No newline at end of file + + public function getFileContentType() { + $fileExtension = substr($this->getFileName(), -3); + $contentTypes = [ + 'jpg' => 'image/jpeg', + 'gif' => 'image/fig', + 'png' => 'image/png', + 'default' => 'application/octet-stream', + ]; + + return $contentTypes[ + array_key_exists($fileExtension, $contentTypes) ? $fileExtension : 'default' + ]; + } +}