123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- <?php
- namespace OC\Repair\Owncloud;
- use OC\BackgroundJob\QueuedJob;
- use OCP\Files\IRootFolder;
- use OCP\Files\NotFoundException;
- use OCP\Files\Storage;
- use OCP\IAvatarManager;
- use OCP\IUser;
- use OCP\IUserManager;
- use Psr\Log\LoggerInterface;
- use function is_resource;
- class MoveAvatarsBackgroundJob extends QueuedJob {
-
- private $userManager;
-
- private $logger;
-
- private $avatarManager;
-
- private $owncloudAvatarStorage;
- public function __construct(IUserManager $userManager, LoggerInterface $logger, IAvatarManager $avatarManager, IRootFolder $rootFolder) {
- $this->userManager = $userManager;
- $this->logger = $logger;
- $this->avatarManager = $avatarManager;
- try {
- $this->owncloudAvatarStorage = $rootFolder->get('avatars')->getStorage();
- } catch (\Exception $e) {
- }
- }
- public function run($arguments) {
- $this->logger->info('Started migrating avatars to AppData folder');
- $this->moveAvatars();
- $this->logger->info('All avatars migrated to AppData folder');
- }
- private function moveAvatars(): void {
- if (!$this->owncloudAvatarStorage) {
- $this->logger->info('No legacy avatars available, skipping migration');
- return;
- }
- $counter = 0;
- $this->userManager->callForSeenUsers(function (IUser $user) use ($counter) {
- $uid = $user->getUID();
- $path = 'avatars/' . $this->buildOwnCloudAvatarPath($uid);
- $avatar = $this->avatarManager->getAvatar($uid);
- try {
- $avatarPath = $path . '/avatar.' . $this->getExtension($path);
- $resource = $this->owncloudAvatarStorage->fopen($avatarPath, 'r');
- if (is_resource($resource)) {
- $avatar->set($resource);
- fclose($resource);
- } else {
- throw new \Exception('Failed to open old avatar file for reading');
- }
- } catch (NotFoundException $e) {
-
- } catch (\Throwable $e) {
- $this->logger->error('Failed to migrate avatar for user ' . $uid, ['exception' => $e]);
- }
- $counter++;
- if ($counter % 100 === 0) {
- $this->logger->info('{amount} avatars migrated', ['amount' => $counter]);
- }
- });
- }
-
- private function getExtension(string $path): string {
- if ($this->owncloudAvatarStorage->file_exists("{$path}/avatar.jpg")) {
- return 'jpg';
- }
- if ($this->owncloudAvatarStorage->file_exists("{$path}/avatar.png")) {
- return 'png';
- }
- throw new NotFoundException("{$path}/avatar.jpg|png");
- }
- protected function buildOwnCloudAvatarPath(string $userId): string {
- return substr_replace(substr_replace(md5($userId), '/', 4, 0), '/', 2, 0);
- }
- }
|