2020-05-11 11:35:54 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @copyright 2020 Christoph Wurst <christoph@winzerhof-wurst.at>
|
|
|
|
*
|
2020-08-24 15:54:25 +03:00
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
|
|
|
* @author John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
|
2020-12-16 16:54:15 +03:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2020-05-11 11:35:54 +03:00
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* 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
|
2020-08-24 15:54:25 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
2020-05-11 11:35:54 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OCA\Files\Search;
|
|
|
|
|
2020-06-17 11:29:50 +03:00
|
|
|
use OC\Search\Provider\File;
|
|
|
|
use OC\Search\Result\File as FileResult;
|
2020-08-04 11:00:27 +03:00
|
|
|
use OCP\Files\IMimeTypeDetector;
|
2020-09-11 11:27:02 +03:00
|
|
|
use OCP\Files\IRootFolder;
|
2020-05-11 11:35:54 +03:00
|
|
|
use OCP\IL10N;
|
2020-06-17 11:29:50 +03:00
|
|
|
use OCP\IURLGenerator;
|
2020-05-11 11:35:54 +03:00
|
|
|
use OCP\IUser;
|
|
|
|
use OCP\Search\IProvider;
|
|
|
|
use OCP\Search\ISearchQuery;
|
|
|
|
use OCP\Search\SearchResult;
|
2020-08-04 11:00:27 +03:00
|
|
|
use OCP\Search\SearchResultEntry;
|
2020-05-11 11:35:54 +03:00
|
|
|
|
|
|
|
class FilesSearchProvider implements IProvider {
|
|
|
|
|
2020-06-17 11:29:50 +03:00
|
|
|
/** @var File */
|
|
|
|
private $fileSearch;
|
|
|
|
|
2020-05-11 11:35:54 +03:00
|
|
|
/** @var IL10N */
|
|
|
|
private $l10n;
|
|
|
|
|
2020-06-17 11:29:50 +03:00
|
|
|
/** @var IURLGenerator */
|
|
|
|
private $urlGenerator;
|
|
|
|
|
2020-08-04 11:00:27 +03:00
|
|
|
/** @var IMimeTypeDetector */
|
|
|
|
private $mimeTypeDetector;
|
|
|
|
|
2020-09-11 11:27:02 +03:00
|
|
|
/** @var IRootFolder */
|
|
|
|
private $rootFolder;
|
|
|
|
|
2020-06-17 11:29:50 +03:00
|
|
|
public function __construct(File $fileSearch,
|
|
|
|
IL10N $l10n,
|
2020-08-04 11:00:27 +03:00
|
|
|
IURLGenerator $urlGenerator,
|
2020-09-11 11:27:02 +03:00
|
|
|
IMimeTypeDetector $mimeTypeDetector,
|
|
|
|
IRootFolder $rootFolder) {
|
2020-05-11 11:35:54 +03:00
|
|
|
$this->l10n = $l10n;
|
2020-06-17 11:29:50 +03:00
|
|
|
$this->fileSearch = $fileSearch;
|
|
|
|
$this->urlGenerator = $urlGenerator;
|
2020-08-04 11:00:27 +03:00
|
|
|
$this->mimeTypeDetector = $mimeTypeDetector;
|
2020-09-11 11:27:02 +03:00
|
|
|
$this->rootFolder = $rootFolder;
|
2020-05-11 11:35:54 +03:00
|
|
|
}
|
|
|
|
|
2020-08-04 11:00:27 +03:00
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
2020-05-11 11:35:54 +03:00
|
|
|
public function getId(): string {
|
|
|
|
return 'files';
|
|
|
|
}
|
|
|
|
|
2020-08-04 11:00:27 +03:00
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
2020-08-03 13:54:37 +03:00
|
|
|
public function getName(): string {
|
|
|
|
return $this->l10n->t('Files');
|
|
|
|
}
|
|
|
|
|
2020-08-04 11:00:27 +03:00
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
2020-08-05 10:49:18 +03:00
|
|
|
public function getOrder(string $route, array $routeParameters): int {
|
|
|
|
if ($route === 'files.View.index') {
|
|
|
|
// Before comments
|
|
|
|
return -5;
|
|
|
|
}
|
2020-08-04 11:00:27 +03:00
|
|
|
return 5;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
2020-05-11 11:35:54 +03:00
|
|
|
public function search(IUser $user, ISearchQuery $query): SearchResult {
|
2020-09-11 11:27:02 +03:00
|
|
|
|
|
|
|
// Make sure we setup the users filesystem
|
|
|
|
$this->rootFolder->getUserFolder($user->getUID());
|
|
|
|
|
2020-05-11 11:35:54 +03:00
|
|
|
return SearchResult::complete(
|
|
|
|
$this->l10n->t('Files'),
|
2020-06-17 11:29:50 +03:00
|
|
|
array_map(function (FileResult $result) {
|
2020-08-03 13:54:37 +03:00
|
|
|
// Generate thumbnail url
|
2020-08-04 11:00:27 +03:00
|
|
|
$thumbnailUrl = $result->has_preview
|
2020-09-15 10:20:10 +03:00
|
|
|
? $this->urlGenerator->linkToRouteAbsolute('core.Preview.getPreviewByFileId', ['x' => 32, 'y' => 32, 'fileId' => $result->id])
|
2020-08-04 11:00:27 +03:00
|
|
|
: '';
|
2020-08-03 13:54:37 +03:00
|
|
|
|
2020-12-02 16:26:48 +03:00
|
|
|
$searchResultEntry = new SearchResultEntry(
|
2020-08-03 13:54:37 +03:00
|
|
|
$thumbnailUrl,
|
2020-06-17 11:29:50 +03:00
|
|
|
$result->name,
|
2020-08-03 13:54:37 +03:00
|
|
|
$this->formatSubline($result),
|
2020-09-15 10:20:10 +03:00
|
|
|
$this->urlGenerator->getAbsoluteURL($result->link),
|
2020-08-04 11:00:27 +03:00
|
|
|
$result->type === 'folder' ? 'icon-folder' : $this->mimeTypeDetector->mimeTypeIcon($result->mime_type)
|
2020-06-17 11:29:50 +03:00
|
|
|
);
|
2020-12-02 16:26:48 +03:00
|
|
|
$searchResultEntry->addAttribute('fileId', (string)$result->id);
|
|
|
|
$searchResultEntry->addAttribute('path', $result->path);
|
|
|
|
return $searchResultEntry;
|
2020-06-17 11:29:50 +03:00
|
|
|
}, $this->fileSearch->search($query->getTerm()))
|
2020-05-11 11:35:54 +03:00
|
|
|
);
|
|
|
|
}
|
2020-08-03 13:54:37 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Format subline for files
|
|
|
|
*
|
|
|
|
* @param FileResult $result
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private function formatSubline($result): string {
|
|
|
|
// Do not show the location if the file is in root
|
|
|
|
if ($result->path === '/' . $result->name) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
$path = ltrim(dirname($result->path), '/');
|
|
|
|
return $this->l10n->t('in %s', [$path]);
|
|
|
|
}
|
2020-05-11 11:35:54 +03:00
|
|
|
}
|