diff options
author | Joas Schilling <coding@schilljs.com> | 2017-05-17 13:02:10 +0200 |
---|---|---|
committer | Joas Schilling <coding@schilljs.com> | 2017-05-18 10:49:04 +0200 |
commit | 682a57d50e8ce3ef2307fe50eba9f91ff6537cc4 (patch) | |
tree | 841b9c2cad8e4dcc02a7d207f39f18804107848a /lib/private/Repair | |
parent | c90d56748f92f0636c987721cea3b2308b983950 (diff) | |
download | nextcloud-server-682a57d50e8ce3ef2307fe50eba9f91ff6537cc4.tar.gz nextcloud-server-682a57d50e8ce3ef2307fe50eba9f91ff6537cc4.zip |
Copy avatars to the new location
Signed-off-by: Joas Schilling <coding@schilljs.com>
Diffstat (limited to 'lib/private/Repair')
-rw-r--r-- | lib/private/Repair/NC11/MoveAvatarsBackgroundJob.php | 70 |
1 files changed, 56 insertions, 14 deletions
diff --git a/lib/private/Repair/NC11/MoveAvatarsBackgroundJob.php b/lib/private/Repair/NC11/MoveAvatarsBackgroundJob.php index 9808b3b9e34..d46b6fec8fc 100644 --- a/lib/private/Repair/NC11/MoveAvatarsBackgroundJob.php +++ b/lib/private/Repair/NC11/MoveAvatarsBackgroundJob.php @@ -28,6 +28,7 @@ use OCP\Files\Folder; use OCP\Files\IAppData; use OCP\Files\IRootFolder; use OCP\Files\NotFoundException; +use OCP\Files\SimpleFS\ISimpleFolder; use OCP\ILogger; use OCP\IUser; use OCP\IUserManager; @@ -63,8 +64,14 @@ class MoveAvatarsBackgroundJob extends QueuedJob { } private function moveAvatars() { + try { + $ownCloudAvatars = $this->rootFolder->get('avatars'); + } catch (NotFoundException $e) { + $ownCloudAvatars = null; + } + $counter = 0; - $this->userManager->callForSeenUsers(function (IUser $user) use ($counter) { + $this->userManager->callForSeenUsers(function (IUser $user) use ($counter, $ownCloudAvatars) { $uid = $user->getUID(); \OC\Files\Filesystem::initMountPoints($uid); @@ -77,26 +84,61 @@ class MoveAvatarsBackgroundJob extends QueuedJob { $userData = $this->appData->newFolder($uid); } + $foundAvatars = $this->copyAvatarsFromFolder($userFolder, $userData); - $regex = '/^avatar\.([0-9]+\.)?(jpg|png)$/'; - $avatars = $userFolder->getDirectoryListing(); - - foreach ($avatars as $avatar) { - /** @var File $avatar */ - if (preg_match($regex, $avatar->getName())) { - /* - * This is not the most effective but it is the most abstract way - * to handle this. Avatars should be small anyways. - */ - $newAvatar = $userData->newFile($avatar->getName()); - $newAvatar->putContent($avatar->getContent()); - $avatar->delete(); + // ownCloud migration? + if ($foundAvatars === 0 && $ownCloudAvatars instanceof Folder) { + $parts = $this->buildOwnCloudAvatarPath($uid); + $userOwnCloudAvatar = $ownCloudAvatars; + foreach ($parts as $part) { + try { + $userOwnCloudAvatar = $userOwnCloudAvatar->get($part); + } catch (NotFoundException $e) { + return; + } } + + $this->copyAvatarsFromFolder($userOwnCloudAvatar, $userData); } + $counter++; if ($counter % 100 === 0) { $this->logger->info('{amount} avatars migrated', ['amount' => $counter]); } }); } + + /** + * @param Folder $source + * @param ISimpleFolder $target + * @return int + * @throws \OCP\Files\NotPermittedException + * @throws NotFoundException + */ + protected function copyAvatarsFromFolder(Folder $source, ISimpleFolder $target) { + $foundAvatars = 0; + $avatars = $source->getDirectoryListing(); + $regex = '/^avatar\.([0-9]+\.)?(jpg|png)$/'; + + foreach ($avatars as $avatar) { + /** @var File $avatar */ + if (preg_match($regex, $avatar->getName())) { + /* + * This is not the most effective but it is the most abstract way + * to handle this. Avatars should be small anyways. + */ + $newAvatar = $target->newFile($avatar->getName()); + $newAvatar->putContent($avatar->getContent()); + $avatar->delete(); + $foundAvatars++; + } + } + + return $foundAvatars; + } + + protected function buildOwnCloudAvatarPath($userId) { + $avatar = substr_replace(substr_replace(md5($userId), '/', 4, 0), '/', 2, 0); + return explode('/', $avatar); + } } |