MoveAvatarsBackgroundJob.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2016 Roeland Jago Douma <roeland@famdouma.nl>
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Julius Härtl <jus@bitgrid.net>
  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\Owncloud;
  26. use OCP\AppFramework\Utility\ITimeFactory;
  27. use OCP\BackgroundJob\QueuedJob;
  28. use OCP\Files\IRootFolder;
  29. use OCP\Files\NotFoundException;
  30. use OCP\Files\Storage\IStorage;
  31. use OCP\IAvatarManager;
  32. use OCP\IUser;
  33. use OCP\IUserManager;
  34. use Psr\Log\LoggerInterface;
  35. use function is_resource;
  36. class MoveAvatarsBackgroundJob extends QueuedJob {
  37. private ?IStorage $owncloudAvatarStorage = null;
  38. public function __construct(
  39. private IUserManager $userManager,
  40. private LoggerInterface $logger,
  41. private IAvatarManager $avatarManager,
  42. private IRootFolder $rootFolder,
  43. ITimeFactory $time,
  44. ) {
  45. parent::__construct($time);
  46. try {
  47. $this->owncloudAvatarStorage = $rootFolder->get('avatars')->getStorage();
  48. } catch (\Exception $e) {
  49. }
  50. }
  51. public function run($arguments) {
  52. $this->logger->info('Started migrating avatars to AppData folder');
  53. $this->moveAvatars();
  54. $this->logger->info('All avatars migrated to AppData folder');
  55. }
  56. private function moveAvatars(): void {
  57. if (!$this->owncloudAvatarStorage) {
  58. $this->logger->info('No legacy avatars available, skipping migration');
  59. return;
  60. }
  61. $counter = 0;
  62. $this->userManager->callForSeenUsers(function (IUser $user) use (&$counter) {
  63. $uid = $user->getUID();
  64. $path = 'avatars/' . $this->buildOwnCloudAvatarPath($uid);
  65. $avatar = $this->avatarManager->getAvatar($uid);
  66. try {
  67. $avatarPath = $path . '/avatar.' . $this->getExtension($path);
  68. $resource = $this->owncloudAvatarStorage->fopen($avatarPath, 'r');
  69. if (is_resource($resource)) {
  70. $avatar->set($resource);
  71. fclose($resource);
  72. } else {
  73. throw new \Exception('Failed to open old avatar file for reading');
  74. }
  75. } catch (NotFoundException $e) {
  76. // In case there is no avatar we can just skip
  77. } catch (\Throwable $e) {
  78. $this->logger->error('Failed to migrate avatar for user ' . $uid, ['exception' => $e]);
  79. }
  80. $counter++;
  81. if ($counter % 100 === 0) {
  82. $this->logger->info('{amount} avatars migrated', ['amount' => $counter]);
  83. }
  84. });
  85. }
  86. /**
  87. * @throws NotFoundException
  88. */
  89. private function getExtension(string $path): string {
  90. if ($this->owncloudAvatarStorage->file_exists("{$path}/avatar.jpg")) {
  91. return 'jpg';
  92. }
  93. if ($this->owncloudAvatarStorage->file_exists("{$path}/avatar.png")) {
  94. return 'png';
  95. }
  96. throw new NotFoundException("{$path}/avatar.jpg|png");
  97. }
  98. protected function buildOwnCloudAvatarPath(string $userId): string {
  99. return substr_replace(substr_replace(md5($userId), '/', 4, 0), '/', 2, 0);
  100. }
  101. }