summaryrefslogtreecommitdiffstats
path: root/apps/theming/lib/Jobs/MigrateBackgroundImages.php
blob: 97806fa600ac9ef25aa07f3b7fb5abef44faef9a (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
<?php

declare(strict_types=1);

/**
 * @copyright Copyright (c) 2022 Arthur Schiwon <blizzz@arthur-schiwon.de>
 *
 * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
 *
 * @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 <https://www.gnu.org/licenses/>.
 *
 */

namespace OCA\Theming\Jobs;

use OCA\Theming\AppInfo\Application;
use OCP\App\IAppManager;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\IJobList;
use OCP\BackgroundJob\QueuedJob;
use OCP\Files\AppData\IAppDataFactory;
use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException;
use OCP\IConfig;

class MigrateBackgroundImages extends QueuedJob {
	public const TIME_SENSITIVE = 0;

	private IConfig $config;
	private IAppManager $appManager;
	private IAppDataFactory $appDataFactory;
	private IJobList $jobList;

	public function __construct(ITimeFactory $time, IAppDataFactory $appDataFactory, IConfig $config, IAppManager $appManager, IJobList $jobList) {
		parent::__construct($time);
		$this->config = $config;
		$this->appManager = $appManager;
		$this->appDataFactory = $appDataFactory;
		$this->jobList = $jobList;
	}

	protected function run($argument): void {
		if (!$this->appManager->isEnabledForUser('dashboard')) {
			return;
		}

		$themingData = $this->appDataFactory->get(Application::APP_ID);
		$dashboardData = $this->appDataFactory->get('dashboard');

		$userIds = $this->config->getUsersForUserValue('theming', 'background', 'custom');

		$notSoFastMode = \count($userIds) > 5000;
		$reTrigger = false;
		$processed = 0;

		foreach ($userIds as $userId) {
			try {
				// precondition
				if ($notSoFastMode) {
					if ($this->config->getUserValue($userId, 'theming', 'background-migrated', '0') === '1') {
						// already migrated
						continue;
					}
					$reTrigger = true;
				}

				// migration
				$file = $dashboardData->getFolder($userId)->getFile('background.jpg');
				try {
					$targetDir = $themingData->getFolder($userId);
				} catch (NotFoundException $e) {
					$targetDir = $themingData->newFolder($userId);
				}
				if (!$targetDir->fileExists('background.jpg')) {
					$targetDir->newFile('background.jpg', $file->getContent());
				}
				$file->delete();
			} catch (NotFoundException|NotPermittedException $e) {
			}
			// capture state
			if ($notSoFastMode) {
				$this->config->setUserValue($userId, 'theming', 'background-migrated', '1');
				$processed++;
			}
			if ($processed > 4999) {
				break;
			}
		}

		if ($reTrigger) {
			$this->jobList->add(self::class);
		}
	}
}