From 4e6f6518ff9aec0b838ce0c43a9d3f880b4464ad Mon Sep 17 00:00:00 2001 From: Roeland Jago Douma Date: Thu, 7 Jan 2016 20:51:18 +0100 Subject: [PATCH] Remove all cache avatars on avatar deletion Fixes #21513 Since we cache the generated avatars. We should also delete the generated sizes when we remove the avatar. --- lib/private/avatar.php | 14 ++++++++------ tests/lib/avatartest.php | 30 +++++++++++++++++++++++------- 2 files changed, 31 insertions(+), 13 deletions(-) diff --git a/lib/private/avatar.php b/lib/private/avatar.php index c87facd25d..966e490364 100644 --- a/lib/private/avatar.php +++ b/lib/private/avatar.php @@ -124,12 +124,14 @@ class Avatar implements \OCP\IAvatar { * @return void */ public function remove () { - try { - $this->folder->get('avatar.jpg')->delete(); - } catch (\OCP\Files\NotFoundException $e) {} - try { - $this->folder->get('avatar.png')->delete(); - } catch (\OCP\Files\NotFoundException $e) {} + $regex = '/^avatar\.([0-9]+\.)?(jpg|png)$/'; + $avatars = $this->folder->search('avatar'); + + foreach ($avatars as $avatar) { + if (preg_match($regex, $avatar->getName())) { + $avatar->delete(); + } + } } /** diff --git a/tests/lib/avatartest.php b/tests/lib/avatartest.php index 3d77a282a7..d3e615977c 100644 --- a/tests/lib/avatartest.php +++ b/tests/lib/avatartest.php @@ -110,13 +110,29 @@ class AvatarTest extends \Test\TestCase { } public function testSetAvatar() { - $oldFile = $this->getMock('\OCP\Files\File'); - $this->folder->method('get') - ->will($this->returnValueMap([ - ['avatar.jpg', $oldFile], - ['avatar.png', $oldFile], - ])); - $oldFile->expects($this->exactly(2))->method('delete'); + $avatarFileJPG = $this->getMock('\OCP\Files\File'); + $avatarFileJPG->method('getName') + ->willReturn('avatar.jpg'); + $avatarFileJPG->expects($this->once())->method('delete'); + + $avatarFilePNG = $this->getMock('\OCP\Files\File'); + $avatarFilePNG->method('getName') + ->willReturn('avatar.png'); + $avatarFilePNG->expects($this->once())->method('delete'); + + $resizedAvatarFile = $this->getMock('\OCP\Files\File'); + $resizedAvatarFile->method('getName') + ->willReturn('avatar.32.jpg'); + $resizedAvatarFile->expects($this->once())->method('delete'); + + $nonAvatarFile = $this->getMock('\OCP\Files\File'); + $nonAvatarFile->method('getName') + ->willReturn('avatarX'); + $nonAvatarFile->expects($this->never())->method('delete'); + + $this->folder->method('search') + ->with('avatar') + ->willReturn([$avatarFileJPG, $avatarFilePNG, $resizedAvatarFile, $nonAvatarFile]); $newFile = $this->getMock('\OCP\Files\File'); $this->folder->expects($this->once())