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.
This commit is contained in:
Roeland Jago Douma 2016-01-07 20:51:18 +01:00
parent 9ca670f94f
commit 4e6f6518ff
2 changed files with 31 additions and 13 deletions

View File

@ -124,12 +124,14 @@ class Avatar implements \OCP\IAvatar {
* @return void * @return void
*/ */
public function remove () { public function remove () {
try { $regex = '/^avatar\.([0-9]+\.)?(jpg|png)$/';
$this->folder->get('avatar.jpg')->delete(); $avatars = $this->folder->search('avatar');
} catch (\OCP\Files\NotFoundException $e) {}
try { foreach ($avatars as $avatar) {
$this->folder->get('avatar.png')->delete(); if (preg_match($regex, $avatar->getName())) {
} catch (\OCP\Files\NotFoundException $e) {} $avatar->delete();
}
}
} }
/** /**

View File

@ -110,13 +110,29 @@ class AvatarTest extends \Test\TestCase {
} }
public function testSetAvatar() { public function testSetAvatar() {
$oldFile = $this->getMock('\OCP\Files\File'); $avatarFileJPG = $this->getMock('\OCP\Files\File');
$this->folder->method('get') $avatarFileJPG->method('getName')
->will($this->returnValueMap([ ->willReturn('avatar.jpg');
['avatar.jpg', $oldFile], $avatarFileJPG->expects($this->once())->method('delete');
['avatar.png', $oldFile],
])); $avatarFilePNG = $this->getMock('\OCP\Files\File');
$oldFile->expects($this->exactly(2))->method('delete'); $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'); $newFile = $this->getMock('\OCP\Files\File');
$this->folder->expects($this->once()) $this->folder->expects($this->once())