diff --git a/controllers/HomeController.php b/controllers/HomeController.php index d080fa4..3cf7c11 100644 --- a/controllers/HomeController.php +++ b/controllers/HomeController.php @@ -40,7 +40,7 @@ class HomeController extends Controller 'rules' => [ [ 'allow' => true, - 'actions' => ['index', 'download', 'preview', 'rename', 'delete', 'upload', 'new-folder', 'download-folder', 'multi-ff-zip-dl', 'zip', 'unzip', 'paste'], + 'actions' => ['index', 'download', 'preview', 'rename', 'delete', 'upload', 'new-folder', 'download-folder', 'multi-ff-zip-dl', 'zip', 'unzip', 'paste', 'search'], 'roles' => ['user'], ], ], @@ -60,6 +60,7 @@ class HomeController extends Controller 'zip' => ['POST'], //剩余空间检查√ 'unzip' => ['POST'], //剩余空间检查√ 'paste' => ['POST'], //剩余空间检查√ + 'search' => ['POST'] ], ], ] @@ -649,7 +650,7 @@ class HomeController extends Controller 'directory' => pathinfo($relativePath, PATHINFO_FILENAME) . '_' . $now_time, 'parentDirectory' => dirname($relativePath), ]; - }else{ + } else { Yii::$app->session->setFlash('success', '解压成功'); return [ 'status' => 200, @@ -877,4 +878,43 @@ class HomeController extends Controller 'sha256' => strtoupper($sha256), ]; } + + /** + * @throws NotFoundHttpException + */ + public function actionSearch(): Response + { + if (Yii::$app->request->isAjax) { + $directory = empty(Yii::$app->request->post('directory'))?'.':Yii::$app->request->post('directory'); + $keyword = Yii::$app->request->post('keyword'); + if ($keyword == null) { + return $this->asJson(['status' => 'error', 'message' => '关键词不能为空']); + } + if (strlen($keyword) < 3) { + return $this->asJson(['status' => 'error', 'message' => '关键词长度不得小于3']); + } + $absolutePath = Yii::getAlias(Yii::$app->params['dataDirectory']) . '/' . Yii::$app->user->id . '/' . $directory; + // check if the directory is valid & exists + if (!preg_match($this->pattern, $directory) || str_contains($directory, '..') || !is_dir($absolutePath)) { + return $this->asJson(['status' => 'error', 'message' => '无效路径']); + } + + $directoryIterator = new RecursiveDirectoryIterator($absolutePath); + $iterator = new RecursiveIteratorIterator($directoryIterator); + + $results = []; + foreach ($iterator as $file) { + if (str_contains($file->getFilename(), $keyword)) { + $results[] = [ + 'name' => $file->getFilename(), + 'type' => FileTypeDetector::detect($file->getPathname()), + 'relativePath' => $directory.str_replace($absolutePath, '', $file->getPath()) + ]; + } + } + + return $this->asJson(['status' => 'success', 'data' => $results]); + } + return $this->asJson(['status' => 'error', 'message' => '非法请求']); + } } diff --git a/views/home/index.php b/views/home/index.php index ab0128e..5f9dd42 100644 --- a/views/home/index.php +++ b/views/home/index.php @@ -13,6 +13,7 @@ use app\assets\AceAsset; use app\assets\PlyrAsset; use app\assets\ViewerJsAsset; use app\models\CollectionTasks; +use app\models\FileSearch; use app\models\NewFolderForm; use app\models\RenameForm; use app\models\Share; @@ -78,8 +79,7 @@ $this->registerCssFile('@web/css/home_style.css');
  • 'dropdown-item folder-upload-btn']) ?>
  • - - + + +
    + +
    +EOL; +Modal::end(); + Modal::begin([ 'title' => '

    视频播放

    ', 'id' => 'videoModal', diff --git a/web/js/home_script.js b/web/js/home_script.js index db89246..8fb2a01 100644 --- a/web/js/home_script.js +++ b/web/js/home_script.js @@ -670,4 +670,44 @@ $(document).on('click', '.create-collection-btn', function () { }); const popoverTriggerList = document.querySelectorAll('[data-bs-toggle="popover"]') -const popoverList = [...popoverTriggerList].map(popoverTriggerEl => new bootstrap.Popover(popoverTriggerEl)) \ No newline at end of file +const popoverList = [...popoverTriggerList].map(popoverTriggerEl => new bootstrap.Popover(popoverTriggerEl)) + +$(document).on('click', '#btnSearch', function () { + var keyword = $('#filesearch-keyword').val(); + var directory = $('#filesearch-directory').val(); + $('#search-result').html(''); + $.ajax({ + type: "POST", + url: "index.php?r=home%2Fsearch", + data: {keyword: keyword, directory: directory}, + success: function (response) { + if (response.status === 'success') { + var table = ''; + $.each(response.data, function(index, item) { + let path = item.relativePath; + let correctedPath = path.replace(/\\/g, '/'); + table += '' + ''; + }); + table += '
    文件/文件夹名位置
    '+item.name + '' + item.relativePath + '
    '; + $('#search-result').html(table); + } else if (response.status === 'error') { + $('#search-result').html(response.message); + } else { + $('#search-result').html('An error occurred while processing your request.'); + } + }, + error: function () { + $('#search-result').html('An error occurred while processing your request.'); + }, + beforeSend: function() { + $('#loading').show(); + }, + complete: function() { + $('#loading').hide(); + }, + }); +}); +$('#searchModal').on('hidden.bs.modal', function () { + $('#search-result').html(''); + $('#filesearch-keyword').val(''); +}); \ No newline at end of file