You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MigrateBackgroundImages.php 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2022 Arthur Schiwon <blizzz@arthur-schiwon.de>
  5. *
  6. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\Theming\Jobs;
  25. use OCA\Theming\AppInfo\Application;
  26. use OCP\App\IAppManager;
  27. use OCP\AppFramework\Utility\ITimeFactory;
  28. use OCP\BackgroundJob\IJobList;
  29. use OCP\BackgroundJob\QueuedJob;
  30. use OCP\Files\AppData\IAppDataFactory;
  31. use OCP\Files\IAppData;
  32. use OCP\Files\NotFoundException;
  33. use OCP\Files\NotPermittedException;
  34. use OCP\Files\SimpleFS\ISimpleFolder;
  35. use OCP\IDBConnection;
  36. use Psr\Log\LoggerInterface;
  37. class MigrateBackgroundImages extends QueuedJob {
  38. public const TIME_SENSITIVE = 0;
  39. public const STAGE_PREPARE = 'prepare';
  40. public const STAGE_EXECUTE = 'execute';
  41. // will be saved in appdata/theming/global/
  42. protected const STATE_FILE_NAME = '25_dashboard_to_theming_migration_users.json';
  43. private IAppDataFactory $appDataFactory;
  44. private IJobList $jobList;
  45. private IDBConnection $dbc;
  46. private IAppData $appData;
  47. private LoggerInterface $logger;
  48. public function __construct(
  49. ITimeFactory $time,
  50. IAppDataFactory $appDataFactory,
  51. IJobList $jobList,
  52. IDBConnection $dbc,
  53. IAppData $appData,
  54. LoggerInterface $logger
  55. ) {
  56. parent::__construct($time);
  57. $this->appDataFactory = $appDataFactory;
  58. $this->jobList = $jobList;
  59. $this->dbc = $dbc;
  60. $this->appData = $appData;
  61. $this->logger = $logger;
  62. }
  63. protected function run($argument): void {
  64. if (!isset($argument['stage'])) {
  65. // not executed in 25.0.0?!
  66. $argument['stage'] = self::STAGE_PREPARE;
  67. }
  68. switch ($argument['stage']) {
  69. case self::STAGE_PREPARE:
  70. $this->runPreparation();
  71. break;
  72. case self::STAGE_EXECUTE:
  73. $this->runMigration();
  74. break;
  75. default:
  76. break;
  77. }
  78. }
  79. protected function runPreparation(): void {
  80. try {
  81. $selector = $this->dbc->getQueryBuilder();
  82. $result = $selector->select('userid')
  83. ->from('preferences')
  84. ->where($selector->expr()->eq('appid', $selector->createNamedParameter('theming')))
  85. ->andWhere($selector->expr()->eq('configkey', $selector->createNamedParameter('background')))
  86. ->andWhere($selector->expr()->eq('configvalue', $selector->createNamedParameter('custom')))
  87. ->executeQuery();
  88. $userIds = $result->fetchAll(\PDO::FETCH_COLUMN);
  89. $this->storeUserIdsToProcess($userIds);
  90. } catch (\Throwable $t) {
  91. $this->jobList->add(self::class, self::STAGE_PREPARE);
  92. throw $t;
  93. }
  94. $this->jobList->add(self::class, self::STAGE_EXECUTE);
  95. }
  96. /**
  97. * @throws NotPermittedException
  98. * @throws NotFoundException
  99. */
  100. protected function runMigration(): void {
  101. $allUserIds = $this->readUserIdsToProcess();
  102. $notSoFastMode = count($allUserIds) > 5000;
  103. $dashboardData = $this->appDataFactory->get('dashboard');
  104. $userIds = $notSoFastMode ? array_slice($allUserIds, 0, 5000) : $allUserIds;
  105. foreach ($userIds as $userId) {
  106. try {
  107. // migration
  108. $file = $dashboardData->getFolder($userId)->getFile('background.jpg');
  109. $targetDir = $this->getUserFolder($userId);
  110. if (!$targetDir->fileExists('background.jpg')) {
  111. $targetDir->newFile('background.jpg', $file->getContent());
  112. }
  113. $file->delete();
  114. } catch (NotFoundException|NotPermittedException $e) {
  115. }
  116. }
  117. if ($notSoFastMode) {
  118. $remainingUserIds = array_slice($allUserIds, 5000);
  119. $this->storeUserIdsToProcess($remainingUserIds);
  120. $this->jobList->add(self::class, ['stage' => self::STAGE_EXECUTE]);
  121. } else {
  122. $this->deleteStateFile();
  123. }
  124. }
  125. /**
  126. * @throws NotPermittedException
  127. * @throws NotFoundException
  128. */
  129. protected function readUserIdsToProcess(): array {
  130. $globalFolder = $this->appData->getFolder('global');
  131. if ($globalFolder->fileExists(self::STATE_FILE_NAME)) {
  132. $file = $globalFolder->getFile(self::STATE_FILE_NAME);
  133. try {
  134. $userIds = \json_decode($file->getContent(), true);
  135. } catch (NotFoundException $e) {
  136. $userIds = [];
  137. }
  138. if ($userIds === null) {
  139. $userIds = [];
  140. }
  141. } else {
  142. $userIds = [];
  143. }
  144. return $userIds;
  145. }
  146. /**
  147. * @throws NotFoundException
  148. */
  149. protected function storeUserIdsToProcess(array $userIds): void {
  150. $storableUserIds = \json_encode($userIds);
  151. $globalFolder = $this->appData->getFolder('global');
  152. try {
  153. if ($globalFolder->fileExists(self::STATE_FILE_NAME)) {
  154. $file = $globalFolder->getFile(self::STATE_FILE_NAME);
  155. } else {
  156. $file = $globalFolder->newFile(self::STATE_FILE_NAME);
  157. }
  158. $file->putContent($storableUserIds);
  159. } catch (NotFoundException $e) {
  160. } catch (NotPermittedException $e) {
  161. $this->logger->warning('Lacking permissions to create {file}',
  162. [
  163. 'app' => 'theming',
  164. 'file' => self::STATE_FILE_NAME,
  165. 'exception' => $e,
  166. ]
  167. );
  168. }
  169. }
  170. /**
  171. * @throws NotFoundException
  172. */
  173. protected function deleteStateFile(): void {
  174. $globalFolder = $this->appData->getFolder('global');
  175. if ($globalFolder->fileExists(self::STATE_FILE_NAME)) {
  176. $file = $globalFolder->getFile(self::STATE_FILE_NAME);
  177. try {
  178. $file->delete();
  179. } catch (NotPermittedException $e) {
  180. $this->logger->info('Could not delete {file} due to permissions. It is safe to delete manually inside data -> appdata -> theming -> global.',
  181. [
  182. 'app' => 'theming',
  183. 'file' => $file->getName(),
  184. 'exception' => $e,
  185. ]
  186. );
  187. }
  188. }
  189. }
  190. /**
  191. * Get the root location for users theming data
  192. */
  193. protected function getUserFolder(string $userId): ISimpleFolder {
  194. $themingData = $this->appDataFactory->get(Application::APP_ID);
  195. try {
  196. $rootFolder = $themingData->getFolder('users');
  197. } catch (NotFoundException $e) {
  198. $rootFolder = $themingData->newFolder('users');
  199. }
  200. try {
  201. return $rootFolder->getFolder($userId);
  202. } catch (NotFoundException $e) {
  203. return $rootFolder->newFolder($userId);
  204. }
  205. }
  206. }