Skip to content

Commit

Permalink
Merge pull request #40 from dmstr/dev/api-search
Browse files Browse the repository at this point in the history
enhance Api::actionSearch
  • Loading branch information
handcode authored Feb 6, 2025
2 parents 5cfde0d + 31f18dd commit 3e07803
Showing 1 changed file with 52 additions and 23 deletions.
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']
]
];
$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;
}

}

0 comments on commit 3e07803

Please sign in to comment.