diff options
author | Daniel Kesselberg <mail@danielkesselberg.de> | 2019-01-26 15:28:31 +0100 |
---|---|---|
committer | Daniel Kesselberg <mail@danielkesselberg.de> | 2019-01-28 21:41:26 +0100 |
commit | 4dbef1210ccfecdafdb1d35e76bdc172c0117ba4 (patch) | |
tree | d48dba7cf3fe4df9c81b0ab40286550779b8ddb6 /apps | |
parent | 198a45ff75c64d38c1ccb6bd903ee55b446177b3 (diff) | |
download | nextcloud-server-4dbef1210ccfecdafdb1d35e76bdc172c0117ba4.tar.gz nextcloud-server-4dbef1210ccfecdafdb1d35e76bdc172c0117ba4.zip |
Add ico as avatar
Signed-off-by: Daniel Kesselberg <mail@danielkesselberg.de>
Diffstat (limited to 'apps')
-rw-r--r-- | apps/dav/lib/CardDAV/PhotoCache.php | 64 |
1 files changed, 32 insertions, 32 deletions
diff --git a/apps/dav/lib/CardDAV/PhotoCache.php b/apps/dav/lib/CardDAV/PhotoCache.php index fa244857e39..3a076c52ba3 100644 --- a/apps/dav/lib/CardDAV/PhotoCache.php +++ b/apps/dav/lib/CardDAV/PhotoCache.php @@ -35,6 +35,14 @@ use Sabre\VObject\Reader; class PhotoCache { + /** @var array */ + protected const ALLOWED_CONTENT_TYPES = [ + 'image/png' => 'png', + 'image/jpeg' => 'jpg', + 'image/gif' => 'gif', + 'application/octet-stream' => 'ico', + ]; + /** @var IAppData */ protected $appData; @@ -90,27 +98,26 @@ class PhotoCache { /** * @param ISimpleFolder $folder * @param Card $card + * @throws NotPermittedException */ - private function init(ISimpleFolder $folder, Card $card) { + private function init(ISimpleFolder $folder, Card $card): void { $data = $this->getPhoto($card); - if ($data === false) { + if ($data === false || !isset($data['Content-Type'])) { $folder->newFile('nophoto'); - } else { - switch ($data['Content-Type']) { - case 'image/png': - $ext = 'png'; - break; - case 'image/jpeg': - $ext = 'jpg'; - break; - case 'image/gif': - $ext = 'gif'; - break; - } - $file = $folder->newFile('photo.' . $ext); - $file->putContent($data['body']); + return; + } + + $contentType = $data['Content-Type']; + $extension = self::ALLOWED_CONTENT_TYPES[$contentType] ?? null; + + if ($extension === null) { + $folder->newFile('nophoto'); + return; } + + $file = $folder->newFile('photo.' . $extension); + $file->putContent($data['body']); } private function hasPhoto(ISimpleFolder $folder) { @@ -147,7 +154,7 @@ class PhotoCache { if ($size !== -1) { $photo->resize($size); } - + try { $file = $folder->newFile($path); $file->putContent($photo->data()); @@ -180,15 +187,14 @@ class PhotoCache { * @return string * @throws NotFoundException */ - private function getExtension(ISimpleFolder $folder) { - if ($folder->fileExists('photo.jpg')) { - return 'jpg'; - } elseif ($folder->fileExists('photo.png')) { - return 'png'; - } elseif ($folder->fileExists('photo.gif')) { - return 'gif'; + private function getExtension(ISimpleFolder $folder): string { + foreach (self::ALLOWED_CONTENT_TYPES as $extension) { + if ($folder->fileExists('photo.' . $extension)) { + return $extension; + } } - throw new NotFoundException; + + throw new NotFoundException('Avatar not found'); } private function getPhoto(Card $node) { @@ -218,13 +224,7 @@ class PhotoCache { $type = $this->getBinaryType($photo); } - $allowedContentTypes = [ - 'image/png', - 'image/jpeg', - 'image/gif', - ]; - - if (!in_array($type, $allowedContentTypes, true)) { + if (empty($type) || !isset(self::ALLOWED_CONTENT_TYPES[$type])) { $type = 'application/octet-stream'; } |