Use temporary file as cache

This commit is contained in:
Lukas Reschke 2016-08-27 21:38:41 +02:00
parent d4d90a0b84
commit 49da5267a9
No known key found for this signature in database
GPG Key ID: B9F6980CF6E759B1
2 changed files with 16 additions and 4 deletions

View File

@ -39,6 +39,7 @@ use OCP\IConfig;
use OCP\IL10N;
use OCP\IRequest;
use OCA\Theming\Util;
use OCP\ITempManager;
/**
* Class ThemingController
@ -60,6 +61,8 @@ class ThemingController extends Controller {
private $config;
/** @var IRootFolder */
private $rootFolder;
/** @var ITempManager */
private $tempManager;
/**
* ThemingController constructor.
@ -72,6 +75,7 @@ class ThemingController extends Controller {
* @param ITimeFactory $timeFactory
* @param IL10N $l
* @param IRootFolder $rootFolder
* @param ITempManager $tempManager
*/
public function __construct(
$appName,
@ -81,7 +85,8 @@ class ThemingController extends Controller {
Util $util,
ITimeFactory $timeFactory,
IL10N $l,
IRootFolder $rootFolder
IRootFolder $rootFolder,
ITempManager $tempManager
) {
parent::__construct($appName, $request);
@ -91,6 +96,7 @@ class ThemingController extends Controller {
$this->l = $l;
$this->config = $config;
$this->rootFolder = $rootFolder;
$this->tempManager = $tempManager;
}
/**
@ -199,14 +205,16 @@ class ThemingController extends Controller {
// 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, $target->fopen('w'), 75);
imagejpeg($image, $tmpFile, 75);
imagedestroy($image);
stream_copy_to_stream(fopen($tmpFile, 'r'), $target->fopen('w'));
$this->template->set('backgroundMime', $newBackgroundLogo['type']);
$name = $newBackgroundLogo['name'];
}

View File

@ -32,6 +32,7 @@ use OCP\Files\IRootFolder;
use OCP\IConfig;
use OCP\IL10N;
use OCP\IRequest;
use OCP\ITempManager;
use Test\TestCase;
use OCA\Theming\ThemingDefaults;
@ -52,6 +53,8 @@ class ThemingControllerTest extends TestCase {
private $themingController;
/** @var IRootFolder|\PHPUnit_Framework_MockObject_MockObject */
private $rootFolder;
/** @var ITempManager */
private $tempManager;
public function setUp() {
$this->request = $this->getMockBuilder('OCP\IRequest')->getMock();
@ -64,10 +67,10 @@ class ThemingControllerTest extends TestCase {
->getMock();
$this->l10n = $this->getMockBuilder('OCP\IL10N')->getMock();
$this->rootFolder = $this->getMockBuilder('OCP\Files\IRootFolder')->getMock();
$this->timeFactory->expects($this->any())
->method('getTime')
->willReturn(123);
$this->tempManager = \OC::$server->getTempManager();
$this->themingController = new ThemingController(
'theming',
@ -77,7 +80,8 @@ class ThemingControllerTest extends TestCase {
$this->util,
$this->timeFactory,
$this->l10n,
$this->rootFolder
$this->rootFolder,
$this->tempManager
);
return parent::setUp();