Skip to content

Commit

Permalink
Resize maximum dimensions for pictures coming from SocialApiService
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Citharel <[email protected]>
  • Loading branch information
tcitworld committed May 16, 2022
1 parent 938bf0c commit 4ef3551
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 7 deletions.
48 changes: 48 additions & 0 deletions lib/Service/ImageResizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
/**
* @copyright Copyright (c) 2022 Thomas Citharel <[email protected]>
*
* @author Thomas Citharel <[email protected]>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace OCA\Contacts\Service;


use OCP\Image;

class ImageResizer {
public const RESIZE_MAX_X = 256;
public const RESIZE_MAX_Y = 256;

/**
* @param string $socialData
* @return null|string
*/
public function resizeImage(string $socialData) {
$image = new Image();

$image->loadFromData($socialData);

if ($image->valid()) {
$image->scaleDownToFit(self::RESIZE_MAX_X, self::RESIZE_MAX_Y);
return $image->data();
}
return null;
}
}
18 changes: 16 additions & 2 deletions lib/Service/SocialApiService.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
use OCP\IAddressBook;
use OCP\IConfig;
use OCP\IL10N;
use OCP\Image;
use OCP\IURLGenerator;

class SocialApiService {
Expand All @@ -57,7 +58,8 @@ class SocialApiService {
private $davBackend;
/** @var ITimeFactory */
private $timeFactory;

/** @var ImageResizer */
private $imageResizer;

public function __construct(
CompositeSocialProvider $socialProvider,
Expand All @@ -67,7 +69,8 @@ public function __construct(
IL10N $l10n,
IURLGenerator $urlGen,
CardDavBackend $davBackend,
ITimeFactory $timeFactory) {
ITimeFactory $timeFactory,
ImageResizer $imageResizer) {
$this->appName = Application::APP_ID;
$this->socialProvider = $socialProvider;
$this->manager = $manager;
Expand All @@ -77,6 +80,7 @@ public function __construct(
$this->urlGen = $urlGen;
$this->davBackend = $davBackend;
$this->timeFactory = $timeFactory;
$this->imageResizer = $imageResizer;
}


Expand Down Expand Up @@ -224,6 +228,16 @@ public function updateContact(string $addressbookId, string $contactId, ?string
return new JSONResponse([], Http::STATUS_NOT_FOUND);
}

if (is_resource($socialdata)) {
$socialdata = stream_get_contents($socialdata);
}

$socialdata = $this->imageResizer->resizeImage($socialdata);

if (!$socialdata) {
return new JSONResponse([], Http::STATUS_INTERNAL_SERVER_ERROR);
}

// update contact
$changes = [];
$changes['URI'] = $contact['URI'];
Expand Down
41 changes: 41 additions & 0 deletions tests/unit/Service/ImageResizerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
/**
* @copyright Copyright (c) 2022 Thomas Citharel <[email protected]>
*
* @author Thomas Citharel <[email protected]>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/


namespace OCA\Contacts\Service;

use ChristophWurst\Nextcloud\Testing\TestCase;

class ImageResizerTest extends TestCase {

/** @var ImageResizer */
private $imageResizer;

protected function setUp(): void {
$this->imageResizer = new ImageResizer();
}

public function testReturnsNullForInvalidImage() {
$this->assertNull($this->imageResizer->resizeImage('badImage'));
}
}
29 changes: 24 additions & 5 deletions tests/unit/Service/SocialApiServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ class SocialApiServiceTest extends TestCase {
private $davBackend;
/** @var ITimeFactory|MockObject */
private $timeFactory;
/** @var ImageResizer|MockObject */
private $imageResizer;

public function allSocialProfileProviders(): array {
$body = "the body";
Expand Down Expand Up @@ -127,6 +129,7 @@ protected function setUp(): void {
$this->urlGen = $this->createMock(IURLGenerator::class);
$this->davBackend = $this->createMock(CardDavBackend::class);
$this->timeFactory = $this->createMock(ITimeFactory::class);
$this->imageResizer = $this->createMock(ImageResizer::class);
$this->service = new SocialApiService(
$this->socialProvider,
$this->manager,
Expand All @@ -135,7 +138,8 @@ protected function setUp(): void {
$this->l10n,
$this->urlGen,
$this->davBackend,
$this->timeFactory
$this->timeFactory,
$this->imageResizer
);
}

Expand Down Expand Up @@ -174,12 +178,16 @@ public function testUpdateContactWithoutNetwork($addressbooks, $providers, $body
$this->clientService
->method('NewClient')
->willReturn($client);
$this->imageResizer
->expects($body ? $this->once() : $this->never())
->method('resizeImage')
->willReturn($body);

$result = $this->service
->updateContact(
'contacts',
'3225c0d5-1bd2-43e5-a08c-4e65eaa406b0',
null);
->updateContact(
'contacts',
'3225c0d5-1bd2-43e5-a08c-4e65eaa406b0',
null);
$this->assertEquals($status, $result->getStatus());
}

Expand Down Expand Up @@ -231,6 +239,10 @@ public function testUpdateContactWithNetworkVersion3() {
$this->clientService
->method('NewClient')
->willReturn($client);
$this->imageResizer
->expects($this->once())
->method('resizeImage')
->willReturn($body);

$changes = [
'URI' => $contact['URI'],
Expand Down Expand Up @@ -300,6 +312,10 @@ public function testUpdateContactWithNetworkVersion4() {
$this->clientService
->method('NewClient')
->willReturn($client);
$this->imageResizer
->expects($this->once())
->method('resizeImage')
->willReturn($body);

$changes = [
'URI' => $contact['URI'],
Expand Down Expand Up @@ -424,6 +440,9 @@ protected function setupAddressbooks() {
$this->clientService
->method('NewClient')
->willReturn($client);
$this->imageResizer
->method('resizeImage')
->willReturn('someBody');
}

/**
Expand Down

0 comments on commit 4ef3551

Please sign in to comment.