Skip to content

Commit

Permalink
Add abstract class for migrations
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Kesselberg <[email protected]>
  • Loading branch information
kesselb committed Feb 23, 2020
1 parent e1d8e1c commit 6742d2f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 30 deletions.
19 changes: 19 additions & 0 deletions keeweb/lib/Migration/MimeTypeMigration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace OCA\Keeweb\Migration;

use OCP\Files\IMimeTypeLoader;
use OCP\Migration\IOutput;
use OCP\Migration\IRepairStep;

abstract class MimeTypeMigration implements IRepairStep
{
const CUSTOM_MIMETYPEMAPPING = 'mimetypemapping.json';

protected $mimeTypeLoader;

public function __construct(IMimeTypeLoader $mimeTypeLoader)
{
$this->mimeTypeLoader = $mimeTypeLoader;
}
}
19 changes: 6 additions & 13 deletions keeweb/lib/Migration/RegisterMimeType.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,23 @@
use OCP\Migration\IOutput;
use OCP\Migration\IRepairStep;

class RegisterMimeType implements IRepairStep
class RegisterMimeType extends MimeTypeMigration
{
private $mimeTypeLoader;

public function __construct(IMimeTypeLoader $mimeTypeLoader)
{
$this->mimeTypeLoader = $mimeTypeLoader;
}

public function getName()
{
return 'Register MIME type for "application/x-kdbx"';
}

private function registerForExistingFiles()
{
$mimetypeId = $this->mimeTypeLoader->getId('application/x-kdbx');
$this->mimeTypeLoader->updateFilecache('kdbx', $mimetypeId);
$mimeTypeId = $this->mimeTypeLoader->getId('application/x-kdbx');
$this->mimeTypeLoader->updateFilecache('kdbx', $mimeTypeId);
}

private function registerForNewFiles()
{
$mapping = ['kdbx' => ['application/x-kdbx']];
$mappingFile = \OC::$configDir . 'mimetypemapping.json';
$mappingFile = \OC::$configDir . self::CUSTOM_MIMETYPEMAPPING;

if (file_exists($mappingFile)) {
$existingMapping = json_decode(file_get_contents($mappingFile), true);
Expand All @@ -43,14 +36,14 @@ private function registerForNewFiles()

public function run(IOutput $output)
{
$this->logger->info('Registering the mimetype...');
$output->info('Registering the mimetype...');

// Register the mime type for existing files
$this->registerForExistingFiles();

// Register the mime type for new files
$this->registerForNewFiles();

$this->logger->info('The mimetype was successfully registered.');
$output->info('The mimetype was successfully registered.');
}
}
27 changes: 10 additions & 17 deletions keeweb/lib/Migration/UnregisterMimeType.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,22 @@
use OCP\Migration\IOutput;
use OCP\Migration\IRepairStep;

class UnregisterMimeType implements IRepairStep
class UnregisterMimeType extends MimeTypeMigration
{
private $mimeTypeLoader;

public function __construct(IMimeTypeLoader $mimeTypeLoader)
{
$this->mimeTypeLoader = $mimeTypeLoader;
}

public function getName()
{
return 'Unregister MIME type for "application/x-kdbx"';
}

private function registerForExistingFiles()
private function unregisterForExistingFiles()
{
$mimetypeId = $this->mimeTypeLoader->getId('application/octet-stream');
$this->mimeTypeLoader->updateFilecache('kdbx', $mimetypeId);
$mimeTypeId = $this->mimeTypeLoader->getId('application/octet-stream');
$this->mimeTypeLoader->updateFilecache('kdbx', $mimeTypeId);
}

private function registerForNewFiles()
private function unregisterForNewFiles()
{
$mappingFile = \OC::$configDir . 'mimetypemapping.json';
$mappingFile = \OC::$configDir . self::CUSTOM_MIMETYPEMAPPING;

if (file_exists($mappingFile)) {
$mapping = json_decode(file_get_contents($mappingFile), true);
Expand All @@ -43,14 +36,14 @@ private function registerForNewFiles()

public function run(IOutput $output)
{
$this->logger->info('Unregistering the mimetype...');
$output->info('Unregistering the mimetype...');

// Register the mime type for existing files
$this->registerForExistingFiles();
$this->unregisterForExistingFiles();

// Register the mime type for new files
$this->registerForNewFiles();
$this->unregisterForNewFiles();

$this->logger->info('The mimetype was successfully unregistered.');
$output->info('The mimetype was successfully unregistered.');
}
}

0 comments on commit 6742d2f

Please sign in to comment.