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-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;
|
|
|
|
|
2021-03-18 18:22:29 +03:00
|
|
|
use OC\Files\Search\SearchComparison;
|
2021-03-19 16:29:08 +03:00
|
|
|
use OC\Files\Search\SearchOrder;
|
2021-03-18 18:22:29 +03:00
|
|
|
use OC\Files\Search\SearchQuery;
|
|
|
|
use OCP\Files\FileInfo;
|
2020-08-04 11:00:27 +03:00
|
|
|
use OCP\Files\IMimeTypeDetector;
|
2020-09-11 11:27:02 +03:00
|
|
|
use OCP\Files\IRootFolder;
|
2021-03-18 18:22:29 +03:00
|
|
|
use OCP\Files\Search\ISearchComparison;
|
|
|
|
use OCP\Files\Node;
|
2021-03-19 16:29:08 +03:00
|
|
|
use OCP\Files\Search\ISearchOrder;
|
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 {
|
|
|
|
|
|
|
|
/** @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;
|
|
|
|
|
2021-03-18 18:22:29 +03:00
|
|
|
public function __construct(
|
|
|
|
IL10N $l10n,
|
|
|
|
IURLGenerator $urlGenerator,
|
|
|
|
IMimeTypeDetector $mimeTypeDetector,
|
|
|
|
IRootFolder $rootFolder
|
|
|
|
) {
|
2020-05-11 11:35:54 +03:00
|
|
|
$this->l10n = $l10n;
|
2020-06-17 11:29:50 +03:00
|
|
|
$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 {
|
2021-03-18 18:22:29 +03:00
|
|
|
$userFolder = $this->rootFolder->getUserFolder($user->getUID());
|
|
|
|
$fileQuery = new SearchQuery(
|
|
|
|
new SearchComparison(ISearchComparison::COMPARE_LIKE, 'name', '%' . $query->getTerm() . '%'),
|
|
|
|
$query->getLimit(),
|
|
|
|
(int)$query->getCursor(),
|
2021-03-19 16:29:08 +03:00
|
|
|
$query->getSortOrder() === ISearchQuery::SORT_DATE_DESC ? [
|
|
|
|
new SearchOrder(ISearchOrder::DIRECTION_DESCENDING, 'mtime'),
|
|
|
|
] : [],
|
2021-03-18 18:22:29 +03:00
|
|
|
$user
|
|
|
|
);
|
2020-09-11 11:27:02 +03:00
|
|
|
|
2021-03-10 20:46:18 +03:00
|
|
|
return SearchResult::paginated(
|
2020-05-11 11:35:54 +03:00
|
|
|
$this->l10n->t('Files'),
|
2021-03-18 18:22:29 +03:00
|
|
|
array_map(function (Node $result) use ($userFolder) {
|
2020-08-03 13:54:37 +03:00
|
|
|
// Generate thumbnail url
|
2021-03-18 18:22:29 +03:00
|
|
|
$thumbnailUrl = $this->urlGenerator->linkToRouteAbsolute('core.Preview.getPreviewByFileId', ['x' => 32, 'y' => 32, 'fileId' => $result->getId()]);
|
|
|
|
$path = $userFolder->getRelativePath($result->getPath());
|
|
|
|
$link = $this->urlGenerator->linkToRoute(
|
|
|
|
'files.view.index',
|
|
|
|
[
|
|
|
|
'dir' => dirname($path),
|
|
|
|
'scrollto' => $result->getName(),
|
|
|
|
]
|
|
|
|
);
|
2020-08-03 13:54:37 +03:00
|
|
|
|
2020-08-04 11:00:27 +03:00
|
|
|
return new SearchResultEntry(
|
2020-08-03 13:54:37 +03:00
|
|
|
$thumbnailUrl,
|
2021-03-18 18:22:29 +03:00
|
|
|
$result->getName(),
|
|
|
|
$this->formatSubline($path),
|
|
|
|
$this->urlGenerator->getAbsoluteURL($link),
|
|
|
|
$result->getMimetype() === FileInfo::MIMETYPE_FOLDER ? 'icon-folder' : $this->mimeTypeDetector->mimeTypeIcon($result->getMimetype())
|
2020-06-17 11:29:50 +03:00
|
|
|
);
|
2021-03-18 18:22:29 +03:00
|
|
|
}, $userFolder->search($fileQuery)),
|
2021-03-10 20:46:18 +03:00
|
|
|
$query->getCursor() + $query->getLimit()
|
2020-05-11 11:35:54 +03:00
|
|
|
);
|
|
|
|
}
|
2020-08-03 13:54:37 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Format subline for files
|
|
|
|
*
|
2021-03-18 18:22:29 +03:00
|
|
|
* @param string $path
|
2020-08-03 13:54:37 +03:00
|
|
|
* @return string
|
|
|
|
*/
|
2021-03-18 18:22:29 +03:00
|
|
|
private function formatSubline(string $path): string {
|
2020-08-03 13:54:37 +03:00
|
|
|
// Do not show the location if the file is in root
|
2021-03-18 18:22:29 +03:00
|
|
|
if (strrpos($path, '/') > 0) {
|
|
|
|
$path = ltrim(dirname($path), '/');
|
|
|
|
return $this->l10n->t('in %s', [$path]);
|
|
|
|
} else {
|
2020-08-03 13:54:37 +03:00
|
|
|
return '';
|
|
|
|
}
|
|
|
|
}
|
2020-05-11 11:35:54 +03:00
|
|
|
}
|