You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MoveAvatarsBackgroundJob.php 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. /**
  3. * @copyright 2016 Roeland Jago Douma <roeland@famdouma.nl>
  4. *
  5. * @author Julius Härtl <jus@bitgrid.net>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace OC\Repair\Owncloud;
  24. use OC\BackgroundJob\QueuedJob;
  25. use OCP\Files\IRootFolder;
  26. use OCP\Files\NotFoundException;
  27. use OCP\Files\Storage;
  28. use OCP\IAvatarManager;
  29. use OCP\IUser;
  30. use OCP\IUserManager;
  31. use Psr\Log\LoggerInterface;
  32. use function is_resource;
  33. class MoveAvatarsBackgroundJob extends QueuedJob {
  34. /** @var IUserManager */
  35. private $userManager;
  36. /** @var LoggerInterface */
  37. private $logger;
  38. /** @var IAvatarManager */
  39. private $avatarManager;
  40. /** @var Storage */
  41. private $owncloudAvatarStorage;
  42. public function __construct(IUserManager $userManager, LoggerInterface $logger, IAvatarManager $avatarManager, IRootFolder $rootFolder) {
  43. $this->userManager = $userManager;
  44. $this->logger = $logger;
  45. $this->avatarManager = $avatarManager;
  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. }