diff options
author | Roeland Jago Douma <roeland@famdouma.nl> | 2017-03-26 16:30:08 +0200 |
---|---|---|
committer | Roeland Jago Douma <roeland@famdouma.nl> | 2017-03-28 21:46:23 +0200 |
commit | 54f9b35f71cec60880b46f5684c3a2115a0e9cda (patch) | |
tree | eb5863f32396fa6bd6bacfcbc3ae54708e0345b2 /core/Controller | |
parent | e26f138fc596492da88b8a0d57749f5703e1d100 (diff) | |
download | nextcloud-server-54f9b35f71cec60880b46f5684c3a2115a0e9cda.tar.gz nextcloud-server-54f9b35f71cec60880b46f5684c3a2115a0e9cda.zip |
Allow to gzip CSS/JS files
Since in production the SCSS files are compiled once and the javascript
files are combined once we can just as well gzip them aggresively.
This means that once they are requested and the browser supports gzip we
can just serve the gzipped file saving precious bandwidth.
Signed-off-by: Roeland Jago Douma <roeland@famdouma.nl>
Diffstat (limited to 'core/Controller')
-rw-r--r-- | core/Controller/CssController.php | 32 | ||||
-rw-r--r-- | core/Controller/JsController.php | 32 |
2 files changed, 60 insertions, 4 deletions
diff --git a/core/Controller/CssController.php b/core/Controller/CssController.php index 1206c95a5b8..b467d386f98 100644 --- a/core/Controller/CssController.php +++ b/core/Controller/CssController.php @@ -28,6 +28,8 @@ use OCP\AppFramework\Http\FileDisplayResponse; use OCP\AppFramework\Utility\ITimeFactory; use OCP\Files\IAppData; use OCP\Files\NotFoundException; +use OCP\Files\SimpleFS\ISimpleFile; +use OCP\Files\SimpleFS\ISimpleFolder; use OCP\IRequest; class CssController extends Controller { @@ -62,12 +64,16 @@ class CssController extends Controller { public function getCss($fileName, $appName) { try { $folder = $this->appData->getFolder($appName); - $cssFile = $folder->getFile($fileName); + $gzip = false; + $file = $this->getFile($folder, $fileName, $gzip); } catch(NotFoundException $e) { return new NotFoundResponse(); } - $response = new FileDisplayResponse($cssFile, Http::STATUS_OK, ['Content-Type' => 'text/css']); + $response = new FileDisplayResponse($file, Http::STATUS_OK, ['Content-Type' => 'text/css']); + if ($gzip) { + $response->addHeader('Content-Encoding', 'gzip'); + } $response->cacheFor(86400); $expires = new \DateTime(); $expires->setTimestamp($this->timeFactory->getTime()); @@ -76,4 +82,26 @@ class CssController extends Controller { $response->addHeader('Pragma', 'cache'); return $response; } + + /** + * @param ISimpleFolder $folder + * @param string $fileName + * @param bool $gzip is set to true if we use the gzip file + * @return ISimpleFile + */ + private function getFile(ISimpleFolder $folder, $fileName, &$gzip) { + $encoding = $this->request->getHeader('Accept-Encoding'); + + if ($encoding !== null && strpos($encoding, 'gzip') !== false) { + try { + $gzip = true; + return $folder->getFile($fileName . '.gz'); + } catch (NotFoundException $e) { + // continue + } + } + + $gzip = false; + return $folder->getFile($fileName); + } } diff --git a/core/Controller/JsController.php b/core/Controller/JsController.php index 0770974e7a1..0b50abc158a 100644 --- a/core/Controller/JsController.php +++ b/core/Controller/JsController.php @@ -29,6 +29,8 @@ use OCP\AppFramework\Http\FileDisplayResponse; use OCP\AppFramework\Utility\ITimeFactory; use OCP\Files\IAppData; use OCP\Files\NotFoundException; +use OCP\Files\SimpleFS\ISimpleFile; +use OCP\Files\SimpleFS\ISimpleFolder; use OCP\IRequest; class JsController extends Controller { @@ -63,12 +65,16 @@ class JsController extends Controller { public function getJs($fileName, $appName) { try { $folder = $this->appData->getFolder($appName); - $jsFile = $folder->getFile($fileName); + $gzip = false; + $file = $this->getFile($folder, $fileName, $gzip); } catch(NotFoundException $e) { return new NotFoundResponse(); } - $response = new FileDisplayResponse($jsFile, Http::STATUS_OK, ['Content-Type' => 'application/javascript']); + $response = new FileDisplayResponse($file, Http::STATUS_OK, ['Content-Type' => 'application/javascript']); + if ($gzip) { + $response->addHeader('Content-Encoding', 'gzip'); + } $response->cacheFor(86400); $expires = new \DateTime(); $expires->setTimestamp($this->timeFactory->getTime()); @@ -77,4 +83,26 @@ class JsController extends Controller { $response->addHeader('Pragma', 'cache'); return $response; } + + /** + * @param ISimpleFolder $folder + * @param string $fileName + * @param bool $gzip is set to true if we use the gzip file + * @return ISimpleFile + */ + private function getFile(ISimpleFolder $folder, $fileName, &$gzip) { + $encoding = $this->request->getHeader('Accept-Encoding'); + + if ($encoding !== null && strpos($encoding, 'gzip') !== false) { + try { + $gzip = true; + return $folder->getFile($fileName . '.gz'); + } catch (NotFoundException $e) { + // continue + } + } + + $gzip = false; + return $folder->getFile($fileName); + } } |