aboutsummaryrefslogtreecommitdiffstats
path: root/lib/private/Repair/Owncloud/MoveAvatarsBackgroundJob.php
blob: ec076d116c53de0c7d66280ff27c739a705393d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php
/**
 * @copyright 2016 Roeland Jago Douma <roeland@famdouma.nl>
 *
 * @author Joas Schilling <coding@schilljs.com>
 * @author Morris Jobke <hey@morrisjobke.de>
 * @author Roeland Jago Douma <roeland@famdouma.nl>
 *
 * @license GNU AGPL version 3 or any later version
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
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;

class MoveAvatarsBackgroundJob extends QueuedJob {

	/** @var IUserManager */
	private $userManager;

	/** @var LoggerInterface */
	private $logger;

	/** @var IAvatarManager */
	private $avatarManager;

	/** @var Storage */
	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 ($resource) {
					$avatar->set($resource);
					fclose($resource);
				} else {
					throw new \Exception('Failed to open old avatar file for reading');
				}
			} catch (NotFoundException $e) {
				// In case there is no avatar we can just skip
			} 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]);
			}
		});
	}

	/**
	 * @throws NotFoundException
	 */
	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);
	}
}