Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enhance Api::actionSearch #40

Merged
merged 3 commits into from
Feb 6, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 52 additions & 23 deletions controllers/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function behaviors()
'upload' => ['POST'],
'resolve-permissions' => ['POST'],
'change-permissions' => ['POST'],
'search' => ['GET']
'search' => ['GET', 'POST']
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is POST allowed for search? It actually doesn't change anything on the server.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, doesn't change anything. But using POST for such requests is valid, it would only be a problem the other way round, i.e. if we were to allow GET for non-idempotent requests.

By the way: for the list action, which also writes nothing, only POST is allowed ;-)

]
];
$behaviors['corsFilter'] = [
Expand Down Expand Up @@ -485,31 +485,17 @@ public function actionList($path)
$time = $fileSystem->getTimestamp($item['path']) ?: time();
}

$thumbnail = '';
if (is_callable($this->module->thumbnailCallback)) {
$thumbnail = call_user_func($this->module->thumbnailCallback, $item);
}

$itemUrls = [];
if (is_callable($this->module->urlCallback)) {
$itemUrls = call_user_func($this->module->urlCallback, $item);
}
$previewUrl = '';
if (is_callable($this->module->previewCallback)) {
$previewUrl = call_user_func($this->module->previewCallback, $item);
}

$files[] = [
'name' => $item['basename'],
'dirname' => $item['dirname'],
'path' => $item['path'],
'urls' => $itemUrls,
'thumbnail' => $thumbnail,
'preview' => $previewUrl,
'urls' => $this->getItemUrls($item),
'thumbnail' => $this->getItemThumbnail($item),
'preview' => $this->getItemPreview($item),
'size' => $size,
'date' => date('Y-m-d H:i:s', $time),
'type' => $item['type'],
'extension' => $item['extension'] ?? ''
'extension' => $item['extension'] ?? $this->getItemExtensionFromPath($item),
];
}
}
Expand Down Expand Up @@ -722,22 +708,26 @@ public function actionChangePermissions($path = null, $item = null)

/**
* @param string $q search string
* @param int $limit limit result
* @param null $limit limit result
* @param bool $only_files limit result to files, default true
* @param int $q_limit limit items in the db query that will be checked for read access, default 1000
* @param null $basePath optional basePath for the search, if set a `LIKE basePath%` condition will be added
*
* @return Response
*/
public function actionSearch($q, $limit = null, $only_files = true, $q_limit = 1000)
public function actionSearch($q, $limit = null, $only_files = true, $q_limit = 1000, $basePath = null)
{

$basePath = (!empty($basePath) && is_string($basePath)) ? $basePath . '%' : null;

// use default find() WITHOUT constraints from ActiveRecordAccessTrait here, as we MUST check permissions
// via $fileSystem->grantAccess() to get recursive read access checks
FileflyHashmap::$activeAccessTrait = false;
$query = FileflyHashmap::find()
->select(['path'])
->select(['path', 'type'])
->andWhere(['component' => $this->module->filesystem])
->andWhere(['LIKE', 'path', $q])
->andFilterWhere(['LIKE', 'path', $basePath, false])
->orderBy(['updated_at' => SORT_DESC])
->limit($q_limit ?: 1000)
->asArray();
Expand All @@ -755,9 +745,10 @@ public function actionSearch($q, $limit = null, $only_files = true, $q_limit = 1
if (!$fileSystem->grantAccess($item['path'], Filefly::ACCESS_READ)) {
continue;
}

try {
$item['extension'] = $this->getItemExtensionFromPath($item);
$item['id'] = $item['path'];
$item['thumbnail'] = $this->getItemThumbnail($item);
$item['mime'] = '';
$result[] = $item;
} catch (FileNotFoundException $e) {
Expand All @@ -774,4 +765,42 @@ public function actionSearch($q, $limit = null, $only_files = true, $q_limit = 1

return $this->asJson($result);
}


private function getItemThumbnail(array $item)
{
$thumbnail = '';
if (is_callable($this->module->thumbnailCallback)) {
$thumbnail = call_user_func($this->module->thumbnailCallback, $item);
}
return $thumbnail;
}

private function getItemPreview(array $item)
{
$previewUrl = '';
if (is_callable($this->module->previewCallback)) {
$previewUrl = call_user_func($this->module->previewCallback, $item);
}
return $previewUrl;
}

private function getItemUrls(array $item)
{
$itemUrls = [];
if (is_callable($this->module->urlCallback)) {
$itemUrls = call_user_func($this->module->urlCallback, $item);
}
return $itemUrls;
}

private function getItemExtensionFromPath(array $item)
{
$extension = '';
if (isset($item['path']) && $item['type'] === 'file') {
$extension = pathinfo($item['path'], PATHINFO_EXTENSION);
}
return $extension;
}

}