1
0

MigrateAdminConfig.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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\IL10N;
  28. use OCP\Migration\IOutput;
  29. use OCP\Migration\IRepairStep;
  30. use Throwable;
  31. class MigrateAdminConfig implements IRepairStep {
  32. private IAppData $appData;
  33. private IL10N $l10n;
  34. public function __construct(
  35. IAppData $appData,
  36. IL10N $l10n
  37. ) {
  38. $this->appData = $appData;
  39. $this->l10n = $l10n;
  40. }
  41. public function getName(): string {
  42. return $this->l10n->t('Failed to clean up the old administration theming images folder');
  43. }
  44. public function run(IOutput $output): void {
  45. $output->info('Migrating administration images');
  46. $this->migrateAdminImages($output);
  47. $this->cleanupAdminImages($output);
  48. }
  49. private function migrateAdminImages(IOutput $output): void {
  50. try {
  51. $images = $this->appData->getFolder('images');
  52. $output->info('Migrating administration images');
  53. // get or init the global folder if any
  54. try {
  55. $global = $this->appData->getFolder('global');
  56. } catch (NotFoundException $e) {
  57. $global = $this->appData->newFolder('global');
  58. }
  59. // get or init the new images folder if any
  60. try {
  61. $newImages = $global->getFolder('images');
  62. } catch (NotFoundException $e) {
  63. $newImages = $global->newFolder('images');
  64. }
  65. $files = $images->getDirectoryListing();
  66. $output->startProgress(count($files));
  67. foreach($files as $file) {
  68. $newImages->newFile($file->getName(), $file->getContent());
  69. $output->advance();
  70. }
  71. $output->finishProgress();
  72. } catch(NotFoundException $e) {
  73. $output->info('No administration images to migrate');
  74. }
  75. }
  76. private function cleanupAdminImages(IOutput $output): void {
  77. try {
  78. $images = $this->appData->getFolder('images');
  79. $images->delete();
  80. } catch (NotFoundException $e) {
  81. } catch (Throwable $e) {
  82. $output->warning($this->l10n->t('Failed to clean up the old administration image folder', [$e->getMessage()]));
  83. }
  84. }
  85. }