Skip to content

Commit

Permalink
fix(theming): fix admin images migration
Browse files Browse the repository at this point in the history
Signed-off-by: John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
  • Loading branch information
skjnldsv committed Jan 20, 2023
1 parent 0df8fc0 commit 7581dbb
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 13 deletions.
2 changes: 1 addition & 1 deletion apps/theming/appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@

<repair-steps>
<pre-migration>
<step>OCA\Theming\Migration\MigrateAdminConfig</step>
<step>OCA\Theming\Migration\MigrateUserConfig</step>
</pre-migration>
<post-migration>
<step>OCA\Theming\Migration\InitBackgroundImagesMigration</step>
<step>OCA\Theming\Migration\CleanupOldCache</step>
</post-migration>
</repair-steps>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,14 @@
namespace OCA\Theming\Migration;

use OCP\Files\IAppData;
use OCP\Files\NotFoundException;
use OCP\Files\SimpleFS\ISimpleFolder;
use OCP\IL10N;
use OCP\Migration\IOutput;
use OCP\Migration\IRepairStep;
use Throwable;

class CleanupOldCache implements IRepairStep {
class MigrateAdminConfig implements IRepairStep {
private const CACHE_FOLDERS = [
'global',
'users',
Expand All @@ -55,22 +56,51 @@ public function getName(): string {
}

public function run(IOutput $output): void {
$folders = array_filter(
$this->appData->getDirectoryListing(),
fn (ISimpleFolder $folder): bool => !in_array($folder->getName(), static::CACHE_FOLDERS, true),
);
$output->info('Migrating admin images');
$this->migrateAdminImages($output);
$this->cleanupAdminImages($output);
}

$output->startProgress(count($folders));
private function migrateAdminImages(IOutput $output) {
try {
$images = $this->appData->getFolder('images');
$output->info('Migrating admin images');

foreach ($folders as $folder) {
// get or init the global folder if any
try {
$folder->delete();
} catch (Throwable $e) {
$output->warning($this->l10n->t('Failed to delete folder: "%1$s", error: %2$s', [$folder->getName(), $e->getMessage()]));
$global = $this->appData->getFolder('global');
} catch (NotFoundException $e) {
$global = $this->appData->newFolder('global');
}

// get or init the new images folder if any
try {
$newImages = $global->getFolder('images');
} catch (NotFoundException $e) {
$newImages = $global->newFolder('images');
}

$files = $images->getDirectoryListing();
$output->startProgress(count($files));
foreach($files as $file) {
$newImages->newFile($file->getName(), $file->getContent());
$output->advance();
}
$output->advance();

$output->finishProgress();
} catch(NotFoundException $e) {
$output->info('No admin images to migrate');
}
}

$output->finishProgress();

private function cleanupAdminImages(IOutput $output) {
try {
$images = $this->appData->getFolder('images');
$images->delete();
} catch (NotFoundException $e) {
} catch (Throwable $e) {
$output->warning($this->l10n->t('Failed to cleanup the old admin image folder', [$e->getMessage()]));
}
}
}

0 comments on commit 7581dbb

Please sign in to comment.