Merge pull request #13708 from nextcloud/stb14-vcard-avatar-fixes

[stable14] vcard avatar fixes
This commit is contained in:
Morris Jobke 2019-01-28 17:36:12 +01:00 committed by GitHub
commit c66f93ad58
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 36 additions and 17 deletions

View File

@ -84,7 +84,10 @@ if ($debugging) {
$server->addPlugin(new \Sabre\DAV\Sync\Plugin()); $server->addPlugin(new \Sabre\DAV\Sync\Plugin());
$server->addPlugin(new \Sabre\CardDAV\VCFExportPlugin()); $server->addPlugin(new \Sabre\CardDAV\VCFExportPlugin());
$server->addPlugin(new \OCA\DAV\CardDAV\ImageExportPlugin(new \OCA\DAV\CardDAV\PhotoCache(\OC::$server->getAppDataDir('dav-photocache')))); $server->addPlugin(new \OCA\DAV\CardDAV\ImageExportPlugin(new \OCA\DAV\CardDAV\PhotoCache(
\OC::$server->getAppDataDir('dav-photocache'),
\OC::$server->getLogger()
)));
$server->addPlugin(new ExceptionLoggerPlugin('carddav', \OC::$server->getLogger())); $server->addPlugin(new ExceptionLoggerPlugin('carddav', \OC::$server->getLogger()));
// And off we go! // And off we go!

View File

@ -54,7 +54,8 @@ class Application extends App {
$container->registerService(PhotoCache::class, function(SimpleContainer $s) use ($server) { $container->registerService(PhotoCache::class, function(SimpleContainer $s) use ($server) {
return new PhotoCache( return new PhotoCache(
$server->getAppDataDir('dav-photocache') $server->getAppDataDir('dav-photocache'),
$server->getLogger()
); );
}); });

View File

@ -24,6 +24,7 @@
namespace OCA\DAV\CardDAV; namespace OCA\DAV\CardDAV;
use OCP\Files\IAppData; use OCP\Files\IAppData;
use OCP\ILogger;
use OCP\Files\NotFoundException; use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException; use OCP\Files\NotPermittedException;
use OCP\Files\SimpleFS\ISimpleFile; use OCP\Files\SimpleFS\ISimpleFile;
@ -34,16 +35,21 @@ use Sabre\VObject\Reader;
class PhotoCache { class PhotoCache {
/** @var IAppData $appData */ /** @var IAppData */
protected $appData; protected $appData;
/** @var ILogger */
protected $logger;
/** /**
* PhotoCache constructor. * PhotoCache constructor.
* *
* @param IAppData $appData * @param IAppData $appData
* @param ILogger $logger
*/ */
public function __construct(IAppData $appData) { public function __construct(IAppData $appData, ILogger $logger) {
$this->appData = $appData; $this->appData = $appData;
$this->logger = $logger;
} }
/** /**
@ -134,13 +140,14 @@ class PhotoCache {
$ratio = $photo->width() / $photo->height(); $ratio = $photo->width() / $photo->height();
if ($ratio < 1) { if ($ratio < 1) {
$ratio = 1/$ratio; $ratio = 1 / $ratio;
} }
$size = (int)($size * $ratio);
$size = (int) ($size * $ratio);
if ($size !== -1) { if ($size !== -1) {
$photo->resize($size); $photo->resize($size);
} }
try { try {
$file = $folder->newFile($path); $file = $folder->newFile($path);
$file->putContent($photo->data()); $file->putContent($photo->data());
@ -152,7 +159,6 @@ class PhotoCache {
return $file; return $file;
} }
/** /**
* @param int $addressBookId * @param int $addressBookId
* @param string $cardUri * @param string $cardUri
@ -193,19 +199,23 @@ class PhotoCache {
} }
$photo = $vObject->PHOTO; $photo = $vObject->PHOTO;
$type = $this->getType($photo);
$val = $photo->getValue(); $val = $photo->getValue();
// handle data URI. e.g PHOTO;VALUE=URI:data:image/jpeg;base64,/9j/4AAQSkZJRgABAQE
if ($photo->getValueType() === 'URI') { if ($photo->getValueType() === 'URI') {
$parsed = \Sabre\URI\parse($val); $parsed = \Sabre\URI\parse($val);
//only allow data://
// only allow data://
if ($parsed['scheme'] !== 'data') { if ($parsed['scheme'] !== 'data') {
return false; return false;
} }
if (substr_count($parsed['path'], ';') === 1) { if (substr_count($parsed['path'], ';') === 1) {
list($type,) = explode(';', $parsed['path']); list($type) = explode(';', $parsed['path']);
} }
$val = file_get_contents($val); $val = file_get_contents($val);
} else {
// get type if binary data
$type = $this->getBinaryType($photo);
} }
$allowedContentTypes = [ $allowedContentTypes = [
@ -214,7 +224,7 @@ class PhotoCache {
'image/gif', 'image/gif',
]; ];
if(!in_array($type, $allowedContentTypes, true)) { if (!in_array($type, $allowedContentTypes, true)) {
$type = 'application/octet-stream'; $type = 'application/octet-stream';
} }
@ -222,8 +232,10 @@ class PhotoCache {
'Content-Type' => $type, 'Content-Type' => $type,
'body' => $val 'body' => $val
]; ];
} catch(\Exception $ex) { } catch (\Exception $e) {
$this->logger->logException($e, [
'message' => 'Exception during vcard photo parsing'
]);
} }
return false; return false;
} }
@ -240,7 +252,7 @@ class PhotoCache {
* @param Binary $photo * @param Binary $photo
* @return string * @return string
*/ */
private function getType(Binary $photo) { private function getBinaryType(Binary $photo) {
$params = $photo->parameters(); $params = $photo->parameters();
if (isset($params['TYPE']) || isset($params['MEDIATYPE'])) { if (isset($params['TYPE']) || isset($params['MEDIATYPE'])) {
/** @var Parameter $typeParam */ /** @var Parameter $typeParam */

View File

@ -162,7 +162,10 @@ class Server {
$this->server->addPlugin(new DAV\Sharing\Plugin($authBackend, \OC::$server->getRequest())); $this->server->addPlugin(new DAV\Sharing\Plugin($authBackend, \OC::$server->getRequest()));
$this->server->addPlugin(new \OCA\DAV\CardDAV\Plugin()); $this->server->addPlugin(new \OCA\DAV\CardDAV\Plugin());
$this->server->addPlugin(new VCFExportPlugin()); $this->server->addPlugin(new VCFExportPlugin());
$this->server->addPlugin(new ImageExportPlugin(new PhotoCache(\OC::$server->getAppDataDir('dav-photocache')))); $this->server->addPlugin(new ImageExportPlugin(new PhotoCache(
\OC::$server->getAppDataDir('dav-photocache'),
\OC::$server->getLogger())
));
} }
// system tags plugins // system tags plugins