-
-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathRegisterMimeType.php
66 lines (51 loc) · 2.16 KB
/
RegisterMimeType.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
namespace OCA\DICOMViewer\Migration;
use OCP\Migration\IOutput;
use OCP\Migration\IRepairStep;
use OCP\ILogger;
class RegisterMimeType implements IRepairStep {
protected $logger;
private $customMimetypeMapping;
public function __construct(ILogger $logger) {
$this->logger = $logger;
// Define the custom mimetype mapping
$this->customMimetypeMapping = array(
"dcm" => array("application/dicom"),
"ima" => array("application/dicom"),
"dicom" => array("application/dicom"),
"dic" => array("application/dicom"),
"dc3" => array("application/dicom"),
"pano" => array("application/dicom")
);
}
public function getName() {
return 'Register MIME type for "application/dcm"';
}
private function registerForExistingFiles() {
$mimetypeMapping = $this->customMimetypeMapping;
$mimeTypeLoader = \OC::$server->getMimeTypeLoader();
foreach($mimetypeMapping as $mimetypeKey => $mimetypeValues) {
foreach($mimetypeValues as $mimetypeValue) {
$mimeId = $mimeTypeLoader->getId($mimetypeValue);
$mimeTypeLoader->updateFilecache($mimetypeKey, $mimeId);
}
}
}
private function registerForNewFiles() {
$mimetypeMapping = $this->customMimetypeMapping;
$mimetypeMappingFile = \OC::$configDir . 'mimetypemapping.json';
if (file_exists($mimetypeMappingFile)) {
$existingMimetypeMapping = json_decode(file_get_contents($mimetypeMappingFile), true);
$mimetypeMapping = array_merge($existingMimetypeMapping, $mimetypeMapping);
}
file_put_contents($mimetypeMappingFile, json_encode($mimetypeMapping, 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.');
}
}