ClearGeneratedAvatarCache.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OC\Repair;
  7. use OC\Avatar\AvatarManager;
  8. use OCP\BackgroundJob\IJobList;
  9. use OCP\IConfig;
  10. use OCP\Migration\IOutput;
  11. use OCP\Migration\IRepairStep;
  12. class ClearGeneratedAvatarCache implements IRepairStep {
  13. protected AvatarManager $avatarManager;
  14. private IConfig $config;
  15. private IJobList $jobList;
  16. public function __construct(IConfig $config, AvatarManager $avatarManager, IJobList $jobList) {
  17. $this->config = $config;
  18. $this->avatarManager = $avatarManager;
  19. $this->jobList = $jobList;
  20. }
  21. public function getName(): string {
  22. return 'Clear every generated avatar';
  23. }
  24. /**
  25. * Check if this repair step should run
  26. */
  27. private function shouldRun(): bool {
  28. $versionFromBeforeUpdate = $this->config->getSystemValueString('version', '0.0.0.0');
  29. // This job only runs if the server was on a version lower than or equal to 27.0.0 before the upgrade.
  30. // To clear the avatar cache again, bump the version to the currently released version (and change the operator to <= if it's not the master branch) and wait for the next release.
  31. return version_compare($versionFromBeforeUpdate, '27.0.0', '<');
  32. }
  33. public function run(IOutput $output): void {
  34. if ($this->shouldRun()) {
  35. try {
  36. $this->jobList->add(ClearGeneratedAvatarCacheJob::class, []);
  37. $output->info('Avatar cache clearing job added');
  38. } catch (\Exception $e) {
  39. $output->warning('Unable to clear the avatar cache');
  40. }
  41. }
  42. }
  43. }