Completed
Push — master ( ff3e8c...ea9b1c )
by Lukas
13:22 queued 02:44
created

MoveAvatarsBackgroundJob::moveAvatars()   B

Complexity

Conditions 6
Paths 1

Size

Total Lines 39
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 21
nc 1
nop 0
dl 0
loc 39
rs 8.439
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright 2016 Roeland Jago Douma <[email protected]>
4
 *
5
 * @author Roeland Jago Douma <[email protected]>
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
25
use OC\BackgroundJob\QueuedJob;
26
use OCP\Files\File;
27
use OCP\Files\Folder;
28
use OCP\Files\IAppData;
29
use OCP\Files\IRootFolder;
30
use OCP\Files\NotFoundException;
31
use OCP\ILogger;
32
use OCP\IUser;
33
use OCP\IUserManager;
34
35
class MoveAvatarsBackgroundJob extends QueuedJob {
36
37
	/** @var IUserManager */
38
	private $userManager;
39
40
	/** @var IRootFolder */
41
	private $rootFolder;
42
43
	/** @var IAppData */
44
	private $appData;
45
46
	/** @var ILogger */
47
	private $logger;
48
49
	/**
50
	 * MoveAvatars constructor.
51
	 */
52
	public function __construct() {
53
		$this->userManager = \OC::$server->getUserManager();
54
		$this->rootFolder = \OC::$server->getRootFolder();
55
		$this->logger = \OC::$server->getLogger();
56
		$this->appData = \OC::$server->getAppDataDir('avatar');
57
	}
58
59
	public function run($arguments) {
60
		$this->logger->info('Started migrating avatars to AppData folder');
61
		$this->moveAvatars();
62
		$this->logger->info('All avatars migrated to AppData folder');
63
	}
64
65
	private function moveAvatars() {
66
		$counter = 0;
67
		$this->userManager->callForAllUsers(function (IUser $user) use ($counter) {
68
			if ($user->getLastLogin() !== 0) {
69
				$uid = $user->getUID();
70
71
				\OC\Files\Filesystem::initMountPoints($uid);
72
				/** @var Folder $userFolder */
73
				$userFolder = $this->rootFolder->get($uid);
74
75
				try {
76
					$userData = $this->appData->getFolder($uid);
77
				} catch (NotFoundException $e) {
78
					$userData = $this->appData->newFolder($uid);
79
				}
80
81
82
				$regex = '/^avatar\.([0-9]+\.)?(jpg|png)$/';
83
				$avatars = $userFolder->getDirectoryListing();
84
85
				foreach ($avatars as $avatar) {
86
					/** @var File $avatar */
87
					if (preg_match($regex, $avatar->getName())) {
88
						/*
89
						 * This is not the most effective but it is the most abstract way
90
						 * to handle this. Avatars should be small anyways.
91
						 */
92
						$newAvatar = $userData->newFile($avatar->getName());
93
						$newAvatar->putContent($avatar->getContent());
94
						$avatar->delete();
95
					}
96
				}
97
			}
98
			$counter++;
99
			if ($counter % 100) {
100
				$this->logger->info('{amount} avatars migrated', ['amount' => $counter]);
101
			}
102
		});
103
	}
104
}
105