Merge pull request #1126 from nextcloud/stable10-theming-performance

[stable10] theming performance
This commit is contained in:
Joas Schilling 2016-08-29 15:44:19 +02:00 committed by GitHub
commit 167cea0b09
3 changed files with 107 additions and 10 deletions

View File

@ -116,8 +116,8 @@ $(document).ready(function () {
submit: function(e, response) { submit: function(e, response) {
OC.msg.startSaving('#theming_settings_msg'); OC.msg.startSaving('#theming_settings_msg');
}, },
fail: function (e, data){ fail: function (e, response){
OC.msg.finishedSaving('#theming_settings_msg', response); OC.msg.finishedError('#theming_settings_msg', response._response.jqXHR.responseJSON.data.message);
} }
}; };
var uploadParamsLogin = { var uploadParamsLogin = {
@ -130,8 +130,8 @@ $(document).ready(function () {
submit: function(e, response) { submit: function(e, response) {
OC.msg.startSaving('#theming_settings_msg'); OC.msg.startSaving('#theming_settings_msg');
}, },
fail: function (e, data){ fail: function (e, response){
OC.msg.finishedSaving('#theming_settings_msg', response); OC.msg.finishedError('#theming_settings_msg', response._response.jqXHR.responseJSON.data.message);
} }
}; };

View File

@ -39,6 +39,7 @@ use OCP\IConfig;
use OCP\IL10N; use OCP\IL10N;
use OCP\IRequest; use OCP\IRequest;
use OCA\Theming\Util; use OCA\Theming\Util;
use OCP\ITempManager;
/** /**
* Class ThemingController * Class ThemingController
@ -60,6 +61,8 @@ class ThemingController extends Controller {
private $config; private $config;
/** @var IRootFolder */ /** @var IRootFolder */
private $rootFolder; private $rootFolder;
/** @var ITempManager */
private $tempManager;
/** /**
* ThemingController constructor. * ThemingController constructor.
@ -72,6 +75,7 @@ class ThemingController extends Controller {
* @param ITimeFactory $timeFactory * @param ITimeFactory $timeFactory
* @param IL10N $l * @param IL10N $l
* @param IRootFolder $rootFolder * @param IRootFolder $rootFolder
* @param ITempManager $tempManager
*/ */
public function __construct( public function __construct(
$appName, $appName,
@ -81,7 +85,8 @@ class ThemingController extends Controller {
Util $util, Util $util,
ITimeFactory $timeFactory, ITimeFactory $timeFactory,
IL10N $l, IL10N $l,
IRootFolder $rootFolder IRootFolder $rootFolder,
ITempManager $tempManager
) { ) {
parent::__construct($appName, $request); parent::__construct($appName, $request);
@ -91,6 +96,7 @@ class ThemingController extends Controller {
$this->l = $l; $this->l = $l;
$this->config = $config; $this->config = $config;
$this->rootFolder = $rootFolder; $this->rootFolder = $rootFolder;
$this->tempManager = $tempManager;
} }
/** /**
@ -171,7 +177,8 @@ class ThemingController extends Controller {
'message' => $this->l->t('No file uploaded') 'message' => $this->l->t('No file uploaded')
] ]
], ],
Http::STATUS_UNPROCESSABLE_ENTITY); Http::STATUS_UNPROCESSABLE_ENTITY
);
} }
$name = ''; $name = '';
if(!empty($newLogo)) { if(!empty($newLogo)) {
@ -182,7 +189,32 @@ class ThemingController extends Controller {
} }
if(!empty($newBackgroundLogo)) { if(!empty($newBackgroundLogo)) {
$target = $this->rootFolder->newFile('themedbackgroundlogo'); $target = $this->rootFolder->newFile('themedbackgroundlogo');
stream_copy_to_stream(fopen($newBackgroundLogo['tmp_name'], 'r'), $target->fopen('w'));
$image = @imagecreatefromstring(file_get_contents($newBackgroundLogo['tmp_name'], 'r'));
if($image === false) {
return new DataResponse(
[
'data' => [
'message' => $this->l->t('Unsupported image type'),
],
'status' => 'failure',
],
Http::STATUS_UNPROCESSABLE_ENTITY
);
}
// Optimize the image since some people may upload images that will be
// either to big or are not progressive rendering.
$tmpFile = $this->tempManager->getTemporaryFile();
if(function_exists('imagescale')) {
// FIXME: Once PHP 5.5.0 is a requirement the above check can be removed
$image = imagescale($image, 1920);
}
imageinterlace($image, 1);
imagejpeg($image, $tmpFile, 75);
imagedestroy($image);
stream_copy_to_stream(fopen($tmpFile, 'r'), $target->fopen('w'));
$this->template->set('backgroundMime', $newBackgroundLogo['type']); $this->template->set('backgroundMime', $newBackgroundLogo['type']);
$name = $newBackgroundLogo['name']; $name = $newBackgroundLogo['name'];
} }
@ -236,6 +268,7 @@ class ThemingController extends Controller {
$response->addHeader('Expires', date(\DateTime::RFC2822, $this->timeFactory->getTime())); $response->addHeader('Expires', date(\DateTime::RFC2822, $this->timeFactory->getTime()));
$response->addHeader('Content-Disposition', 'attachment'); $response->addHeader('Content-Disposition', 'attachment');
$response->addHeader('Content-Type', $this->config->getAppValue($this->appName, 'logoMime', '')); $response->addHeader('Content-Type', $this->config->getAppValue($this->appName, 'logoMime', ''));
$response->addHeader('Pragma', 'cache');
return $response; return $response;
} }
@ -256,6 +289,7 @@ class ThemingController extends Controller {
$response->addHeader('Expires', date(\DateTime::RFC2822, $this->timeFactory->getTime())); $response->addHeader('Expires', date(\DateTime::RFC2822, $this->timeFactory->getTime()));
$response->addHeader('Content-Disposition', 'attachment'); $response->addHeader('Content-Disposition', 'attachment');
$response->addHeader('Content-Type', $this->config->getAppValue($this->appName, 'backgroundMime', '')); $response->addHeader('Content-Type', $this->config->getAppValue($this->appName, 'backgroundMime', ''));
$response->addHeader('Pragma', 'cache');
return $response; return $response;
} }
@ -334,6 +368,7 @@ class ThemingController extends Controller {
$response = new DataDownloadResponse($responseCss, 'style', 'text/css'); $response = new DataDownloadResponse($responseCss, 'style', 'text/css');
$response->addHeader('Expires', date(\DateTime::RFC2822, $this->timeFactory->getTime())); $response->addHeader('Expires', date(\DateTime::RFC2822, $this->timeFactory->getTime()));
$response->addHeader('Pragma', 'cache');
$response->cacheFor(3600); $response->cacheFor(3600);
return $response; return $response;
} }
@ -354,8 +389,9 @@ class ThemingController extends Controller {
}; };
})();'; })();';
$response = new Http\DataDisplayResponse($responseJS); $response = new Http\DataDisplayResponse($responseJS);
$response->addHeader("Content-type","text/javascript"); $response->addHeader('Content-type', 'text/javascript');
$response->addHeader('Expires', date(\DateTime::RFC2822, $this->timeFactory->getTime())); $response->addHeader('Expires', date(\DateTime::RFC2822, $this->timeFactory->getTime()));
$response->addHeader('Pragma', 'cache');
$response->cacheFor(3600); $response->cacheFor(3600);
return $response; return $response;
} }

View File

@ -32,6 +32,7 @@ use OCP\Files\IRootFolder;
use OCP\IConfig; use OCP\IConfig;
use OCP\IL10N; use OCP\IL10N;
use OCP\IRequest; use OCP\IRequest;
use OCP\ITempManager;
use Test\TestCase; use Test\TestCase;
use OCA\Theming\ThemingDefaults; use OCA\Theming\ThemingDefaults;
@ -52,6 +53,8 @@ class ThemingControllerTest extends TestCase {
private $themingController; private $themingController;
/** @var IRootFolder|\PHPUnit_Framework_MockObject_MockObject */ /** @var IRootFolder|\PHPUnit_Framework_MockObject_MockObject */
private $rootFolder; private $rootFolder;
/** @var ITempManager */
private $tempManager;
public function setUp() { public function setUp() {
$this->request = $this->getMockBuilder('OCP\IRequest')->getMock(); $this->request = $this->getMockBuilder('OCP\IRequest')->getMock();
@ -64,10 +67,10 @@ class ThemingControllerTest extends TestCase {
->getMock(); ->getMock();
$this->l10n = $this->getMockBuilder('OCP\IL10N')->getMock(); $this->l10n = $this->getMockBuilder('OCP\IL10N')->getMock();
$this->rootFolder = $this->getMockBuilder('OCP\Files\IRootFolder')->getMock(); $this->rootFolder = $this->getMockBuilder('OCP\Files\IRootFolder')->getMock();
$this->timeFactory->expects($this->any()) $this->timeFactory->expects($this->any())
->method('getTime') ->method('getTime')
->willReturn(123); ->willReturn(123);
$this->tempManager = \OC::$server->getTempManager();
$this->themingController = new ThemingController( $this->themingController = new ThemingController(
'theming', 'theming',
@ -77,7 +80,8 @@ class ThemingControllerTest extends TestCase {
$this->util, $this->util,
$this->timeFactory, $this->timeFactory,
$this->l10n, $this->l10n,
$this->rootFolder $this->rootFolder,
$this->tempManager
); );
return parent::setUp(); return parent::setUp();
@ -214,6 +218,7 @@ class ThemingControllerTest extends TestCase {
$destination = \OC::$server->getTempManager()->getTemporaryFolder(); $destination = \OC::$server->getTempManager()->getTemporaryFolder();
touch($tmpLogo); touch($tmpLogo);
file_put_contents($tmpLogo, file_get_contents(__DIR__ . '/../../../../tests/data/desktopapp.png'));
$this->request $this->request
->expects($this->at(0)) ->expects($this->at(0))
->method('getUploadedFile') ->method('getUploadedFile')
@ -261,6 +266,52 @@ class ThemingControllerTest extends TestCase {
$this->assertEquals($expected, $this->themingController->updateLogo()); $this->assertEquals($expected, $this->themingController->updateLogo());
} }
public function testUpdateLogoLoginScreenUploadWithInvalidImage() {
$tmpLogo = \OC::$server->getTempManager()->getTemporaryFolder() . '/logo.svg';
$destination = \OC::$server->getTempManager()->getTemporaryFolder();
touch($tmpLogo);
file_put_contents($tmpLogo, file_get_contents(__DIR__ . '/../../../../tests/data/data.zip'));
$this->request
->expects($this->at(0))
->method('getUploadedFile')
->with('uploadlogo')
->willReturn(null);
$this->request
->expects($this->at(1))
->method('getUploadedFile')
->with('upload-login-background')
->willReturn([
'tmp_name' => $tmpLogo,
'type' => 'text/svg',
'name' => 'logo.svg',
]);
$this->l10n
->expects($this->once())
->method('t')
->with('Unsupported image type')
->willReturn('Unsupported image type');
$file = $this->getMockBuilder('\\OCP\\Files\\File')
->disableOriginalConstructor()
->getMock();
$this->rootFolder
->expects($this->once())
->method('newFile')
->with('themedbackgroundlogo')
->willReturn($file);
$expected = new DataResponse(
[
'data' =>
[
'message' => 'Unsupported image type',
],
'status' => 'failure'
],
Http::STATUS_UNPROCESSABLE_ENTITY
);
$this->assertEquals($expected, $this->themingController->updateLogo());
}
public function testUndo() { public function testUndo() {
$this->l10n $this->l10n
->expects($this->once()) ->expects($this->once())
@ -311,6 +362,7 @@ class ThemingControllerTest extends TestCase {
$expected->addHeader('Expires', date(\DateTime::RFC2822, 123)); $expected->addHeader('Expires', date(\DateTime::RFC2822, 123));
$expected->addHeader('Content-Disposition', 'attachment'); $expected->addHeader('Content-Disposition', 'attachment');
$expected->addHeader('Content-Type', 'text/svg'); $expected->addHeader('Content-Type', 'text/svg');
$expected->addHeader('Pragma', 'cache');
@$this->assertEquals($expected, $this->themingController->getLogo()); @$this->assertEquals($expected, $this->themingController->getLogo());
} }
@ -340,6 +392,7 @@ class ThemingControllerTest extends TestCase {
$expected->addHeader('Expires', date(\DateTime::RFC2822, 123)); $expected->addHeader('Expires', date(\DateTime::RFC2822, 123));
$expected->addHeader('Content-Disposition', 'attachment'); $expected->addHeader('Content-Disposition', 'attachment');
$expected->addHeader('Content-Type', 'image/png'); $expected->addHeader('Content-Type', 'image/png');
$expected->addHeader('Pragma', 'cache');
@$this->assertEquals($expected, $this->themingController->getLoginBackground()); @$this->assertEquals($expected, $this->themingController->getLoginBackground());
} }
@ -400,6 +453,7 @@ class ThemingControllerTest extends TestCase {
$expected->cacheFor(3600); $expected->cacheFor(3600);
$expected->addHeader('Expires', date(\DateTime::RFC2822, 123)); $expected->addHeader('Expires', date(\DateTime::RFC2822, 123));
$expected->addHeader('Pragma', 'cache');
@$this->assertEquals($expected, $this->themingController->getStylesheet()); @$this->assertEquals($expected, $this->themingController->getStylesheet());
} }
@ -464,6 +518,7 @@ class ThemingControllerTest extends TestCase {
$expected->cacheFor(3600); $expected->cacheFor(3600);
$expected->addHeader('Expires', date(\DateTime::RFC2822, 123)); $expected->addHeader('Expires', date(\DateTime::RFC2822, 123));
$expected->addHeader('Pragma', 'cache');
@$this->assertEquals($expected, $this->themingController->getStylesheet()); @$this->assertEquals($expected, $this->themingController->getStylesheet());
} }
@ -507,6 +562,7 @@ class ThemingControllerTest extends TestCase {
$expected->cacheFor(3600); $expected->cacheFor(3600);
$expected->addHeader('Expires', date(\DateTime::RFC2822, 123)); $expected->addHeader('Expires', date(\DateTime::RFC2822, 123));
$expected->addHeader('Pragma', 'cache');
@$this->assertEquals($expected, $this->themingController->getStylesheet()); @$this->assertEquals($expected, $this->themingController->getStylesheet());
} }
@ -542,6 +598,7 @@ class ThemingControllerTest extends TestCase {
$expected->cacheFor(3600); $expected->cacheFor(3600);
$expected->addHeader('Expires', date(\DateTime::RFC2822, 123)); $expected->addHeader('Expires', date(\DateTime::RFC2822, 123));
$expected->addHeader('Pragma', 'cache');
@$this->assertEquals($expected, $this->themingController->getStylesheet()); @$this->assertEquals($expected, $this->themingController->getStylesheet());
} }
@ -618,6 +675,7 @@ class ThemingControllerTest extends TestCase {
$expected->cacheFor(3600); $expected->cacheFor(3600);
$expected->addHeader('Expires', date(\DateTime::RFC2822, 123)); $expected->addHeader('Expires', date(\DateTime::RFC2822, 123));
$expected->addHeader('Pragma', 'cache');
@$this->assertEquals($expected, $this->themingController->getStylesheet()); @$this->assertEquals($expected, $this->themingController->getStylesheet());
} }
@ -698,6 +756,7 @@ class ThemingControllerTest extends TestCase {
$expected->cacheFor(3600); $expected->cacheFor(3600);
$expected->addHeader('Expires', date(\DateTime::RFC2822, 123)); $expected->addHeader('Expires', date(\DateTime::RFC2822, 123));
$expected->addHeader('Pragma', 'cache');
@$this->assertEquals($expected, $this->themingController->getStylesheet()); @$this->assertEquals($expected, $this->themingController->getStylesheet());
} }
@ -732,6 +791,7 @@ class ThemingControllerTest extends TestCase {
$expected = new Http\DataDisplayResponse($expectedResponse); $expected = new Http\DataDisplayResponse($expectedResponse);
$expected->addHeader("Content-type","text/javascript"); $expected->addHeader("Content-type","text/javascript");
$expected->addHeader('Expires', date(\DateTime::RFC2822, $this->timeFactory->getTime())); $expected->addHeader('Expires', date(\DateTime::RFC2822, $this->timeFactory->getTime()));
$expected->addHeader('Pragma', 'cache');
$expected->cacheFor(3600); $expected->cacheFor(3600);
@$this->assertEquals($expected, $this->themingController->getJavascript()); @$this->assertEquals($expected, $this->themingController->getJavascript());
} }
@ -765,6 +825,7 @@ class ThemingControllerTest extends TestCase {
$expected = new Http\DataDisplayResponse($expectedResponse); $expected = new Http\DataDisplayResponse($expectedResponse);
$expected->addHeader("Content-type","text/javascript"); $expected->addHeader("Content-type","text/javascript");
$expected->addHeader('Expires', date(\DateTime::RFC2822, $this->timeFactory->getTime())); $expected->addHeader('Expires', date(\DateTime::RFC2822, $this->timeFactory->getTime()));
$expected->addHeader('Pragma', 'cache');
$expected->cacheFor(3600); $expected->cacheFor(3600);
@$this->assertEquals($expected, $this->themingController->getJavascript()); @$this->assertEquals($expected, $this->themingController->getJavascript());
} }