Skip to content

Commit 950869c

Browse files
committed
use mimeIcon is preview not available
Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
1 parent 82850dc commit 950869c

File tree

2 files changed

+33
-10
lines changed

2 files changed

+33
-10
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
/build
33
/node_modules
44
/vendor/
5-
5+
.idea/

lib/Dashboard/RecommendationWidget.php

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
<?php
2+
3+
declare(strict_types=1);
4+
25
/**
36
* @copyright Copyright (c) 2020 Julius Härtl <jus@bitgrid.net>
47
*
@@ -30,6 +33,7 @@
3033
use OCP\Dashboard\IIconWidget;
3134
use OCP\Dashboard\IWidget;
3235
use OCP\Dashboard\Model\WidgetItem;
36+
use OCP\Files\IMimeTypeDetector;
3337
use OCP\IL10N;
3438
use OCP\IURLGenerator;
3539
use OCP\IUserManager;
@@ -40,19 +44,22 @@ class RecommendationWidget implements IWidget, IIconWidget, IAPIWidget {
4044
private IUserSession $userSession;
4145
private IL10N $l10n;
4246
private IURLGenerator $urlGenerator;
47+
private IMimeTypeDetector $mimeTypeDetector;
4348
private RecommendationService $recommendationService;
4449
private IUserManager $userManager;
4550

4651
public function __construct(
4752
IUserSession $userSession,
4853
IL10N $l10n,
4954
IURLGenerator $urlGenerator,
55+
IMimeTypeDetector $mimeTypeDetector,
5056
RecommendationService $recommendationService,
5157
IUserManager $userManager
5258
) {
5359
$this->userSession = $userSession;
5460
$this->l10n = $l10n;
5561
$this->urlGenerator = $urlGenerator;
62+
$this->mimeTypeDetector = $mimeTypeDetector;
5663
$this->recommendationService = $recommendationService;
5764
$this->userManager = $userManager;
5865
}
@@ -96,15 +103,31 @@ public function getItems(string $userId, ?string $since = null, int $limit = 7):
96103
}
97104
$recommendations = $this->recommendationService->getRecommendations($user, $limit);
98105

99-
return array_map(function(IRecommendation $recommendation) {
100-
$url = $this->urlGenerator->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $recommendation->getNode()->getId()]);
101-
$icon = $this->urlGenerator->linkToRouteAbsolute('core.Preview.getPreviewByFileId', [
102-
'x' => 256,
103-
'y' => 256,
104-
'fileId' => $recommendation->getNode()->getId(),
105-
'c' => $recommendation->getNode()->getEtag(),
106-
]);
107-
return new WidgetItem($recommendation->getNode()->getName(), '', $url, $icon, $recommendation->getTimestamp());
106+
return array_map(function (IRecommendation $recommendation) {
107+
$url = $this->urlGenerator->linkToRouteAbsolute(
108+
'files.viewcontroller.showFile', ['fileid' => $recommendation->getNode()->getId()]
109+
);
110+
111+
if ($recommendation->hasPreview()) {
112+
$icon = $this->urlGenerator->linkToRouteAbsolute('core.Preview.getPreviewByFileId', [
113+
'x' => 256,
114+
'y' => 256,
115+
'fileId' => $recommendation->getNode()->getId(),
116+
'c' => $recommendation->getNode()->getEtag(),
117+
]);
118+
} else {
119+
$icon = $this->urlGenerator->getAbsoluteURL(
120+
$this->mimeTypeDetector->mimeTypeIcon($recommendation->getNode()->getMimetype())
121+
);
122+
}
123+
124+
return new WidgetItem(
125+
$recommendation->getNode()->getName(),
126+
'',
127+
$url,
128+
$icon,
129+
(string)$recommendation->getTimestamp()
130+
);
108131
}, $recommendations);
109132
}
110133
}

0 commit comments

Comments
 (0)