From 51646bb3f66bd5ffd97759d7b894bd142b2a9525 Mon Sep 17 00:00:00 2001 From: Lukas Reschke Date: Mon, 27 Jun 2016 10:47:44 +0200 Subject: [PATCH] Use stream instead of rename --- .../lib/controller/themingcontroller.php | 14 ++++-- .../lib/controller/ThemingControllerTest.php | 45 ++++++++++++++----- 2 files changed, 45 insertions(+), 14 deletions(-) diff --git a/apps/theming/lib/controller/themingcontroller.php b/apps/theming/lib/controller/themingcontroller.php index dd4ff82195..995f929b51 100644 --- a/apps/theming/lib/controller/themingcontroller.php +++ b/apps/theming/lib/controller/themingcontroller.php @@ -26,6 +26,7 @@ use OCA\Theming\Template; use OCP\AppFramework\Controller; use OCP\AppFramework\Http; use OCP\AppFramework\Http\DataResponse; +use OCP\Files\IRootFolder; use OCP\IConfig; use OCP\IL10N; use OCP\IRequest; @@ -44,6 +45,8 @@ class ThemingController extends Controller { private $l; /** @var IConfig */ private $config; + /** @var IRootFolder */ + private $rootFolder; /** * ThemingController constructor. @@ -53,19 +56,22 @@ class ThemingController extends Controller { * @param IConfig $config * @param Template $template * @param IL10N $l + * @param IRootFolder $rootFolder */ public function __construct( $appName, IRequest $request, IConfig $config, Template $template, - IL10N $l + IL10N $l, + IRootFolder $rootFolder ) { parent::__construct($appName, $request); $this->template = $template; $this->l = $l; $this->config = $config; + $this->rootFolder = $rootFolder; } /** @@ -106,12 +112,14 @@ class ThemingController extends Controller { } $name = ''; if(!empty($newLogo)) { - rename($newLogo['tmp_name'], $this->config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/themedinstancelogo'); + $target = $this->rootFolder->newFile('themedinstancelogo'); + stream_copy_to_stream(fopen($newLogo['tmp_name'], 'r'), $target->fopen('w')); $this->template->set('logoMime', $newLogo['type']); $name = $newLogo['name']; } if(!empty($newBackgroundLogo)) { - rename($newBackgroundLogo['tmp_name'], $this->config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/themedbackgroundlogo'); + $target = $this->rootFolder->newFile('themedbackgroundlogo'); + stream_copy_to_stream(fopen($newBackgroundLogo['tmp_name'], 'r'), $target->fopen('w')); $this->template->set('backgroundMime', $newBackgroundLogo['type']); $name = $newBackgroundLogo['name']; } diff --git a/apps/theming/tests/lib/controller/ThemingControllerTest.php b/apps/theming/tests/lib/controller/ThemingControllerTest.php index 82aa7d1381..7fba27316a 100644 --- a/apps/theming/tests/lib/controller/ThemingControllerTest.php +++ b/apps/theming/tests/lib/controller/ThemingControllerTest.php @@ -24,6 +24,7 @@ use OCA\Theming\Controller\ThemingController; use OCA\Theming\Template; use OCP\AppFramework\Http; use OCP\AppFramework\Http\DataResponse; +use OCP\Files\IRootFolder; use OCP\IConfig; use OCP\IL10N; use OCP\IRequest; @@ -40,6 +41,8 @@ class ThemingControllerTest extends TestCase { private $l10n; /** @var ThemingController */ private $themingController; + /** @var IRootFolder */ + private $rootFolder; public function setUp() { $this->request = $this->getMock('\\OCP\\IRequest'); @@ -47,12 +50,15 @@ class ThemingControllerTest extends TestCase { $this->template = $this->getMockBuilder('\\OCA\\Theming\\Template') ->disableOriginalConstructor()->getMock(); $this->l10n = $this->getMock('\\OCP\\IL10N'); + $this->rootFolder = $this->getMock('\\OCP\\Files\\IRootFolder'); + $this->themingController = new ThemingController( 'theming', $this->request, $this->config, $this->template, - $this->l10n + $this->l10n, + $this->rootFolder ); return parent::setUp(); @@ -130,16 +136,24 @@ class ThemingControllerTest extends TestCase { ->method('getUploadedFile') ->with('upload-login-background') ->willReturn(null); - $this->config - ->expects($this->at(0)) - ->method('getSystemValue') - ->with('datadirectory', \OC::$SERVERROOT . '/data') - ->willReturn($destination); $this->l10n ->expects($this->once()) ->method('t') ->with('Saved') ->willReturn('Saved'); + $file = $this->getMockBuilder('\\OCP\\Files\\File') + ->disableOriginalConstructor() + ->getMock(); + $this->rootFolder + ->expects($this->once()) + ->method('newFile') + ->with('themedinstancelogo') + ->willReturn($file); + $file + ->expects($this->once()) + ->method('fopen') + ->with('w') + ->willReturn(fopen($destination . '/themedinstancelogo', 'w')); $expected = new DataResponse( [ @@ -174,16 +188,25 @@ class ThemingControllerTest extends TestCase { 'type' => 'text/svg', 'name' => 'logo.svg', ]); - $this->config - ->expects($this->at(0)) - ->method('getSystemValue') - ->with('datadirectory', \OC::$SERVERROOT . '/data') - ->willReturn($destination); $this->l10n ->expects($this->once()) ->method('t') ->with('Saved') ->willReturn('Saved'); + $file = $this->getMockBuilder('\\OCP\\Files\\File') + ->disableOriginalConstructor() + ->getMock(); + $this->rootFolder + ->expects($this->once()) + ->method('newFile') + ->with('themedbackgroundlogo') + ->willReturn($file); + $file + ->expects($this->once()) + ->method('fopen') + ->with('w') + ->willReturn(fopen($destination . '/themedbackgroundlogo', 'w')); + $expected = new DataResponse( [