@@ -48,112 +48,112 @@ |
||
48 | 48 | |
49 | 49 | class FilesSearchProvider implements IProvider { |
50 | 50 | |
51 | - /** @var IL10N */ |
|
52 | - private $l10n; |
|
53 | - |
|
54 | - /** @var IURLGenerator */ |
|
55 | - private $urlGenerator; |
|
56 | - |
|
57 | - /** @var IMimeTypeDetector */ |
|
58 | - private $mimeTypeDetector; |
|
59 | - |
|
60 | - /** @var IRootFolder */ |
|
61 | - private $rootFolder; |
|
62 | - |
|
63 | - public function __construct( |
|
64 | - IL10N $l10n, |
|
65 | - IURLGenerator $urlGenerator, |
|
66 | - IMimeTypeDetector $mimeTypeDetector, |
|
67 | - IRootFolder $rootFolder |
|
68 | - ) { |
|
69 | - $this->l10n = $l10n; |
|
70 | - $this->urlGenerator = $urlGenerator; |
|
71 | - $this->mimeTypeDetector = $mimeTypeDetector; |
|
72 | - $this->rootFolder = $rootFolder; |
|
73 | - } |
|
74 | - |
|
75 | - /** |
|
76 | - * @inheritDoc |
|
77 | - */ |
|
78 | - public function getId(): string { |
|
79 | - return 'files'; |
|
80 | - } |
|
81 | - |
|
82 | - /** |
|
83 | - * @inheritDoc |
|
84 | - */ |
|
85 | - public function getName(): string { |
|
86 | - return $this->l10n->t('Files'); |
|
87 | - } |
|
88 | - |
|
89 | - /** |
|
90 | - * @inheritDoc |
|
91 | - */ |
|
92 | - public function getOrder(string $route, array $routeParameters): int { |
|
93 | - if ($route === 'files.View.index') { |
|
94 | - // Before comments |
|
95 | - return -5; |
|
96 | - } |
|
97 | - return 5; |
|
98 | - } |
|
99 | - |
|
100 | - /** |
|
101 | - * @inheritDoc |
|
102 | - */ |
|
103 | - public function search(IUser $user, ISearchQuery $query): SearchResult { |
|
104 | - $userFolder = $this->rootFolder->getUserFolder($user->getUID()); |
|
105 | - $fileQuery = new SearchQuery( |
|
106 | - new SearchComparison(ISearchComparison::COMPARE_LIKE, 'name', '%' . $query->getTerm() . '%'), |
|
107 | - $query->getLimit(), |
|
108 | - (int)$query->getCursor(), |
|
109 | - $query->getSortOrder() === ISearchQuery::SORT_DATE_DESC ? [ |
|
110 | - new SearchOrder(ISearchOrder::DIRECTION_DESCENDING, 'mtime'), |
|
111 | - ] : [], |
|
112 | - $user |
|
113 | - ); |
|
114 | - |
|
115 | - return SearchResult::paginated( |
|
116 | - $this->l10n->t('Files'), |
|
117 | - array_map(function (Node $result) use ($userFolder) { |
|
118 | - // Generate thumbnail url |
|
119 | - $thumbnailUrl = $this->urlGenerator->linkToRouteAbsolute('core.Preview.getPreviewByFileId', ['x' => 32, 'y' => 32, 'fileId' => $result->getId()]); |
|
120 | - $path = $userFolder->getRelativePath($result->getPath()); |
|
121 | - |
|
122 | - // Use shortened link to centralize the various |
|
123 | - // files/folder url redirection in files.View.showFile |
|
124 | - $link = $this->urlGenerator->linkToRoute( |
|
125 | - 'files.View.showFile', |
|
126 | - ['fileid' => $result->getId()] |
|
127 | - ); |
|
128 | - |
|
129 | - $searchResultEntry = new SearchResultEntry( |
|
130 | - $thumbnailUrl, |
|
131 | - $result->getName(), |
|
132 | - $this->formatSubline($path), |
|
133 | - $this->urlGenerator->getAbsoluteURL($link), |
|
134 | - $result->getMimetype() === FileInfo::MIMETYPE_FOLDER ? 'icon-folder' : $this->mimeTypeDetector->mimeTypeIcon($result->getMimetype()) |
|
135 | - ); |
|
136 | - $searchResultEntry->addAttribute('fileId', (string)$result->getId()); |
|
137 | - $searchResultEntry->addAttribute('path', $path); |
|
138 | - return $searchResultEntry; |
|
139 | - }, $userFolder->search($fileQuery)), |
|
140 | - $query->getCursor() + $query->getLimit() |
|
141 | - ); |
|
142 | - } |
|
143 | - |
|
144 | - /** |
|
145 | - * Format subline for files |
|
146 | - * |
|
147 | - * @param string $path |
|
148 | - * @return string |
|
149 | - */ |
|
150 | - private function formatSubline(string $path): string { |
|
151 | - // Do not show the location if the file is in root |
|
152 | - if (strrpos($path, '/') > 0) { |
|
153 | - $path = ltrim(dirname($path), '/'); |
|
154 | - return $this->l10n->t('in %s', [$path]); |
|
155 | - } else { |
|
156 | - return ''; |
|
157 | - } |
|
158 | - } |
|
51 | + /** @var IL10N */ |
|
52 | + private $l10n; |
|
53 | + |
|
54 | + /** @var IURLGenerator */ |
|
55 | + private $urlGenerator; |
|
56 | + |
|
57 | + /** @var IMimeTypeDetector */ |
|
58 | + private $mimeTypeDetector; |
|
59 | + |
|
60 | + /** @var IRootFolder */ |
|
61 | + private $rootFolder; |
|
62 | + |
|
63 | + public function __construct( |
|
64 | + IL10N $l10n, |
|
65 | + IURLGenerator $urlGenerator, |
|
66 | + IMimeTypeDetector $mimeTypeDetector, |
|
67 | + IRootFolder $rootFolder |
|
68 | + ) { |
|
69 | + $this->l10n = $l10n; |
|
70 | + $this->urlGenerator = $urlGenerator; |
|
71 | + $this->mimeTypeDetector = $mimeTypeDetector; |
|
72 | + $this->rootFolder = $rootFolder; |
|
73 | + } |
|
74 | + |
|
75 | + /** |
|
76 | + * @inheritDoc |
|
77 | + */ |
|
78 | + public function getId(): string { |
|
79 | + return 'files'; |
|
80 | + } |
|
81 | + |
|
82 | + /** |
|
83 | + * @inheritDoc |
|
84 | + */ |
|
85 | + public function getName(): string { |
|
86 | + return $this->l10n->t('Files'); |
|
87 | + } |
|
88 | + |
|
89 | + /** |
|
90 | + * @inheritDoc |
|
91 | + */ |
|
92 | + public function getOrder(string $route, array $routeParameters): int { |
|
93 | + if ($route === 'files.View.index') { |
|
94 | + // Before comments |
|
95 | + return -5; |
|
96 | + } |
|
97 | + return 5; |
|
98 | + } |
|
99 | + |
|
100 | + /** |
|
101 | + * @inheritDoc |
|
102 | + */ |
|
103 | + public function search(IUser $user, ISearchQuery $query): SearchResult { |
|
104 | + $userFolder = $this->rootFolder->getUserFolder($user->getUID()); |
|
105 | + $fileQuery = new SearchQuery( |
|
106 | + new SearchComparison(ISearchComparison::COMPARE_LIKE, 'name', '%' . $query->getTerm() . '%'), |
|
107 | + $query->getLimit(), |
|
108 | + (int)$query->getCursor(), |
|
109 | + $query->getSortOrder() === ISearchQuery::SORT_DATE_DESC ? [ |
|
110 | + new SearchOrder(ISearchOrder::DIRECTION_DESCENDING, 'mtime'), |
|
111 | + ] : [], |
|
112 | + $user |
|
113 | + ); |
|
114 | + |
|
115 | + return SearchResult::paginated( |
|
116 | + $this->l10n->t('Files'), |
|
117 | + array_map(function (Node $result) use ($userFolder) { |
|
118 | + // Generate thumbnail url |
|
119 | + $thumbnailUrl = $this->urlGenerator->linkToRouteAbsolute('core.Preview.getPreviewByFileId', ['x' => 32, 'y' => 32, 'fileId' => $result->getId()]); |
|
120 | + $path = $userFolder->getRelativePath($result->getPath()); |
|
121 | + |
|
122 | + // Use shortened link to centralize the various |
|
123 | + // files/folder url redirection in files.View.showFile |
|
124 | + $link = $this->urlGenerator->linkToRoute( |
|
125 | + 'files.View.showFile', |
|
126 | + ['fileid' => $result->getId()] |
|
127 | + ); |
|
128 | + |
|
129 | + $searchResultEntry = new SearchResultEntry( |
|
130 | + $thumbnailUrl, |
|
131 | + $result->getName(), |
|
132 | + $this->formatSubline($path), |
|
133 | + $this->urlGenerator->getAbsoluteURL($link), |
|
134 | + $result->getMimetype() === FileInfo::MIMETYPE_FOLDER ? 'icon-folder' : $this->mimeTypeDetector->mimeTypeIcon($result->getMimetype()) |
|
135 | + ); |
|
136 | + $searchResultEntry->addAttribute('fileId', (string)$result->getId()); |
|
137 | + $searchResultEntry->addAttribute('path', $path); |
|
138 | + return $searchResultEntry; |
|
139 | + }, $userFolder->search($fileQuery)), |
|
140 | + $query->getCursor() + $query->getLimit() |
|
141 | + ); |
|
142 | + } |
|
143 | + |
|
144 | + /** |
|
145 | + * Format subline for files |
|
146 | + * |
|
147 | + * @param string $path |
|
148 | + * @return string |
|
149 | + */ |
|
150 | + private function formatSubline(string $path): string { |
|
151 | + // Do not show the location if the file is in root |
|
152 | + if (strrpos($path, '/') > 0) { |
|
153 | + $path = ltrim(dirname($path), '/'); |
|
154 | + return $this->l10n->t('in %s', [$path]); |
|
155 | + } else { |
|
156 | + return ''; |
|
157 | + } |
|
158 | + } |
|
159 | 159 | } |