]> source.dussan.org Git - nextcloud-server.git/commitdiff
graceful background image handling 34461/head
authorArthur Schiwon <blizzz@arthur-schiwon.de>
Thu, 13 Oct 2022 13:40:47 +0000 (15:40 +0200)
committerArthur Schiwon <blizzz@arthur-schiwon.de>
Thu, 13 Oct 2022 15:51:30 +0000 (17:51 +0200)
- fallback to background image from old location
- migrate background images to new location as insensitive job

Signed-off-by: Arthur Schiwon <blizzz@arthur-schiwon.de>
apps/theming/appinfo/info.xml
apps/theming/lib/Jobs/MigrateBackgroundImages.php [new file with mode: 0644]
apps/theming/lib/Migration/InitBackgroundImagesMigration.php [new file with mode: 0644]
apps/theming/lib/Service/BackgroundService.php

index fcdaade0fd5f2e0b1050e4e5e9e049a54e812f99..402ea9f0ba218da02ff1cb547c8c39d004110bbf 100644 (file)
@@ -31,6 +31,9 @@
                <pre-migration>
                        <step>OCA\Theming\Migration\MigrateUserConfig</step>
                </pre-migration>
+               <post-migration>
+                       <step>OCA\Theming\Migration\InitBackgroundImagesMigration</step>
+               </post-migration>
        </repair-steps>
 
        <commands>
diff --git a/apps/theming/lib/Jobs/MigrateBackgroundImages.php b/apps/theming/lib/Jobs/MigrateBackgroundImages.php
new file mode 100644 (file)
index 0000000..97806fa
--- /dev/null
@@ -0,0 +1,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);
+               }
+       }
+}
diff --git a/apps/theming/lib/Migration/InitBackgroundImagesMigration.php b/apps/theming/lib/Migration/InitBackgroundImagesMigration.php
new file mode 100644 (file)
index 0000000..c23a917
--- /dev/null
@@ -0,0 +1,48 @@
+<?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\Migration;
+
+use OCA\Theming\Jobs\MigrateBackgroundImages;
+use OCP\BackgroundJob\IJobList;
+use OCP\Migration\IOutput;
+
+class InitBackgroundImagesMigration implements \OCP\Migration\IRepairStep {
+
+       private IJobList $jobList;
+
+       public function __construct(IJobList $jobList) {
+               $this->jobList = $jobList;
+       }
+
+       public function getName() {
+               return 'Initialize migration of background images from dashboard to theming app';
+       }
+
+       public function run(IOutput $output) {
+               $this->jobList->add(MigrateBackgroundImages::class);
+       }
+}
index 0614fe00357677002d45418812429b9d9a9bf715..36623735728ba85afa5c635cff94e802d71b6489 100644 (file)
@@ -30,6 +30,7 @@ namespace OCA\Theming\Service;
 use InvalidArgumentException;
 use OC\User\NoUserException;
 use OCA\Theming\AppInfo\Application;
+use OCP\Files\AppData\IAppDataFactory;
 use OCP\Files\File;
 use OCP\Files\IAppData;
 use OCP\Files\IRootFolder;
@@ -133,20 +134,22 @@ class BackgroundService {
        private IAppData $appData;
        private IConfig $config;
        private string $userId;
+       private IAppDataFactory $appDataFactory;
 
        public function __construct(
-               IRootFolder $rootFolder,
-               IAppData $appData,
-               IConfig $config,
-               ?string $userId
+               IRootFolder     $rootFolder,
+               IAppDataFactory $appDataFactory,
+               IConfig         $config,
+               ?string         $userId
        ) {
                if ($userId === null) {
                        return;
                }
                $this->rootFolder = $rootFolder;
-               $this->appData = $appData;
+               $this->appData = $appDataFactory->get(Application::APP_ID);
                $this->config = $config;
                $this->userId = $userId;
+               $this->appDataFactory = $appDataFactory;
        }
 
        public function setDefaultBackground(): void {
@@ -193,6 +196,11 @@ class BackgroundService {
                        try {
                                return $this->getAppDataFolder()->getFile('background.jpg');
                        } catch (NotFoundException | NotPermittedException $e) {
+                               try {
+                                       // Fallback can be removed in 26
+                                       $dashboardFolder = $this->appDataFactory->get('dashboard');
+                                       return $dashboardFolder->getFolder($this->userId)->getFile('background.jpg');
+                               } catch (\Throwable $t) {}
                        }
                }
                return null;