1
0

MoveAvatarsBackgroundJob.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. /**
  3. * @copyright 2016 Roeland Jago Douma <roeland@famdouma.nl>
  4. *
  5. * @author Roeland Jago Douma <roeland@famdouma.nl>
  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\NC11;
  24. use OC\BackgroundJob\QueuedJob;
  25. use OCP\Files\File;
  26. use OCP\Files\Folder;
  27. use OCP\Files\IAppData;
  28. use OCP\Files\IRootFolder;
  29. use OCP\Files\NotFoundException;
  30. use OCP\ILogger;
  31. use OCP\IUser;
  32. use OCP\IUserManager;
  33. class MoveAvatarsBackgroundJob extends QueuedJob {
  34. /** @var IUserManager */
  35. private $userManager;
  36. /** @var IRootFolder */
  37. private $rootFolder;
  38. /** @var IAppData */
  39. private $appData;
  40. /** @var ILogger */
  41. private $logger;
  42. /**
  43. * MoveAvatars constructor.
  44. */
  45. public function __construct() {
  46. $this->userManager = \OC::$server->getUserManager();
  47. $this->rootFolder = \OC::$server->getRootFolder();
  48. $this->logger = \OC::$server->getLogger();
  49. $this->appData = \OC::$server->getAppDataDir('avatar');
  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() {
  57. $counter = 0;
  58. $this->userManager->callForSeenUsers(function (IUser $user) use ($counter) {
  59. $uid = $user->getUID();
  60. \OC\Files\Filesystem::initMountPoints($uid);
  61. /** @var Folder $userFolder */
  62. $userFolder = $this->rootFolder->get($uid);
  63. try {
  64. $userData = $this->appData->getFolder($uid);
  65. } catch (NotFoundException $e) {
  66. $userData = $this->appData->newFolder($uid);
  67. }
  68. $regex = '/^avatar\.([0-9]+\.)?(jpg|png)$/';
  69. $avatars = $userFolder->getDirectoryListing();
  70. foreach ($avatars as $avatar) {
  71. /** @var File $avatar */
  72. if (preg_match($regex, $avatar->getName())) {
  73. /*
  74. * This is not the most effective but it is the most abstract way
  75. * to handle this. Avatars should be small anyways.
  76. */
  77. $newAvatar = $userData->newFile($avatar->getName());
  78. $newAvatar->putContent($avatar->getContent());
  79. $avatar->delete();
  80. }
  81. }
  82. $counter++;
  83. if ($counter % 100) {
  84. $this->logger->info('{amount} avatars migrated', ['amount' => $counter]);
  85. }
  86. });
  87. }
  88. }