-
-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #919 from nextcloud/register-mimetypes
Register mimetypes
- Loading branch information
Showing
17 changed files
with
343 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
|
||
namespace OCA\Maps\Migration; | ||
|
||
use OCP\Files\IMimeTypeLoader; | ||
use OCP\Migration\IOutput; | ||
use OCP\Migration\IRepairStep; | ||
|
||
class RegisterMimeType implements IRepairStep | ||
{ | ||
const CUSTOM_MIMETYPEMAPPING = 'mimetypemapping.json'; | ||
|
||
protected IMimeTypeLoader $mimeTypeLoader; | ||
|
||
public function __construct(IMimeTypeLoader $mimeTypeLoader) | ||
{ | ||
$this->mimeTypeLoader = $mimeTypeLoader; | ||
} | ||
|
||
public function getName() | ||
{ | ||
return 'Register Maps MIME types"'; | ||
} | ||
|
||
private function registerForExistingFiles() | ||
{ | ||
$mimeTypeId = $this->mimeTypeLoader->getId('application/x-nextcloud-maps'); | ||
$this->mimeTypeLoader->updateFilecache('maps', $mimeTypeId); | ||
|
||
$mimeTypeId = $this->mimeTypeLoader->getId('application/x-nextcloud-noindex'); | ||
$this->mimeTypeLoader->updateFilecache('noindex', $mimeTypeId); | ||
|
||
$mimeTypeId = $this->mimeTypeLoader->getId('application/x-nextcloud-nomedia'); | ||
$this->mimeTypeLoader->updateFilecache('nomedia', $mimeTypeId); | ||
|
||
$mimeTypeId = $this->mimeTypeLoader->getId('application/x-nextcloud-noimage'); | ||
$this->mimeTypeLoader->updateFilecache('noimage', $mimeTypeId); | ||
|
||
$mimeTypeId = $this->mimeTypeLoader->getId('application/x-nextcloud-maps-notrack'); | ||
$this->mimeTypeLoader->updateFilecache('notrack', $mimeTypeId); | ||
} | ||
|
||
private function registerForNewFiles() | ||
{ | ||
$mapping = [ | ||
'maps' => ['application/x-nextcloud-maps'], | ||
'noindex' => ['application/x-nextcloud-noindex'], | ||
'nomedia' => ['application/x-nextcloud-nomedia'], | ||
'noimage' => ['application/x-nextcloud-noimage'], | ||
'notrack' => ['application/x-nextcloud-maps-notrack'], | ||
]; | ||
$mappingFile = \OC::$configDir . self::CUSTOM_MIMETYPEMAPPING; | ||
|
||
if (file_exists($mappingFile)) { | ||
$existingMapping = json_decode(file_get_contents($mappingFile), true); | ||
if (json_last_error() === JSON_ERROR_NONE && is_array($existingMapping)) { | ||
$mapping = array_merge($existingMapping, $mapping); | ||
} | ||
} | ||
|
||
file_put_contents($mappingFile, json_encode($mapping, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT)); | ||
} | ||
|
||
public function run(IOutput $output) | ||
{ | ||
$output->info('Registering the mimetype...'); | ||
|
||
// Register the mime type for existing files | ||
$this->registerForExistingFiles(); | ||
|
||
// Register the mime type for new files | ||
$this->registerForNewFiles(); | ||
|
||
$output->info('The mimetype was successfully registered.'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
|
||
namespace OCA\Maps\Migration; | ||
|
||
use OCP\Files\IMimeTypeLoader; | ||
use OCP\Migration\IOutput; | ||
use OCP\Migration\IRepairStep; | ||
|
||
class UnregisterMimeType implements IRepairStep | ||
{ | ||
const CUSTOM_MIMETYPEMAPPING = 'mimetypemapping.json'; | ||
|
||
protected IMimeTypeLoader $mimeTypeLoader; | ||
|
||
public function __construct(IMimeTypeLoader $mimeTypeLoader) | ||
{ | ||
$this->mimeTypeLoader = $mimeTypeLoader; | ||
} | ||
|
||
public function getName() | ||
{ | ||
return 'Register Maps MIME types"'; | ||
} | ||
|
||
private function unregisterForExistingFiles() | ||
{ | ||
$mimeTypeId = $this->mimeTypeLoader->getId('application/octet-stream'); | ||
$this->mimeTypeLoader->updateFilecache('maps', $mimeTypeId); | ||
$this->mimeTypeLoader->updateFilecache('noindex', $mimeTypeId); | ||
$this->mimeTypeLoader->updateFilecache('nomedia', $mimeTypeId); | ||
$this->mimeTypeLoader->updateFilecache('noimage', $mimeTypeId); | ||
$this->mimeTypeLoader->updateFilecache('notrack', $mimeTypeId); | ||
} | ||
|
||
private function unregisterForNewFiles() | ||
{ | ||
$mappingFile = \OC::$configDir . self::CUSTOM_MIMETYPEMAPPING; | ||
|
||
if (file_exists($mappingFile)) { | ||
$mapping = json_decode(file_get_contents($mappingFile), true); | ||
if (json_last_error() === JSON_ERROR_NONE && is_array($mapping)) { | ||
unset($mapping['maps']); | ||
unset($mapping['noindex']); | ||
unset($mapping['nomedia']); | ||
unset($mapping['noimage']); | ||
unset($mapping['notrack']); | ||
} else { | ||
$mapping = []; | ||
} | ||
file_put_contents($mappingFile, json_encode($mapping, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT)); | ||
} | ||
} | ||
|
||
public function run(IOutput $output) | ||
{ | ||
$output->info('Unregistering the mimetype...'); | ||
|
||
// Register the mime type for existing files | ||
$this->unregisterForExistingFiles(); | ||
|
||
// Register the mime type for new files | ||
$this->unregisterForNewFiles(); | ||
|
||
$output->info('The mimetype was successfully unregistered.'); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.