ClearGeneratedAvatarCache.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2018 John Molakvoæ <skjnldsv@protonmail.com>
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author John Molakvoæ <skjnldsv@protonmail.com>
  7. * @author Michael Weimann <mail@michael-weimann.eu>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OC\Repair;
  26. use OC\Avatar\AvatarManager;
  27. use OCP\IConfig;
  28. use OCP\Migration\IOutput;
  29. use OCP\Migration\IRepairStep;
  30. class ClearGeneratedAvatarCache implements IRepairStep {
  31. /** @var AvatarManager */
  32. protected $avatarManager;
  33. /** @var IConfig */
  34. private $config;
  35. public function __construct(IConfig $config, AvatarManager $avatarManager) {
  36. $this->config = $config;
  37. $this->avatarManager = $avatarManager;
  38. }
  39. public function getName() {
  40. return 'Clear every generated avatar on major updates';
  41. }
  42. /**
  43. * Check if this repair step should run
  44. *
  45. * @return boolean
  46. */
  47. private function shouldRun() {
  48. $versionFromBeforeUpdate = $this->config->getSystemValue('version', '0.0.0.0');
  49. // was added to 15.0.0.4
  50. return version_compare($versionFromBeforeUpdate, '15.0.0.4', '<=');
  51. }
  52. public function run(IOutput $output) {
  53. if ($this->shouldRun()) {
  54. try {
  55. $this->avatarManager->clearCachedAvatars();
  56. $output->info('Avatar cache cleared');
  57. } catch (\Exception $e) {
  58. $output->warning('Unable to clear the avatar cache');
  59. }
  60. }
  61. }
  62. }