|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * SPDX-FileCopyrightText: 2025 LibreCode coop and contributors |
| 7 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 8 | + */ |
| 9 | + |
| 10 | +namespace OCA\Libresign\Search; |
| 11 | + |
| 12 | +use OCA\Libresign\AppInfo\Application; |
| 13 | +use OCA\Libresign\Db\File; |
| 14 | +use OCA\Libresign\Db\SignRequestMapper; |
| 15 | +use OCP\App\IAppManager; |
| 16 | +use OCP\Files\IMimeTypeDetector; |
| 17 | +use OCP\Files\IRootFolder; |
| 18 | +use OCP\IL10N; |
| 19 | +use OCP\IURLGenerator; |
| 20 | +use OCP\IUser; |
| 21 | +use OCP\Search\IProvider; |
| 22 | +use OCP\Search\ISearchQuery; |
| 23 | +use OCP\Search\SearchResult; |
| 24 | +use OCP\Search\SearchResultEntry; |
| 25 | + |
| 26 | +class FileSearchProvider implements IProvider { |
| 27 | + public function __construct( |
| 28 | + private IL10N $l10n, |
| 29 | + private IURLGenerator $urlGenerator, |
| 30 | + private IRootFolder $rootFolder, |
| 31 | + private IAppManager $appManager, |
| 32 | + private IMimeTypeDetector $mimeTypeDetector, |
| 33 | + private SignRequestMapper $fileMapper, |
| 34 | + ) { |
| 35 | + } |
| 36 | + |
| 37 | + #[\Override] |
| 38 | + public function getId(): string { |
| 39 | + return 'libresign_files'; |
| 40 | + } |
| 41 | + |
| 42 | + #[\Override] |
| 43 | + public function getName(): string { |
| 44 | + return $this->l10n->t('LibreSign documents'); |
| 45 | + } |
| 46 | + |
| 47 | + #[\Override] |
| 48 | + public function getOrder(string $route, array $routeParameters): int { |
| 49 | + if (strpos($route, Application::APP_ID . '.') === 0) { |
| 50 | + return 0; |
| 51 | + } |
| 52 | + return 10; |
| 53 | + } |
| 54 | + |
| 55 | + #[\Override] |
| 56 | + public function search(IUser $user, ISearchQuery $query): SearchResult { |
| 57 | + if (!$this->appManager->isEnabledForUser(Application::APP_ID, $user)) { |
| 58 | + return SearchResult::complete($this->l10n->t('LibreSign documents'), []); |
| 59 | + } |
| 60 | + |
| 61 | + $term = $query->getTerm(); |
| 62 | + $limit = $query->getLimit(); |
| 63 | + $offset = $query->getCursor(); |
| 64 | + |
| 65 | + try { |
| 66 | + $files = $this->fileMapper->getFilesToSearchProvider($user, $term, $limit, (int)$offset); |
| 67 | + } catch (\Exception $e) { |
| 68 | + return SearchResult::complete($this->l10n->t('LibreSign documents'), []); |
| 69 | + } |
| 70 | + |
| 71 | + $results = array_map(function (File $file) use ($user) { |
| 72 | + return $this->formatResult($file, $user); |
| 73 | + }, $files); |
| 74 | + |
| 75 | + return SearchResult::paginated( |
| 76 | + $this->l10n->t('LibreSign documents'), |
| 77 | + $results, |
| 78 | + $offset + $limit |
| 79 | + ); |
| 80 | + } |
| 81 | + |
| 82 | + private function formatResult(File $file): SearchResultEntry { |
| 83 | + $userFolder = $this->rootFolder->getUserFolder($file->getUserId()); |
| 84 | + $thumbnailUrl = ''; |
| 85 | + $subline = ''; |
| 86 | + $icon = ''; |
| 87 | + $path = ''; |
| 88 | + |
| 89 | + try { |
| 90 | + $nodes = $userFolder->getById($file->getNodeId()); |
| 91 | + if (!empty($nodes)) { |
| 92 | + $node = array_shift($nodes); |
| 93 | + |
| 94 | + $icon = $this->mimeTypeDetector->mimeTypeIcon($node->getMimetype()); |
| 95 | + |
| 96 | + $thumbnailUrl = $this->urlGenerator->linkToRouteAbsolute( |
| 97 | + 'core.Preview.getPreviewByFileId', |
| 98 | + [ |
| 99 | + 'x' => 32, |
| 100 | + 'y' => 32, |
| 101 | + 'fileId' => $node->getId() |
| 102 | + ] |
| 103 | + ); |
| 104 | + |
| 105 | + $path = $userFolder->getRelativePath($node->getPath()); |
| 106 | + $subline = $this->formatSubline($path); |
| 107 | + } |
| 108 | + } catch (\Exception $e) { |
| 109 | + } |
| 110 | + |
| 111 | + $link = $this->urlGenerator->linkToRoute( |
| 112 | + 'files.View.showFile', |
| 113 | + ['fileid' => $file->getNodeId()] |
| 114 | + ); |
| 115 | + |
| 116 | + $searchResultEntry = new SearchResultEntry( |
| 117 | + $thumbnailUrl, |
| 118 | + $file->getName(), |
| 119 | + $subline, |
| 120 | + $this->urlGenerator->getAbsoluteURL($link), |
| 121 | + $icon, |
| 122 | + ); |
| 123 | + |
| 124 | + $searchResultEntry->addAttribute('fileId', (string)$file->getNodeId()); |
| 125 | + $searchResultEntry->addAttribute('path', $path); |
| 126 | + |
| 127 | + return $searchResultEntry; |
| 128 | + } |
| 129 | + |
| 130 | + private function formatSubline(string $path): string { |
| 131 | + if (strrpos($path, '/') > 0) { |
| 132 | + $path = ltrim(dirname($path), '/'); |
| 133 | + // TRANSLATORS This string indicates the location of a file in a given path. |
| 134 | + return $this->l10n->t('in %s', [$path]); |
| 135 | + } else { |
| 136 | + return ''; |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | +} |
0 commit comments