Merge pull request #14825 from rullzer/avatarcontroller_node

AvatarController to all OCP functions/Node API
This commit is contained in:
Vincent Petry 2015-08-18 16:24:23 +02:00
commit 2411662095
3 changed files with 30 additions and 19 deletions

View File

@ -82,7 +82,8 @@ class Application extends App {
$c->query('Cache'), $c->query('Cache'),
$c->query('L10N'), $c->query('L10N'),
$c->query('UserManager'), $c->query('UserManager'),
$c->query('UserSession') $c->query('UserSession'),
$c->query('UserFolder')
); );
}); });
@ -116,6 +117,10 @@ class Application extends App {
$container->registerService('Cache', function(SimpleContainer $c) { $container->registerService('Cache', function(SimpleContainer $c) {
return $c->query('ServerContainer')->getCache(); return $c->query('ServerContainer')->getCache();
}); });
$container->registerService('UserFolder', function(SimpleContainer $c) {
return $c->query('ServerContainer')->getUserFolder();
});
$container->registerService('Defaults', function() { $container->registerService('Defaults', function() {

View File

@ -34,6 +34,7 @@ use OCP\IL10N;
use OCP\IRequest; use OCP\IRequest;
use OCP\IUserManager; use OCP\IUserManager;
use OCP\IUserSession; use OCP\IUserSession;
use OCP\Files\Folder;
/** /**
* Class AvatarController * Class AvatarController
@ -57,6 +58,9 @@ class AvatarController extends Controller {
/** @var IUserSession */ /** @var IUserSession */
protected $userSession; protected $userSession;
/** @var Folder */
protected $userFolder;
/** /**
* @param string $appName * @param string $appName
* @param IRequest $request * @param IRequest $request
@ -65,6 +69,7 @@ class AvatarController extends Controller {
* @param IL10N $l10n * @param IL10N $l10n
* @param IUserManager $userManager * @param IUserManager $userManager
* @param IUserSession $userSession * @param IUserSession $userSession
* @param Folder $userFolder
*/ */
public function __construct($appName, public function __construct($appName,
IRequest $request, IRequest $request,
@ -72,7 +77,8 @@ class AvatarController extends Controller {
\OC\Cache\File $cache, \OC\Cache\File $cache,
IL10N $l10n, IL10N $l10n,
IUserManager $userManager, IUserManager $userManager,
IUserSession $userSession) { IUserSession $userSession,
Folder $userFolder) {
parent::__construct($appName, $request); parent::__construct($appName, $request);
$this->avatarManager = $avatarManager; $this->avatarManager = $avatarManager;
@ -80,6 +86,7 @@ class AvatarController extends Controller {
$this->l = $l10n; $this->l = $l10n;
$this->userManager = $userManager; $this->userManager = $userManager;
$this->userSession = $userSession; $this->userSession = $userSession;
$this->userFolder = $userFolder;
} }
/** /**
@ -133,12 +140,12 @@ class AvatarController extends Controller {
if (isset($path)) { if (isset($path)) {
$path = stripslashes($path); $path = stripslashes($path);
$view = new \OC\Files\View('/'.$userId.'/files'); $node = $this->userFolder->get($path);
if ($view->filesize($path) > 20*1024*1024) { if ($node->getSize() > 20*1024*1024) {
return new DataResponse(['data' => ['message' => $this->l->t('File is too big')]], return new DataResponse(['data' => ['message' => $this->l->t('File is too big')]],
Http::STATUS_BAD_REQUEST); Http::STATUS_BAD_REQUEST);
} }
$fileName = $view->getLocalFile($path); $content = $node->getContent();
} elseif (!is_null($files)) { } elseif (!is_null($files)) {
if ( if (
$files['error'][0] === 0 && $files['error'][0] === 0 &&
@ -150,8 +157,7 @@ class AvatarController extends Controller {
Http::STATUS_BAD_REQUEST); Http::STATUS_BAD_REQUEST);
} }
$this->cache->set('avatar_upload', file_get_contents($files['tmp_name'][0]), 7200); $this->cache->set('avatar_upload', file_get_contents($files['tmp_name'][0]), 7200);
$view = new \OC\Files\View('/'.$userId.'/cache'); $content = $this->cache->get('avatar_upload');
$fileName = $view->getLocalFile('avatar_upload');
unlink($files['tmp_name'][0]); unlink($files['tmp_name'][0]);
} else { } else {
return new DataResponse(['data' => ['message' => $this->l->t('Invalid file provided')]], return new DataResponse(['data' => ['message' => $this->l->t('Invalid file provided')]],
@ -165,7 +171,7 @@ class AvatarController extends Controller {
try { try {
$image = new \OC_Image(); $image = new \OC_Image();
$image->loadFromFile($fileName); $image->loadFromData($content);
$image->fixOrientation(); $image->fixOrientation();
if ($image->valid()) { if ($image->valid()) {

View File

@ -26,6 +26,8 @@ use OCP\AppFramework\IAppContainer;
use OC\Files\Filesystem; use OC\Files\Filesystem;
use OCP\AppFramework\Http; use OCP\AppFramework\Http;
use OCP\Image; use OCP\Image;
use OCP\Files\Folder;
use OCP\Files\File;
/** /**
* Overwrite is_uploaded_file in this namespace to allow proper unit testing of * Overwrite is_uploaded_file in this namespace to allow proper unit testing of
@ -72,6 +74,8 @@ class AvatarControllerTest extends \Test\TestCase {
->disableOriginalConstructor()->getMock(); ->disableOriginalConstructor()->getMock();
$this->container['Request'] = $this->getMockBuilder('OCP\IRequest') $this->container['Request'] = $this->getMockBuilder('OCP\IRequest')
->disableOriginalConstructor()->getMock(); ->disableOriginalConstructor()->getMock();
$this->container['UserFolder'] = $this->getMockBuilder('OCP\Files\Folder')
->disableOriginalConstructor()->getMock();
$this->avatarMock = $this->getMockBuilder('OCP\IAvatar') $this->avatarMock = $this->getMockBuilder('OCP\IAvatar')
->disableOriginalConstructor()->getMock(); ->disableOriginalConstructor()->getMock();
@ -89,10 +93,6 @@ class AvatarControllerTest extends \Test\TestCase {
OC::$server->getUserManager()->createUser($this->user, $this->user); OC::$server->getUserManager()->createUser($this->user, $this->user);
$this->loginAsUser($this->user); $this->loginAsUser($this->user);
// Create Cache dir
$view = new \OC\Files\View('/'.$this->user);
$view->mkdir('cache');
// Configure userMock // Configure userMock
$this->userMock->method('getDisplayName')->willReturn($this->user); $this->userMock->method('getDisplayName')->willReturn($this->user);
$this->userMock->method('getUID')->willReturn($this->user); $this->userMock->method('getUID')->willReturn($this->user);
@ -259,8 +259,7 @@ class AvatarControllerTest extends \Test\TestCase {
$this->assertTrue($copyRes); $this->assertTrue($copyRes);
//Create file in cache //Create file in cache
$view = new \OC\Files\View('/'.$this->user.'/cache'); $this->container['Cache']->method('get')->willReturn(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.jpg'));
$view->file_put_contents('avatar_upload', file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.jpg'));
//Create request return //Create request return
$reqRet = ['error' => [0], 'tmp_name' => [$fileName], 'size' => [filesize(OC::$SERVERROOT.'/tests/data/testimage.jpg')]]; $reqRet = ['error' => [0], 'tmp_name' => [$fileName], 'size' => [filesize(OC::$SERVERROOT.'/tests/data/testimage.jpg')]];
@ -298,8 +297,7 @@ class AvatarControllerTest extends \Test\TestCase {
$this->assertTrue($copyRes); $this->assertTrue($copyRes);
//Create file in cache //Create file in cache
$view = new \OC\Files\View('/'.$this->user.'/cache'); $this->container['Cache']->method('get')->willReturn(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.gif'));
$view->file_put_contents('avatar_upload', file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.gif'));
//Create request return //Create request return
$reqRet = ['error' => [0], 'tmp_name' => [$fileName], 'size' => filesize(OC::$SERVERROOT.'/tests/data/testimage.gif')]; $reqRet = ['error' => [0], 'tmp_name' => [$fileName], 'size' => filesize(OC::$SERVERROOT.'/tests/data/testimage.gif')];
@ -317,9 +315,11 @@ class AvatarControllerTest extends \Test\TestCase {
* Test posting avatar from existing file * Test posting avatar from existing file
*/ */
public function testPostAvatarFromFile() { public function testPostAvatarFromFile() {
//Create file in cache //Mock node API call
$view = new \OC\Files\View('/'.$this->user.'/files'); $file = $this->getMockBuilder('OCP\Files\File')
$view->file_put_contents('avatar.jpg', file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.jpg')); ->disableOriginalConstructor()->getMock();
$file->method('getContent')->willReturn(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.jpg'));
$this->container['UserFolder']->method('get')->willReturn($file);
//Create request return //Create request return
$response = $this->avatarController->postAvatar('avatar.jpg'); $response = $this->avatarController->postAvatar('avatar.jpg');