Skip to content

Commit

Permalink
ContactCards: replace raw photo with link
Browse files Browse the repository at this point in the history
  • Loading branch information
georgehrke committed Jun 13, 2016
1 parent 5de19d9 commit e6d7a08
Show file tree
Hide file tree
Showing 4 changed files with 161 additions and 1 deletion.
26 changes: 26 additions & 0 deletions apps/dav/appinfo/routes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
/**
* @author Georg Ehrke <georg@ownCloud.com>
*
* @copyright Copyright (c) 2016, ownCloud, Inc.
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* 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, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/

return [
'routes' => [
['name' => 'photos#get', 'url' => '/contactsphoto/{addressbookId}/{contactUri}', 'verb' => 'GET'],
]
];
10 changes: 10 additions & 0 deletions apps/dav/lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use OCA\DAV\CardDAV\SyncJob;
use OCA\DAV\CardDAV\SyncService;
use OCA\DAV\Connector\Sabre\Principal;
use OCA\DAV\Controller\PhotosController;
use OCA\DAV\DAV\GroupPrincipalBackend;
use OCA\DAV\HookManager;
use OCA\DAV\Migration\Classification;
Expand Down Expand Up @@ -116,6 +117,15 @@ public function __construct (array $urlParams=array()) {
$c->getServer()->getUserManager()
);
});

$container->registerService('PhotosController', function(IAppContainer $c) {
$request = $c->query('Request');
$userSession = $c->getServer()->getUserSession();
$logger = $c->getServer()->getLogger();

return new PhotosController($c->getAppName(), $request,
$userSession, $c->query('CardDavBackend'), $logger);
});
}

/**
Expand Down
12 changes: 11 additions & 1 deletion apps/dav/lib/CardDAV/AddressBookImpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,17 @@ protected function vCard2Array($uri, VCard $vCard) {
];

foreach ($vCard->children as $property) {
$result[$property->name] = $property->getValue();
if ($property->name === 'PHOTO') {
$addressBookId = $this->getKey();

$url = \OC::$server->getURLGenerator()->linkToRouteAbsolute('dav.photos.get', [
'addressbookId' => $addressBookId,
'contactUri' => $uri,
]);
$result['PHOTO'] = 'VALUE=uri:' . $url;
} else {
$result[$property->name] = $property->getValue();
}
}
if ($this->addressBookInfo['principaluri'] === 'principals/system/system' &&
$this->addressBookInfo['uri'] === 'system') {
Expand Down
114 changes: 114 additions & 0 deletions apps/dav/lib/Controller/PhotosController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<?php
/**
* @author Georg Ehrke <georg@ownCloud.com>
*
* @copyright Copyright (c) 2016, ownCloud, Inc.
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* 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, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/

namespace OCA\DAV\Controller;

use OCA\DAV\CardDAV\CardDavBackend;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\NotFoundResponse;
use OCP\ILogger;
use OCP\IRequest;
use OCP\IUserSession;
use Sabre\VObject\Reader;

class PhotosController extends Controller {

/**
* @var CardDavBackend
*/
protected $backend;

/**
* @var IUserSession
*/
protected $userSession;

/**
* @var ILogger
*/
protected $logger;

/**
* @param string $appName
* @param IRequest $request an instance of the request
* @param IUserSession $userSession
* @param CardDavBackend $backend
* @param ILogger $logger
*/
public function __construct($appName, IRequest $request,
IUserSession $userSession,
CardDavBackend $backend,
ILogger $logger) {
parent::__construct($appName, $request);
$this->backend = $backend;
$this->userSession = $userSession;
$this->logger = $logger;
}

/**
* @NoAdminRequired
* @NoCSRFRequired
*
* @param integer $addressbookId
* @param string $contactUri
* @return \OCP\AppFramework\Http\Response|null
*/
public function get($addressbookId, $contactUri) {
$user = $this->userSession->getUser();
if (!$user) {
return new NotFoundResponse();
}

if (!$this->doesAddressBookBelongToUser($addressbookId, $user->getUID())) {
return new NotFoundResponse();
}

$card = $this->backend->getCard($addressbookId, $contactUri);
if (!$card || !isset($card['carddata'])) {
return new NotFoundResponse();
}

$vobject = $this->readCard($card['carddata']);
if (!$vobject->PHOTO) {
return new NotFoundResponse();
}

$photo = $vobject->PHOTO;
try {
$image = new \OC_Image();
$image->load($photo->getValue());
} catch(\Exception $ex) {
$this->logger->debug($ex->getMessage());
return new NotFoundResponse();
}

$image->show();
}

private function doesAddressBookBelongToUser($addressbookId, $userId) {
$addressbook = $this->backend->getAddressBookById($addressbookId);
return $addressbook['principaluri'] === 'principals/users/' . $userId;
}

private function readCard($cardData) {
return Reader::read($cardData);
}
}

0 comments on commit e6d7a08

Please sign in to comment.