MoveAvatarsBackgroundJob.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\Repair\Owncloud;
  8. use OCP\AppFramework\Utility\ITimeFactory;
  9. use OCP\BackgroundJob\QueuedJob;
  10. use OCP\Files\IRootFolder;
  11. use OCP\Files\NotFoundException;
  12. use OCP\Files\Storage\IStorage;
  13. use OCP\IAvatarManager;
  14. use OCP\IUser;
  15. use OCP\IUserManager;
  16. use Psr\Log\LoggerInterface;
  17. use function is_resource;
  18. class MoveAvatarsBackgroundJob extends QueuedJob {
  19. private ?IStorage $owncloudAvatarStorage = null;
  20. public function __construct(
  21. private IUserManager $userManager,
  22. private LoggerInterface $logger,
  23. private IAvatarManager $avatarManager,
  24. private IRootFolder $rootFolder,
  25. ITimeFactory $time,
  26. ) {
  27. parent::__construct($time);
  28. try {
  29. $this->owncloudAvatarStorage = $rootFolder->get('avatars')->getStorage();
  30. } catch (\Exception $e) {
  31. }
  32. }
  33. public function run($arguments) {
  34. $this->logger->info('Started migrating avatars to AppData folder');
  35. $this->moveAvatars();
  36. $this->logger->info('All avatars migrated to AppData folder');
  37. }
  38. private function moveAvatars(): void {
  39. if (!$this->owncloudAvatarStorage) {
  40. $this->logger->info('No legacy avatars available, skipping migration');
  41. return;
  42. }
  43. $counter = 0;
  44. $this->userManager->callForSeenUsers(function (IUser $user) use (&$counter) {
  45. $uid = $user->getUID();
  46. $path = 'avatars/' . $this->buildOwnCloudAvatarPath($uid);
  47. $avatar = $this->avatarManager->getAvatar($uid);
  48. try {
  49. $avatarPath = $path . '/avatar.' . $this->getExtension($path);
  50. $resource = $this->owncloudAvatarStorage->fopen($avatarPath, 'r');
  51. if (is_resource($resource)) {
  52. $avatar->set($resource);
  53. fclose($resource);
  54. } else {
  55. throw new \Exception('Failed to open old avatar file for reading');
  56. }
  57. } catch (NotFoundException $e) {
  58. // In case there is no avatar we can just skip
  59. } catch (\Throwable $e) {
  60. $this->logger->error('Failed to migrate avatar for user ' . $uid, ['exception' => $e]);
  61. }
  62. $counter++;
  63. if ($counter % 100 === 0) {
  64. $this->logger->info('{amount} avatars migrated', ['amount' => $counter]);
  65. }
  66. });
  67. }
  68. /**
  69. * @throws NotFoundException
  70. */
  71. private function getExtension(string $path): string {
  72. if ($this->owncloudAvatarStorage->file_exists("{$path}/avatar.jpg")) {
  73. return 'jpg';
  74. }
  75. if ($this->owncloudAvatarStorage->file_exists("{$path}/avatar.png")) {
  76. return 'png';
  77. }
  78. throw new NotFoundException("{$path}/avatar.jpg|png");
  79. }
  80. protected function buildOwnCloudAvatarPath(string $userId): string {
  81. return substr_replace(substr_replace(md5($userId), '/', 4, 0), '/', 2, 0);
  82. }
  83. }