Skip to content

Commit 2070c38

Browse files
committed
wip: add mimeIconProvider
Signed-off-by: John Molakvoæ <skjnldsv@protonmail.com>
1 parent db2a576 commit 2070c38

4 files changed

Lines changed: 95 additions & 0 deletions

File tree

core/Controller/PreviewController.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
use OCP\Files\NotFoundException;
3939
use OCP\IPreview;
4040
use OCP\IRequest;
41+
use OCP\Preview\IMimeIconProvider;
4142

4243
class PreviewController extends Controller {
4344
public function __construct(
@@ -46,6 +47,7 @@ public function __construct(
4647
private IPreview $preview,
4748
private IRootFolder $root,
4849
private ?string $userId,
50+
private IMimeIconProvider $mimeIconProvider,
4951
) {
5052
parent::__construct($appName, $request);
5153
}
@@ -76,6 +78,9 @@ public function getPreview(
7678
bool $a = false,
7779
bool $forceIcon = true,
7880
string $mode = 'fill'): Http\Response {
81+
// Debug mimeIconProvider
82+
$this->mimeIconProvider->getMimeIconUrl('image/png');
83+
7984
if ($file === '' || $x === 0 || $y === 0) {
8085
return new DataResponse([], Http::STATUS_BAD_REQUEST);
8186
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
/**
3+
* @copyright Copyright (c) 2023 John Molakvoæ <skjnldsv@protonmail.com>
4+
*
5+
* @author John Molakvoæ <skjnldsv@protonmail.com>
6+
*
7+
* @license AGPL-3.0-or-later
8+
*
9+
* This code is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU Affero General Public License, version 3,
11+
* as published by the Free Software Foundation.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* GNU Affero General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU Affero General Public License, version 3,
19+
* along with this program. If not, see <http://www.gnu.org/licenses/>
20+
*
21+
*/
22+
namespace OC\Preview;
23+
24+
use OCP\Files\IMimeTypeDetector;
25+
use OCP\Preview\IMimeIconProvider;
26+
27+
class MimeIconProvider implements IMimeIconProvider {
28+
29+
public function __construct(
30+
protected IMimeTypeDetector $mimetypeDetector,
31+
) {}
32+
33+
public function getMimeIconUrl(string $mime): string|null {
34+
if (!$mime) {
35+
return null;
36+
}
37+
38+
// Fetch all the aliases
39+
$aliases = $this->mimetypeDetector->getAllAliases();
40+
41+
// Remove comments
42+
$aliases = array_filter($aliases, static function ($key) {
43+
return !($key === '' || $key[0] === '_');
44+
}, ARRAY_FILTER_USE_KEY);
45+
46+
// Map all the aliases recursively
47+
foreach ($aliases as $alias) {
48+
if ($alias === $mime) {
49+
$mime = $alias;
50+
}
51+
}
52+
}
53+
}

lib/public/Files/IMimeTypeDetector.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,10 @@ public function detectString($data);
8282
* @since 8.2.0
8383
*/
8484
public function mimeTypeIcon($mimeType);
85+
86+
/**
87+
* @return string[]
88+
* @since 28.0.0
89+
*/
90+
public function getAllAliases(): array;
8591
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
/**
3+
* @copyright Copyright (c) 2023 John Molakvoæ <skjnldsv@protonmail.com>
4+
*
5+
* @author John Molakvoæ <skjnldsv@protonmail.com>
6+
*
7+
* @license AGPL-3.0-or-later
8+
*
9+
* This code is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU Affero General Public License, version 3,
11+
* as published by the Free Software Foundation.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* GNU Affero General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU Affero General Public License, version 3,
19+
* along with this program. If not, see <http://www.gnu.org/licenses/>
20+
*
21+
*/
22+
namespace OCP\Preview;
23+
24+
/**
25+
* Interface IMimeIconProvider
26+
*
27+
* @since 28.0.0
28+
*/
29+
interface IMimeIconProvider {
30+
public function getMimeIconUrl(string $mime): string|null;
31+
}

0 commit comments

Comments
 (0)