From d5b04d7fdb4ba4243ae5d3aafe44e3af2145a34e Mon Sep 17 00:00:00 2001 From: Chenx221 Date: Wed, 20 Mar 2024 16:26:03 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E6=96=87=E4=BB=B6/=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E5=A4=B9=E6=90=9C=E7=B4=A0=E5=8A=9F=E8=83=BD=20*?= =?UTF-8?q?=E4=B8=8D=E6=94=AF=E6=8C=81=E5=86=85=E5=AE=B9=E6=90=9C=E7=B4=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controllers/HomeController.php | 44 ++++++++++++++++++++++++++++++++-- views/home/index.php | 33 +++++++++++++++++++++++-- web/js/home_script.js | 42 +++++++++++++++++++++++++++++++- 3 files changed, 114 insertions(+), 5 deletions(-) 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