AvatarController use proper JSONResponse

* Do not rely on DataResponse magic. We want JSON so use JSON
* Fix tests
This commit is contained in:
Roeland Jago Douma 2016-08-29 21:31:41 +02:00
parent f6423f74e3
commit b1a090f357
No known key found for this signature in database
GPG key ID: 1E152838F164D13B
2 changed files with 36 additions and 45 deletions

View file

@ -29,8 +29,8 @@ namespace OC\Core\Controller;
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\DataDisplayResponse; use OCP\AppFramework\Http\DataDisplayResponse;
use OCP\AppFramework\Http\JSONResponse;
use OCP\Files\File; use OCP\Files\File;
use OCP\Files\IRootFolder; use OCP\Files\IRootFolder;
use OCP\Files\NotFoundException; use OCP\Files\NotFoundException;
@ -111,7 +111,7 @@ class AvatarController extends Controller {
* *
* @param string $userId * @param string $userId
* @param int $size * @param int $size
* @return DataResponse|DataDisplayResponse * @return JSONResponse|DataDisplayResponse
*/ */
public function getAvatar($userId, $size) { public function getAvatar($userId, $size) {
if ($size > 2048) { if ($size > 2048) {
@ -128,13 +128,13 @@ class AvatarController extends Controller {
$resp->setETag($avatar->getEtag()); $resp->setETag($avatar->getEtag());
} catch (NotFoundException $e) { } catch (NotFoundException $e) {
$user = $this->userManager->get($userId); $user = $this->userManager->get($userId);
$resp = new DataResponse([ $resp = new JSONResponse([
'data' => [ 'data' => [
'displayname' => $user->getDisplayName(), 'displayname' => $user->getDisplayName(),
], ],
]); ]);
} catch (\Exception $e) { } catch (\Exception $e) {
$resp = new DataResponse([ $resp = new JSONResponse([
'data' => [ 'data' => [
'displayname' => '', 'displayname' => '',
], ],
@ -152,25 +152,22 @@ class AvatarController extends Controller {
* @NoAdminRequired * @NoAdminRequired
* *
* @param string $path * @param string $path
* @return DataResponse * @return JSONResponse
*/ */
public function postAvatar($path) { public function postAvatar($path) {
$files = $this->request->getUploadedFile('files'); $files = $this->request->getUploadedFile('files');
$headers = [];
if (isset($path)) { if (isset($path)) {
$path = stripslashes($path); $path = stripslashes($path);
$userFolder = $this->rootFolder->getUserFolder($this->userId); $userFolder = $this->rootFolder->getUserFolder($this->userId);
$node = $userFolder->get($path); $node = $userFolder->get($path);
if (!($node instanceof File)) { if (!($node instanceof File)) {
return new DataResponse(['data' => ['message' => $this->l->t('Please select a file.')]], Http::STATUS_OK, $headers); return new JSONResponse(['data' => ['message' => $this->l->t('Please select a file.')]]);
} }
if ($node->getSize() > 20*1024*1024) { if ($node->getSize() > 20*1024*1024) {
return new DataResponse( return new JSONResponse(
['data' => ['message' => $this->l->t('File is too big')]], ['data' => ['message' => $this->l->t('File is too big')]],
Http::STATUS_BAD_REQUEST, Http::STATUS_BAD_REQUEST
$headers
); );
} }
$content = $node->getContent(); $content = $node->getContent();
@ -181,28 +178,25 @@ class AvatarController extends Controller {
!\OC\Files\Filesystem::isFileBlacklisted($files['tmp_name'][0]) !\OC\Files\Filesystem::isFileBlacklisted($files['tmp_name'][0])
) { ) {
if ($files['size'][0] > 20*1024*1024) { if ($files['size'][0] > 20*1024*1024) {
return new DataResponse( return new JSONResponse(
['data' => ['message' => $this->l->t('File is too big')]], ['data' => ['message' => $this->l->t('File is too big')]],
Http::STATUS_BAD_REQUEST, Http::STATUS_BAD_REQUEST
$headers
); );
} }
$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);
$content = $this->cache->get('avatar_upload'); $content = $this->cache->get('avatar_upload');
unlink($files['tmp_name'][0]); unlink($files['tmp_name'][0]);
} else { } else {
return new DataResponse( return new JSONResponse(
['data' => ['message' => $this->l->t('Invalid file provided')]], ['data' => ['message' => $this->l->t('Invalid file provided')]],
Http::STATUS_BAD_REQUEST, Http::STATUS_BAD_REQUEST
$headers
); );
} }
} else { } else {
//Add imgfile //Add imgfile
return new DataResponse( return new JSONResponse(
['data' => ['message' => $this->l->t('No image or file provided')]], ['data' => ['message' => $this->l->t('No image or file provided')]],
Http::STATUS_BAD_REQUEST, Http::STATUS_BAD_REQUEST
$headers
); );
} }
@ -214,57 +208,54 @@ class AvatarController extends Controller {
if ($image->valid()) { if ($image->valid()) {
$mimeType = $image->mimeType(); $mimeType = $image->mimeType();
if ($mimeType !== 'image/jpeg' && $mimeType !== 'image/png') { if ($mimeType !== 'image/jpeg' && $mimeType !== 'image/png') {
return new DataResponse( return new JSONResponse(
['data' => ['message' => $this->l->t('Unknown filetype')]], ['data' => ['message' => $this->l->t('Unknown filetype')]],
Http::STATUS_OK, Http::STATUS_OK
$headers
); );
} }
$this->cache->set('tmpAvatar', $image->data(), 7200); $this->cache->set('tmpAvatar', $image->data(), 7200);
return new DataResponse( return new JSONResponse(
['data' => 'notsquare'], ['data' => 'notsquare'],
Http::STATUS_OK, Http::STATUS_OK
$headers
); );
} else { } else {
return new DataResponse( return new JSONResponse(
['data' => ['message' => $this->l->t('Invalid image')]], ['data' => ['message' => $this->l->t('Invalid image')]],
Http::STATUS_OK, Http::STATUS_OK
$headers
); );
} }
} catch (\Exception $e) { } catch (\Exception $e) {
$this->logger->logException($e, ['app' => 'core']); $this->logger->logException($e, ['app' => 'core']);
return new DataResponse(['data' => ['message' => $this->l->t('An error occurred. Please contact your admin.')]], Http::STATUS_OK, $headers); return new JSONResponse(['data' => ['message' => $this->l->t('An error occurred. Please contact your admin.')]], Http::STATUS_OK);
} }
} }
/** /**
* @NoAdminRequired * @NoAdminRequired
* *
* @return DataResponse * @return JSONResponse
*/ */
public function deleteAvatar() { public function deleteAvatar() {
try { try {
$avatar = $this->avatarManager->getAvatar($this->userId); $avatar = $this->avatarManager->getAvatar($this->userId);
$avatar->remove(); $avatar->remove();
return new DataResponse(); return new JSONResponse();
} catch (\Exception $e) { } catch (\Exception $e) {
$this->logger->logException($e, ['app' => 'core']); $this->logger->logException($e, ['app' => 'core']);
return new DataResponse(['data' => ['message' => $this->l->t('An error occurred. Please contact your admin.')]], Http::STATUS_BAD_REQUEST); return new JSONResponse(['data' => ['message' => $this->l->t('An error occurred. Please contact your admin.')]], Http::STATUS_BAD_REQUEST);
} }
} }
/** /**
* @NoAdminRequired * @NoAdminRequired
* *
* @return DataResponse|DataDisplayResponse * @return JSONResponse|DataDisplayResponse
*/ */
public function getTmpAvatar() { public function getTmpAvatar() {
$tmpAvatar = $this->cache->get('tmpAvatar'); $tmpAvatar = $this->cache->get('tmpAvatar');
if (is_null($tmpAvatar)) { if (is_null($tmpAvatar)) {
return new DataResponse(['data' => [ return new JSONResponse(['data' => [
'message' => $this->l->t("No temporary profile picture available, try again") 'message' => $this->l->t("No temporary profile picture available, try again")
]], ]],
Http::STATUS_NOT_FOUND); Http::STATUS_NOT_FOUND);
@ -286,22 +277,22 @@ class AvatarController extends Controller {
* @NoAdminRequired * @NoAdminRequired
* *
* @param array $crop * @param array $crop
* @return DataResponse * @return JSONResponse
*/ */
public function postCroppedAvatar($crop) { public function postCroppedAvatar($crop) {
if (is_null($crop)) { if (is_null($crop)) {
return new DataResponse(['data' => ['message' => $this->l->t("No crop data provided")]], return new JSONResponse(['data' => ['message' => $this->l->t("No crop data provided")]],
Http::STATUS_BAD_REQUEST); Http::STATUS_BAD_REQUEST);
} }
if (!isset($crop['x'], $crop['y'], $crop['w'], $crop['h'])) { if (!isset($crop['x'], $crop['y'], $crop['w'], $crop['h'])) {
return new DataResponse(['data' => ['message' => $this->l->t("No valid crop data provided")]], return new JSONResponse(['data' => ['message' => $this->l->t("No valid crop data provided")]],
Http::STATUS_BAD_REQUEST); Http::STATUS_BAD_REQUEST);
} }
$tmpAvatar = $this->cache->get('tmpAvatar'); $tmpAvatar = $this->cache->get('tmpAvatar');
if (is_null($tmpAvatar)) { if (is_null($tmpAvatar)) {
return new DataResponse(['data' => [ return new JSONResponse(['data' => [
'message' => $this->l->t("No temporary profile picture available, try again") 'message' => $this->l->t("No temporary profile picture available, try again")
]], ]],
Http::STATUS_BAD_REQUEST); Http::STATUS_BAD_REQUEST);
@ -314,13 +305,13 @@ class AvatarController extends Controller {
$avatar->set($image); $avatar->set($image);
// Clean up // Clean up
$this->cache->remove('tmpAvatar'); $this->cache->remove('tmpAvatar');
return new DataResponse(['status' => 'success']); return new JSONResponse(['status' => 'success']);
} catch (\OC\NotSquareException $e) { } catch (\OC\NotSquareException $e) {
return new DataResponse(['data' => ['message' => $this->l->t('Crop is not square')]], return new JSONResponse(['data' => ['message' => $this->l->t('Crop is not square')]],
Http::STATUS_BAD_REQUEST); Http::STATUS_BAD_REQUEST);
} catch (\Exception $e) { } catch (\Exception $e) {
$this->logger->logException($e, ['app' => 'core']); $this->logger->logException($e, ['app' => 'core']);
return new DataResponse(['data' => ['message' => $this->l->t('An error occurred. Please contact your admin.')]], Http::STATUS_BAD_REQUEST); return new JSONResponse(['data' => ['message' => $this->l->t('An error occurred. Please contact your admin.')]], Http::STATUS_BAD_REQUEST);
} }
} }
} }

View file

@ -228,7 +228,7 @@ class AvatarControllerTest extends \Test\TestCase {
$this->logger->expects($this->once()) $this->logger->expects($this->once())
->method('logException') ->method('logException')
->with(new \Exception("foo")); ->with(new \Exception("foo"));
$expectedResponse = new Http\DataResponse(['data' => ['message' => 'An error occurred. Please contact your admin.']], Http::STATUS_BAD_REQUEST); $expectedResponse = new Http\JSONResponse(['data' => ['message' => 'An error occurred. Please contact your admin.']], Http::STATUS_BAD_REQUEST);
$this->assertEquals($expectedResponse, $this->avatarController->deleteAvatar()); $this->assertEquals($expectedResponse, $this->avatarController->deleteAvatar());
} }
@ -377,7 +377,7 @@ class AvatarControllerTest extends \Test\TestCase {
$this->logger->expects($this->once()) $this->logger->expects($this->once())
->method('logException') ->method('logException')
->with(new \Exception("foo")); ->with(new \Exception("foo"));
$expectedResponse = new Http\DataResponse(['data' => ['message' => 'An error occurred. Please contact your admin.']], Http::STATUS_OK); $expectedResponse = new Http\JSONResponse(['data' => ['message' => 'An error occurred. Please contact your admin.']], Http::STATUS_OK);
$this->assertEquals($expectedResponse, $this->avatarController->postAvatar('avatar.jpg')); $this->assertEquals($expectedResponse, $this->avatarController->postAvatar('avatar.jpg'));
} }
@ -437,7 +437,7 @@ class AvatarControllerTest extends \Test\TestCase {
$this->logger->expects($this->once()) $this->logger->expects($this->once())
->method('logException') ->method('logException')
->with(new \Exception('foo')); ->with(new \Exception('foo'));
$expectedResponse = new Http\DataResponse(['data' => ['message' => 'An error occurred. Please contact your admin.']], Http::STATUS_BAD_REQUEST); $expectedResponse = new Http\JSONResponse(['data' => ['message' => 'An error occurred. Please contact your admin.']], Http::STATUS_BAD_REQUEST);
$this->assertEquals($expectedResponse, $this->avatarController->postCroppedAvatar(['x' => 0, 'y' => 0, 'w' => 10, 'h' => 11])); $this->assertEquals($expectedResponse, $this->avatarController->postCroppedAvatar(['x' => 0, 'y' => 0, 'w' => 10, 'h' => 11]));
} }