Merge pull request #13843 from nextcloud/bugfix/11852-add-icon-caldav
Add ico as avatar
This commit is contained in:
commit
d9b2b3e3c8
|
@ -35,6 +35,14 @@ use Sabre\VObject\Reader;
|
||||||
|
|
||||||
class PhotoCache {
|
class PhotoCache {
|
||||||
|
|
||||||
|
/** @var array */
|
||||||
|
protected const ALLOWED_CONTENT_TYPES = [
|
||||||
|
'image/png' => 'png',
|
||||||
|
'image/jpeg' => 'jpg',
|
||||||
|
'image/gif' => 'gif',
|
||||||
|
'image/vnd.microsoft.icon' => 'ico',
|
||||||
|
];
|
||||||
|
|
||||||
/** @var IAppData */
|
/** @var IAppData */
|
||||||
protected $appData;
|
protected $appData;
|
||||||
|
|
||||||
|
@ -90,27 +98,26 @@ class PhotoCache {
|
||||||
/**
|
/**
|
||||||
* @param ISimpleFolder $folder
|
* @param ISimpleFolder $folder
|
||||||
* @param Card $card
|
* @param Card $card
|
||||||
|
* @throws NotPermittedException
|
||||||
*/
|
*/
|
||||||
private function init(ISimpleFolder $folder, Card $card) {
|
private function init(ISimpleFolder $folder, Card $card): void {
|
||||||
$data = $this->getPhoto($card);
|
$data = $this->getPhoto($card);
|
||||||
|
|
||||||
if ($data === false) {
|
if ($data === false || !isset($data['Content-Type'])) {
|
||||||
$folder->newFile('nophoto');
|
$folder->newFile('nophoto');
|
||||||
} else {
|
return;
|
||||||
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']);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$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) {
|
private function hasPhoto(ISimpleFolder $folder) {
|
||||||
|
@ -147,7 +154,7 @@ class PhotoCache {
|
||||||
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());
|
||||||
|
@ -180,15 +187,14 @@ class PhotoCache {
|
||||||
* @return string
|
* @return string
|
||||||
* @throws NotFoundException
|
* @throws NotFoundException
|
||||||
*/
|
*/
|
||||||
private function getExtension(ISimpleFolder $folder) {
|
private function getExtension(ISimpleFolder $folder): string {
|
||||||
if ($folder->fileExists('photo.jpg')) {
|
foreach (self::ALLOWED_CONTENT_TYPES as $extension) {
|
||||||
return 'jpg';
|
if ($folder->fileExists('photo.' . $extension)) {
|
||||||
} elseif ($folder->fileExists('photo.png')) {
|
return $extension;
|
||||||
return 'png';
|
}
|
||||||
} elseif ($folder->fileExists('photo.gif')) {
|
|
||||||
return 'gif';
|
|
||||||
}
|
}
|
||||||
throw new NotFoundException;
|
|
||||||
|
throw new NotFoundException('Avatar not found');
|
||||||
}
|
}
|
||||||
|
|
||||||
private function getPhoto(Card $node) {
|
private function getPhoto(Card $node) {
|
||||||
|
@ -218,13 +224,7 @@ class PhotoCache {
|
||||||
$type = $this->getBinaryType($photo);
|
$type = $this->getBinaryType($photo);
|
||||||
}
|
}
|
||||||
|
|
||||||
$allowedContentTypes = [
|
if (empty($type) || !isset(self::ALLOWED_CONTENT_TYPES[$type])) {
|
||||||
'image/png',
|
|
||||||
'image/jpeg',
|
|
||||||
'image/gif',
|
|
||||||
];
|
|
||||||
|
|
||||||
if (!in_array($type, $allowedContentTypes, true)) {
|
|
||||||
$type = 'application/octet-stream';
|
$type = 'application/octet-stream';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue