Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 30 additions & 2 deletions core/Controller/CssController.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
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 {
Expand Down Expand Up @@ -62,12 +64,16 @@ public function __construct($appName, IRequest $request, IAppData $appData, ITim
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());
Expand All @@ -76,4 +82,26 @@ public function getCss($fileName, $appName) {
$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);
}
}
32 changes: 30 additions & 2 deletions core/Controller/JsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
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 {
Expand Down Expand Up @@ -63,12 +65,16 @@ public function __construct($appName, IRequest $request, IAppData $appData, ITim
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());
Expand All @@ -77,4 +83,26 @@ public function getJs($fileName, $appName) {
$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);
}
}
7 changes: 7 additions & 0 deletions lib/private/Template/JSCombiner.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,16 @@ protected function cache($path, $fileName, ISimpleFolder $folder) {
$depFile = $folder->newFile($depFileName);
}

try {
$gzipFile = $folder->getFile($fileName . '.gz');
} catch (NotFoundException $e) {
$gzipFile = $folder->newFile($fileName . '.gz');
}

try {
$cachedfile->putContent($res);
$depFile->putContent(json_encode($deps));
$gzipFile->putContent(gzencode($res, 9));
return true;
} catch (NotPermittedException $e) {
return false;
Expand Down
11 changes: 10 additions & 1 deletion lib/private/Template/SCSSCacher.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,18 @@ private function cache($path, $fileNameCSS, $fileNameSCSS, ISimpleFolder $folder
return false;
}

// Gzip file
try {
$cachedfile->putContent($this->rebaseUrls($compiledScss, $webDir));
$gzipFile = $folder->getFile($fileNameCSS . '.gz');
} catch (NotFoundException $e) {
$gzipFile = $folder->newFile($fileNameCSS . '.gz');
}

try {
$data = $this->rebaseUrls($compiledScss, $webDir);
$cachedfile->putContent($data);
$depFile->putContent(json_encode($scss->getParsedFiles()));
$gzipFile->putContent(gzencode($data, 9));
$this->logger->debug($webDir.'/'.$fileNameSCSS.' compiled and successfully cached', ['app' => 'core']);
return true;
} catch(NotPermittedException $e) {
Expand Down
68 changes: 67 additions & 1 deletion tests/Core/Controller/CssControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ class CssControllerTest extends TestCase {
/** @var IAppData|\PHPUnit_Framework_MockObject_MockObject */
private $appData;

/** @var IRequests|\PHPUnit_Framework_MockObject_MockObject */
private $request;

/** @var CssController */
private $controller;

Expand All @@ -52,9 +55,11 @@ public function setUp() {
$timeFactory->method('getTime')
->willReturn(1337);

$this->request = $this->createMock(IRequest::class);

$this->controller = new CssController(
'core',
$this->createMock(IRequest::class),
$this->request,
$this->appData,
$timeFactory
);
Expand Down Expand Up @@ -108,4 +113,65 @@ public function testGetFile() {
$this->assertEquals($expected, $result);
}

public function testGetGzipFile() {
$folder = $this->createMock(ISimpleFolder::class);
$gzipFile = $this->createMock(ISimpleFile::class);
$this->appData->method('getFolder')
->with('myapp')
->willReturn($folder);

$folder->method('getFile')
->with('file.css.gz')
->willReturn($gzipFile);

$this->request->method('getHeader')
->with('Accept-Encoding')
->willReturn('gzip, deflate');

$expected = new FileDisplayResponse($gzipFile, Http::STATUS_OK, ['Content-Type' => 'text/css']);
$expected->addHeader('Content-Encoding', 'gzip');
$expected->cacheFor(86400);
$expires = new \DateTime();
$expires->setTimestamp(1337);
$expires->add(new \DateInterval('PT24H'));
$expected->addHeader('Expires', $expires->format(\DateTime::RFC1123));
$expected->addHeader('Pragma', 'cache');

$result = $this->controller->getCss('file.css', 'myapp');
$this->assertEquals($expected, $result);
}

public function testGetGzipFileNotFound() {
$folder = $this->createMock(ISimpleFolder::class);
$file = $this->createMock(ISimpleFile::class);
$this->appData->method('getFolder')
->with('myapp')
->willReturn($folder);

$folder->method('getFile')
->will($this->returnCallback(
function($fileName) use ($file) {
if ($fileName === 'file.css') {
return $file;
}
throw new NotFoundException();
})
);

$this->request->method('getHeader')
->with('Accept-Encoding')
->willReturn('gzip, deflate');

$expected = new FileDisplayResponse($file, Http::STATUS_OK, ['Content-Type' => 'text/css']);
$expected->cacheFor(86400);
$expires = new \DateTime();
$expires->setTimestamp(1337);
$expires->add(new \DateInterval('PT24H'));
$expected->addHeader('Expires', $expires->format(\DateTime::RFC1123));
$expected->addHeader('Pragma', 'cache');

$result = $this->controller->getCss('file.css', 'myapp');
$this->assertEquals($expected, $result);
}

}
68 changes: 67 additions & 1 deletion tests/Core/Controller/JsControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ class JsControllerTest extends TestCase {
/** @var JsController */
private $controller;

/** @var IRequest|\PHPUnit_Framework_MockObject_MockObject */
private $request;

public function setUp() {
parent::setUp();

Expand All @@ -51,9 +54,11 @@ public function setUp() {
$timeFactory->method('getTime')
->willReturn(1337);

$this->request = $this->createMock(IRequest::class);

$this->controller = new JsController(
'core',
$this->createMock(IRequest::class),
$this->request,
$this->appData,
$timeFactory
);
Expand Down Expand Up @@ -107,4 +112,65 @@ public function testGetFile() {
$this->assertEquals($expected, $result);
}

public function testGetGzipFile() {
$folder = $this->createMock(ISimpleFolder::class);
$gzipFile = $this->createMock(ISimpleFile::class);
$this->appData->method('getFolder')
->with('myapp')
->willReturn($folder);

$folder->method('getFile')
->with('file.js.gz')
->willReturn($gzipFile);

$this->request->method('getHeader')
->with('Accept-Encoding')
->willReturn('gzip, deflate');

$expected = new FileDisplayResponse($gzipFile, Http::STATUS_OK, ['Content-Type' => 'application/javascript']);
$expected->addHeader('Content-Encoding', 'gzip');
$expected->cacheFor(86400);
$expires = new \DateTime();
$expires->setTimestamp(1337);
$expires->add(new \DateInterval('PT24H'));
$expected->addHeader('Expires', $expires->format(\DateTime::RFC1123));
$expected->addHeader('Pragma', 'cache');

$result = $this->controller->getJs('file.js', 'myapp');
$this->assertEquals($expected, $result);
}

public function testGetGzipFileNotFound() {
$folder = $this->createMock(ISimpleFolder::class);
$file = $this->createMock(ISimpleFile::class);
$this->appData->method('getFolder')
->with('myapp')
->willReturn($folder);

$folder->method('getFile')
->will($this->returnCallback(
function($fileName) use ($file) {
if ($fileName === 'file.js') {
return $file;
}
throw new NotFoundException();
})
);

$this->request->method('getHeader')
->with('Accept-Encoding')
->willReturn('gzip, deflate');

$expected = new FileDisplayResponse($file, Http::STATUS_OK, ['Content-Type' => 'application/javascript']);
$expected->cacheFor(86400);
$expires = new \DateTime();
$expires->setTimestamp(1337);
$expires->add(new \DateInterval('PT24H'));
$expected->addHeader('Expires', $expires->format(\DateTime::RFC1123));
$expected->addHeader('Pragma', 'cache');

$result = $this->controller->getJs('file.js', 'myapp');
$this->assertEquals($expected, $result);
}

}
Loading