Use stream instead of rename

This commit is contained in:
Lukas Reschke 2016-06-27 10:47:44 +02:00
parent 0a5c5d9b03
commit 51646bb3f6
2 changed files with 45 additions and 14 deletions

View File

@ -26,6 +26,7 @@ use OCA\Theming\Template;
use OCP\AppFramework\Controller; use OCP\AppFramework\Controller;
use OCP\AppFramework\Http; use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataResponse; use OCP\AppFramework\Http\DataResponse;
use OCP\Files\IRootFolder;
use OCP\IConfig; use OCP\IConfig;
use OCP\IL10N; use OCP\IL10N;
use OCP\IRequest; use OCP\IRequest;
@ -44,6 +45,8 @@ class ThemingController extends Controller {
private $l; private $l;
/** @var IConfig */ /** @var IConfig */
private $config; private $config;
/** @var IRootFolder */
private $rootFolder;
/** /**
* ThemingController constructor. * ThemingController constructor.
@ -53,19 +56,22 @@ class ThemingController extends Controller {
* @param IConfig $config * @param IConfig $config
* @param Template $template * @param Template $template
* @param IL10N $l * @param IL10N $l
* @param IRootFolder $rootFolder
*/ */
public function __construct( public function __construct(
$appName, $appName,
IRequest $request, IRequest $request,
IConfig $config, IConfig $config,
Template $template, Template $template,
IL10N $l IL10N $l,
IRootFolder $rootFolder
) { ) {
parent::__construct($appName, $request); parent::__construct($appName, $request);
$this->template = $template; $this->template = $template;
$this->l = $l; $this->l = $l;
$this->config = $config; $this->config = $config;
$this->rootFolder = $rootFolder;
} }
/** /**
@ -106,12 +112,14 @@ class ThemingController extends Controller {
} }
$name = ''; $name = '';
if(!empty($newLogo)) { 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']); $this->template->set('logoMime', $newLogo['type']);
$name = $newLogo['name']; $name = $newLogo['name'];
} }
if(!empty($newBackgroundLogo)) { 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']); $this->template->set('backgroundMime', $newBackgroundLogo['type']);
$name = $newBackgroundLogo['name']; $name = $newBackgroundLogo['name'];
} }

View File

@ -24,6 +24,7 @@ use OCA\Theming\Controller\ThemingController;
use OCA\Theming\Template; use OCA\Theming\Template;
use OCP\AppFramework\Http; use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataResponse; use OCP\AppFramework\Http\DataResponse;
use OCP\Files\IRootFolder;
use OCP\IConfig; use OCP\IConfig;
use OCP\IL10N; use OCP\IL10N;
use OCP\IRequest; use OCP\IRequest;
@ -40,6 +41,8 @@ class ThemingControllerTest extends TestCase {
private $l10n; private $l10n;
/** @var ThemingController */ /** @var ThemingController */
private $themingController; private $themingController;
/** @var IRootFolder */
private $rootFolder;
public function setUp() { public function setUp() {
$this->request = $this->getMock('\\OCP\\IRequest'); $this->request = $this->getMock('\\OCP\\IRequest');
@ -47,12 +50,15 @@ class ThemingControllerTest extends TestCase {
$this->template = $this->getMockBuilder('\\OCA\\Theming\\Template') $this->template = $this->getMockBuilder('\\OCA\\Theming\\Template')
->disableOriginalConstructor()->getMock(); ->disableOriginalConstructor()->getMock();
$this->l10n = $this->getMock('\\OCP\\IL10N'); $this->l10n = $this->getMock('\\OCP\\IL10N');
$this->rootFolder = $this->getMock('\\OCP\\Files\\IRootFolder');
$this->themingController = new ThemingController( $this->themingController = new ThemingController(
'theming', 'theming',
$this->request, $this->request,
$this->config, $this->config,
$this->template, $this->template,
$this->l10n $this->l10n,
$this->rootFolder
); );
return parent::setUp(); return parent::setUp();
@ -130,16 +136,24 @@ class ThemingControllerTest extends TestCase {
->method('getUploadedFile') ->method('getUploadedFile')
->with('upload-login-background') ->with('upload-login-background')
->willReturn(null); ->willReturn(null);
$this->config
->expects($this->at(0))
->method('getSystemValue')
->with('datadirectory', \OC::$SERVERROOT . '/data')
->willReturn($destination);
$this->l10n $this->l10n
->expects($this->once()) ->expects($this->once())
->method('t') ->method('t')
->with('Saved') ->with('Saved')
->willReturn('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( $expected = new DataResponse(
[ [
@ -174,16 +188,25 @@ class ThemingControllerTest extends TestCase {
'type' => 'text/svg', 'type' => 'text/svg',
'name' => 'logo.svg', 'name' => 'logo.svg',
]); ]);
$this->config
->expects($this->at(0))
->method('getSystemValue')
->with('datadirectory', \OC::$SERVERROOT . '/data')
->willReturn($destination);
$this->l10n $this->l10n
->expects($this->once()) ->expects($this->once())
->method('t') ->method('t')
->with('Saved') ->with('Saved')
->willReturn('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( $expected = new DataResponse(
[ [