Move avatarcontroller towards Node Api

This commit is contained in:
Roeland Jago Douma 2015-03-12 11:13:12 +01:00
parent ffff156965
commit 65cf4028cc
3 changed files with 30 additions and 19 deletions

View File

@ -82,7 +82,8 @@ class Application extends App {
$c->query('Cache'),
$c->query('L10N'),
$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) {
return $c->query('ServerContainer')->getCache();
});
$container->registerService('UserFolder', function(SimpleContainer $c) {
return $c->query('ServerContainer')->getUserFolder();
});
$container->registerService('Defaults', function() {

View File

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

View File

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