Skip to content

Commit

Permalink
wip: add mimeIconProvider
Browse files Browse the repository at this point in the history
Signed-off-by: John Molakvoæ <skjnldsv@protonmail.com>
  • Loading branch information
skjnldsv committed Aug 11, 2023
1 parent db2a576 commit 2070c38
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 0 deletions.
5 changes: 5 additions & 0 deletions core/Controller/PreviewController.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
use OCP\Files\NotFoundException;
use OCP\IPreview;
use OCP\IRequest;
use OCP\Preview\IMimeIconProvider;

class PreviewController extends Controller {
public function __construct(
Expand All @@ -46,6 +47,7 @@ public function __construct(
private IPreview $preview,
private IRootFolder $root,
private ?string $userId,
private IMimeIconProvider $mimeIconProvider,
) {
parent::__construct($appName, $request);
}
Expand Down Expand Up @@ -76,6 +78,9 @@ public function getPreview(
bool $a = false,
bool $forceIcon = true,
string $mode = 'fill'): Http\Response {
// Debug mimeIconProvider
$this->mimeIconProvider->getMimeIconUrl('image/png');

if ($file === '' || $x === 0 || $y === 0) {
return new DataResponse([], Http::STATUS_BAD_REQUEST);
}
Expand Down
53 changes: 53 additions & 0 deletions lib/private/Preview/MimeIconProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php
/**
* @copyright Copyright (c) 2023 John Molakvoæ <skjnldsv@protonmail.com>
*
* @author John Molakvoæ <skjnldsv@protonmail.com>
*
* @license AGPL-3.0-or-later
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
namespace OC\Preview;

use OCP\Files\IMimeTypeDetector;
use OCP\Preview\IMimeIconProvider;

class MimeIconProvider implements IMimeIconProvider {

public function __construct(
protected IMimeTypeDetector $mimetypeDetector,
) {}

public function getMimeIconUrl(string $mime): string|null {
if (!$mime) {
return null;
}

// Fetch all the aliases
$aliases = $this->mimetypeDetector->getAllAliases();

// Remove comments
$aliases = array_filter($aliases, static function ($key) {
return !($key === '' || $key[0] === '_');
}, ARRAY_FILTER_USE_KEY);

// Map all the aliases recursively
foreach ($aliases as $alias) {
if ($alias === $mime) {
$mime = $alias;
}
}
}
}
6 changes: 6 additions & 0 deletions lib/public/Files/IMimeTypeDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,10 @@ public function detectString($data);
* @since 8.2.0
*/
public function mimeTypeIcon($mimeType);

/**
* @return string[]
* @since 28.0.0
*/
public function getAllAliases(): array;
}
31 changes: 31 additions & 0 deletions lib/public/Preview/IMimeIconProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
/**
* @copyright Copyright (c) 2023 John Molakvoæ <skjnldsv@protonmail.com>
*
* @author John Molakvoæ <skjnldsv@protonmail.com>
*
* @license AGPL-3.0-or-later
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
namespace OCP\Preview;

/**
* Interface IMimeIconProvider
*
* @since 28.0.0
*/
interface IMimeIconProvider {
public function getMimeIconUrl(string $mime): string|null;
}

0 comments on commit 2070c38

Please sign in to comment.