MoveAvatarsBackgroundJob.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. /**
  3. * @copyright 2016 Roeland Jago Douma <roeland@famdouma.nl>
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Julius Härtl <jus@bitgrid.net>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OC\Repair\Owncloud;
  25. use OC\BackgroundJob\QueuedJob;
  26. use OCP\Files\IRootFolder;
  27. use OCP\Files\NotFoundException;
  28. use OCP\Files\Storage;
  29. use OCP\IAvatarManager;
  30. use OCP\IUser;
  31. use OCP\IUserManager;
  32. use Psr\Log\LoggerInterface;
  33. use function is_resource;
  34. class MoveAvatarsBackgroundJob extends QueuedJob {
  35. /** @var IUserManager */
  36. private $userManager;
  37. /** @var LoggerInterface */
  38. private $logger;
  39. /** @var IAvatarManager */
  40. private $avatarManager;
  41. /** @var Storage */
  42. private $owncloudAvatarStorage;
  43. public function __construct(IUserManager $userManager, LoggerInterface $logger, IAvatarManager $avatarManager, IRootFolder $rootFolder) {
  44. $this->userManager = $userManager;
  45. $this->logger = $logger;
  46. $this->avatarManager = $avatarManager;
  47. try {
  48. $this->owncloudAvatarStorage = $rootFolder->get('avatars')->getStorage();
  49. } catch (\Exception $e) {
  50. }
  51. }
  52. public function run($arguments) {
  53. $this->logger->info('Started migrating avatars to AppData folder');
  54. $this->moveAvatars();
  55. $this->logger->info('All avatars migrated to AppData folder');
  56. }
  57. private function moveAvatars(): void {
  58. if (!$this->owncloudAvatarStorage) {
  59. $this->logger->info('No legacy avatars available, skipping migration');
  60. return;
  61. }
  62. $counter = 0;
  63. $this->userManager->callForSeenUsers(function (IUser $user) use ($counter) {
  64. $uid = $user->getUID();
  65. $path = 'avatars/' . $this->buildOwnCloudAvatarPath($uid);
  66. $avatar = $this->avatarManager->getAvatar($uid);
  67. try {
  68. $avatarPath = $path . '/avatar.' . $this->getExtension($path);
  69. $resource = $this->owncloudAvatarStorage->fopen($avatarPath, 'r');
  70. if (is_resource($resource)) {
  71. $avatar->set($resource);
  72. fclose($resource);
  73. } else {
  74. throw new \Exception('Failed to open old avatar file for reading');
  75. }
  76. } catch (NotFoundException $e) {
  77. // In case there is no avatar we can just skip
  78. } catch (\Throwable $e) {
  79. $this->logger->error('Failed to migrate avatar for user ' . $uid, ['exception' => $e]);
  80. }
  81. $counter++;
  82. if ($counter % 100 === 0) {
  83. $this->logger->info('{amount} avatars migrated', ['amount' => $counter]);
  84. }
  85. });
  86. }
  87. /**
  88. * @throws NotFoundException
  89. */
  90. private function getExtension(string $path): string {
  91. if ($this->owncloudAvatarStorage->file_exists("{$path}/avatar.jpg")) {
  92. return 'jpg';
  93. }
  94. if ($this->owncloudAvatarStorage->file_exists("{$path}/avatar.png")) {
  95. return 'png';
  96. }
  97. throw new NotFoundException("{$path}/avatar.jpg|png");
  98. }
  99. protected function buildOwnCloudAvatarPath(string $userId): string {
  100. return substr_replace(substr_replace(md5($userId), '/', 4, 0), '/', 2, 0);
  101. }
  102. }