Skip to content

Commit

Permalink
Add application/x-kdbx to mimetypemapping.json
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 d7ed6f3 commit e1d8e1c
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 72 deletions.
12 changes: 0 additions & 12 deletions keeweb/appinfo/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,6 @@
use OCP\AppFramework\App;
use OCA\Keeweb\Controller\PageController;

$mimeTypeDetector = \OC::$server->getMimeTypeDetector();
if ($mimeTypeDetector instanceof Detection) {
/** registerType without getAllMappings will prevent loading nextcloud's default mappings. */
$mimeTypeDetector->getAllMappings();
$mimeTypeDetector->registerType('kdbx', 'application/x-kdbx', 'application/x-kdbx');
}

if (\OC::$REQUESTEDAPP === 'dav') {
/** For dav requests it should be enough to register the mime type and skip the rest of the app initialization. */
return;
}

require_once __DIR__ . '/autoload.php';

class Application extends App {
Expand Down
14 changes: 7 additions & 7 deletions keeweb/appinfo/info.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0"?>
<info xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://apps.nextcloud.com/schema/apps/info.xsd">
<info xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://apps.nextcloud.com/schema/apps/info.xsd">
<id>keeweb</id>
<name>Keeweb</name>
<summary>Open Keepass stores</summary>
Expand All @@ -8,24 +8,24 @@
<licence>agpl</licence>
<author>Jonne Haß</author>
<namespace>Keeweb</namespace>
<types>
<filesystem/>
</types>
<category>tools</category>
<category>integration</category>
<website>https://github.com/jhass/nextcloud-keeweb</website>
<bugs>https://github.com/jhass/nextcloud-keeweb/issues</bugs>
<repository>https://github.com/jhass/nextcloud-keeweb</repository>
<screenshot>https://cloud.aeshna.de/keeweb.gif</screenshot>
<dependencies>
<nextcloud min-version="11" max-version="18" />
<nextcloud min-version="11" max-version="18"/>
</dependencies>
<repair-steps>
<install>
<step>OCA\Keeweb\Migration\AddMimetypeToFilecache</step>
<step>OCA\Keeweb\Migration\RegisterMimeType</step>
</install>
<post-migrate>
<step>OCA\Keeweb\Migration\RegisterMimeType</step>
</post-migrate>
<uninstall>
<step>OCA\Keeweb\Migration\RemoveMimetypeFromFilecache</step>
<step>OCA\Keeweb\Migration\UnregisterMimeType</step>
</uninstall>
</repair-steps>
</info>
Expand Down
27 changes: 0 additions & 27 deletions keeweb/lib/Migration/AddMimetypeToFilecache.php

This file was deleted.

56 changes: 56 additions & 0 deletions keeweb/lib/Migration/RegisterMimeType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace OCA\Keeweb\Migration;

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

class RegisterMimeType implements IRepairStep
{
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);
}

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

if (file_exists($mappingFile)) {
$existingMapping = json_decode(file_get_contents($mappingFile), true);
if (json_last_error() === JSON_ERROR_NONE) {
$mapping = array_merge($existingMapping, $mapping);
}
}

file_put_contents($mappingFile, json_encode($mapping, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT));
}

public function run(IOutput $output)
{
$this->logger->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.');
}
}
26 changes: 0 additions & 26 deletions keeweb/lib/Migration/RemoveMimetypeFromFilecache.php

This file was deleted.

56 changes: 56 additions & 0 deletions keeweb/lib/Migration/UnregisterMimeType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace OCA\Keeweb\Migration;

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

class UnregisterMimeType implements IRepairStep
{
private $mimeTypeLoader;

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

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

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

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

if (file_exists($mappingFile)) {
$mapping = json_decode(file_get_contents($mappingFile), true);
if (json_last_error() === JSON_ERROR_NONE) {
unset($mapping['kdbx']);
} else {
$mapping = [];
}
file_put_contents($mappingFile, json_encode($mapping, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT));
}
}

public function run(IOutput $output)
{
$this->logger->info('Unregistering 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 unregistered.');
}
}

0 comments on commit e1d8e1c

Please sign in to comment.