-
-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathFileSearchProvider.php
More file actions
140 lines (116 loc) · 3.37 KB
/
FileSearchProvider.php
File metadata and controls
140 lines (116 loc) · 3.37 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2025 LibreCode coop and contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Libresign\Search;
use OCA\Libresign\AppInfo\Application;
use OCA\Libresign\Db\File;
use OCA\Libresign\Db\SignRequestMapper;
use OCP\App\IAppManager;
use OCP\Files\IMimeTypeDetector;
use OCP\Files\IRootFolder;
use OCP\IL10N;
use OCP\IURLGenerator;
use OCP\IUser;
use OCP\Search\IProvider;
use OCP\Search\ISearchQuery;
use OCP\Search\SearchResult;
use OCP\Search\SearchResultEntry;
class FileSearchProvider implements IProvider {
public function __construct(
private IL10N $l10n,
private IURLGenerator $urlGenerator,
private IRootFolder $rootFolder,
private IAppManager $appManager,
private IMimeTypeDetector $mimeTypeDetector,
private SignRequestMapper $fileMapper,
) {
}
#[\Override]
public function getId(): string {
return 'libresign_files';
}
#[\Override]
public function getName(): string {
return $this->l10n->t('LibreSign documents');
}
#[\Override]
public function getOrder(string $route, array $routeParameters): int {
if (strpos($route, Application::APP_ID . '.') === 0) {
return 0;
}
return 10;
}
#[\Override]
public function search(IUser $user, ISearchQuery $query): SearchResult {
if (!$this->appManager->isEnabledForUser(Application::APP_ID, $user)) {
return SearchResult::complete($this->l10n->t('LibreSign documents'), []);
}
$term = $query->getTerm();
$limit = $query->getLimit();
$offset = $query->getCursor();
try {
$files = $this->fileMapper->getFilesToSearchProvider($user, $term, $limit, (int)$offset);
} catch (\Exception $e) {
return SearchResult::complete($this->l10n->t('LibreSign documents'), []);
}
$results = array_map(function (File $file) use ($user) {
return $this->formatResult($file, $user);
}, $files);
return SearchResult::paginated(
$this->l10n->t('LibreSign documents'),
$results,
$offset + $limit
);
}
private function formatResult(File $file): SearchResultEntry {
$userFolder = $this->rootFolder->getUserFolder($file->getUserId());
$thumbnailUrl = '';
$subline = '';
$icon = '';
$path = '';
try {
$nodes = $userFolder->getById($file->getNodeId());
if (!empty($nodes)) {
$node = array_shift($nodes);
$icon = $this->mimeTypeDetector->mimeTypeIcon($node->getMimetype());
$thumbnailUrl = $this->urlGenerator->linkToRouteAbsolute(
'core.Preview.getPreviewByFileId',
[
'x' => 32,
'y' => 32,
'fileId' => $node->getId()
]
);
$path = $userFolder->getRelativePath($node->getPath());
$subline = $this->formatSubline($path);
}
} catch (\Exception $e) {
}
$link = $this->urlGenerator->linkToRoute(
'files.View.showFile',
['fileid' => $file->getNodeId()]
);
$searchResultEntry = new SearchResultEntry(
$thumbnailUrl,
$file->getName(),
$subline,
$this->urlGenerator->getAbsoluteURL($link),
$icon,
);
$searchResultEntry->addAttribute('fileId', (string)$file->getNodeId());
$searchResultEntry->addAttribute('path', $path);
return $searchResultEntry;
}
private function formatSubline(string $path): string {
if (strrpos($path, '/') > 0) {
$path = ltrim(dirname($path), '/');
// TRANSLATORS This string indicates the location of a file in a given path.
return $this->l10n->t('in %s', [$path]);
} else {
return '';
}
}
}