1
0

MigrateAdminConfig.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2022 Christopher Ng <chrng8@gmail.com>
  5. *
  6. * @author Christopher Ng <chrng8@gmail.com>
  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 <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\Theming\Migration;
  25. use OCP\Files\IAppData;
  26. use OCP\Files\NotFoundException;
  27. use OCP\Files\SimpleFS\ISimpleFolder;
  28. use OCP\IL10N;
  29. use OCP\Migration\IOutput;
  30. use OCP\Migration\IRepairStep;
  31. use Throwable;
  32. class MigrateAdminConfig implements IRepairStep {
  33. private IAppData $appData;
  34. private IL10N $l10n;
  35. public function __construct(
  36. IAppData $appData,
  37. IL10N $l10n
  38. ) {
  39. $this->appData = $appData;
  40. $this->l10n = $l10n;
  41. }
  42. public function getName(): string {
  43. return $this->l10n->t('Migrate and clean up admin theming images');
  44. }
  45. public function run(IOutput $output): void {
  46. $output->info('Migrating admin images');
  47. $this->migrateAdminImages($output);
  48. $this->cleanupAdminImages($output);
  49. }
  50. private function migrateAdminImages(IOutput $output): void {
  51. try {
  52. $images = $this->appData->getFolder('images');
  53. $output->info('Migrating admin images');
  54. // get or init the global folder if any
  55. try {
  56. $global = $this->appData->getFolder('global');
  57. } catch (NotFoundException $e) {
  58. $global = $this->appData->newFolder('global');
  59. }
  60. // get or init the new images folder if any
  61. try {
  62. $newImages = $global->getFolder('images');
  63. } catch (NotFoundException $e) {
  64. $newImages = $global->newFolder('images');
  65. }
  66. $files = $images->getDirectoryListing();
  67. $output->startProgress(count($files));
  68. foreach($files as $file) {
  69. $newImages->newFile($file->getName(), $file->getContent());
  70. $output->advance();
  71. }
  72. $output->finishProgress();
  73. } catch(NotFoundException $e) {
  74. $output->info('No admin images to migrate');
  75. }
  76. }
  77. private function cleanupAdminImages(IOutput $output): void {
  78. try {
  79. $images = $this->appData->getFolder('images');
  80. $images->delete();
  81. } catch (NotFoundException $e) {
  82. } catch (Throwable $e) {
  83. $output->warning($this->l10n->t('Failed to clean up the old admin theming images folder', [$e->getMessage()]));
  84. }
  85. }
  86. }