Skip to content

Commit a36cf5d

Browse files
author
Mike van den Hoek
committed
(refactor): add mime mapping when retrieving the extension in InformationObject trait
1 parent 195e675 commit a36cf5d

File tree

3 files changed

+42
-12
lines changed

3 files changed

+42
-12
lines changed

src/Entities/Enkelvoudiginformatieobject.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,20 @@ public function formatType(): string
9292
return '';
9393
}
9494

95-
$parts = explode('/', $type);
95+
$mimeMap = [
96+
'application/pdf' => 'pdf',
97+
'application/msword' => 'doc',
98+
'application/vnd.openxmlformats-officedocument.wordprocessingml.document' => 'docx',
99+
'application/vnd.ms-excel' => 'xls',
100+
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' => 'xlsx',
101+
'text/plain' => 'txt',
102+
'text/csv' => 'csv',
103+
'text/html' => 'html',
104+
'application/json' => 'json',
105+
'application/xml' => 'xml',
106+
];
96107

97-
return end($parts) ?: '';
108+
return $mimeMap[$type] ?? '';
98109
}
99110

100111
public function formattedMetaData(): string

src/Routing/Controllers/ZaakInformationObjectRoutingController.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -144,12 +144,19 @@ protected function getExtension(string $file): string
144144
$finfo = finfo_open(FILEINFO_MIME_TYPE);
145145
$mimeType = finfo_file($finfo, $file);
146146

147-
if (! $mimeType) {
148-
return '';
149-
}
150-
151-
$parts = explode('/', $mimeType);
147+
$mimeMap = [
148+
'application/pdf' => 'pdf',
149+
'application/msword' => 'doc',
150+
'application/vnd.openxmlformats-officedocument.wordprocessingml.document' => 'docx',
151+
'application/vnd.ms-excel' => 'xls',
152+
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' => 'xlsx',
153+
'text/plain' => 'txt',
154+
'text/csv' => 'csv',
155+
'text/html' => 'html',
156+
'application/json' => 'json',
157+
'application/xml' => 'xml',
158+
];
152159

153-
return end($parts) ?: '';
160+
return $mimeMap[$mimeType] ?? '';
154161
}
155162
}

src/Traits/InformationObject.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public function informationObjectToBase64(string $url): string
1010
{
1111
try {
1212
$file = file_get_contents($url, false, $this->streamContext());
13-
} catch(Exception $e) {
13+
} catch (Exception $e) {
1414
$file = '';
1515
}
1616

@@ -32,9 +32,21 @@ public function getContentLength(string $url): string
3232
public function getExtension(string $url): string
3333
{
3434
$type = $this->getContentType($url);
35-
$parts = explode('/', $type);
3635

37-
return end($parts);
36+
$mimeMap = [
37+
'application/pdf' => 'pdf',
38+
'application/msword' => 'doc',
39+
'application/vnd.openxmlformats-officedocument.wordprocessingml.document' => 'docx',
40+
'application/vnd.ms-excel' => 'xls',
41+
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' => 'xlsx',
42+
'text/plain' => 'txt',
43+
'text/csv' => 'csv',
44+
'text/html' => 'html',
45+
'application/json' => 'json',
46+
'application/xml' => 'xml',
47+
];
48+
49+
return $mimeMap[$type] ?? '';
3850
}
3951

4052
public function getContentType(string $url): string
@@ -57,7 +69,7 @@ protected function getHeaders(string $url): array
5769

5870
try {
5971
$response = get_headers($url, 1, $this->streamContext());
60-
} catch(Exception $e) {
72+
} catch (Exception $e) {
6173
return [];
6274
}
6375

0 commit comments

Comments
 (0)