实现文件/文件夹搜素功能
*不支持内容搜素
This commit is contained in:
parent
07caef2555
commit
d5b04d7fdb
@ -40,7 +40,7 @@ class HomeController extends Controller
|
|||||||
'rules' => [
|
'rules' => [
|
||||||
[
|
[
|
||||||
'allow' => true,
|
'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'],
|
'roles' => ['user'],
|
||||||
],
|
],
|
||||||
],
|
],
|
||||||
@ -60,6 +60,7 @@ class HomeController extends Controller
|
|||||||
'zip' => ['POST'], //剩余空间检查√
|
'zip' => ['POST'], //剩余空间检查√
|
||||||
'unzip' => ['POST'], //剩余空间检查√
|
'unzip' => ['POST'], //剩余空间检查√
|
||||||
'paste' => ['POST'], //剩余空间检查√
|
'paste' => ['POST'], //剩余空间检查√
|
||||||
|
'search' => ['POST']
|
||||||
],
|
],
|
||||||
],
|
],
|
||||||
]
|
]
|
||||||
@ -649,7 +650,7 @@ class HomeController extends Controller
|
|||||||
'directory' => pathinfo($relativePath, PATHINFO_FILENAME) . '_' . $now_time,
|
'directory' => pathinfo($relativePath, PATHINFO_FILENAME) . '_' . $now_time,
|
||||||
'parentDirectory' => dirname($relativePath),
|
'parentDirectory' => dirname($relativePath),
|
||||||
];
|
];
|
||||||
}else{
|
} else {
|
||||||
Yii::$app->session->setFlash('success', '解压成功');
|
Yii::$app->session->setFlash('success', '解压成功');
|
||||||
return [
|
return [
|
||||||
'status' => 200,
|
'status' => 200,
|
||||||
@ -877,4 +878,43 @@ class HomeController extends Controller
|
|||||||
'sha256' => strtoupper($sha256),
|
'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' => '非法请求']);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -13,6 +13,7 @@ use app\assets\AceAsset;
|
|||||||
use app\assets\PlyrAsset;
|
use app\assets\PlyrAsset;
|
||||||
use app\assets\ViewerJsAsset;
|
use app\assets\ViewerJsAsset;
|
||||||
use app\models\CollectionTasks;
|
use app\models\CollectionTasks;
|
||||||
|
use app\models\FileSearch;
|
||||||
use app\models\NewFolderForm;
|
use app\models\NewFolderForm;
|
||||||
use app\models\RenameForm;
|
use app\models\RenameForm;
|
||||||
use app\models\Share;
|
use app\models\Share;
|
||||||
@ -78,8 +79,7 @@ $this->registerCssFile('@web/css/home_style.css');
|
|||||||
<li><?= Html::button('上传文件夹', ['class' => 'dropdown-item folder-upload-btn']) ?></li>
|
<li><?= Html::button('上传文件夹', ['class' => 'dropdown-item folder-upload-btn']) ?></li>
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
<!--TODO:文件搜索功能-->
|
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#searchModal">
|
||||||
<button type="button" class="btn btn-primary">
|
|
||||||
<i class="fa-solid fa-magnifying-glass"></i>
|
<i class="fa-solid fa-magnifying-glass"></i>
|
||||||
</button>
|
</button>
|
||||||
<button type="button" class="btn btn-primary" data-bs-toggle="popover" data-bs-title="容量使用情况"
|
<button type="button" class="btn btn-primary" data-bs-toggle="popover" data-bs-title="容量使用情况"
|
||||||
@ -307,6 +307,35 @@ echo Html::submitButton('提交', ['class' => 'btn btn-primary']);
|
|||||||
ActiveForm::end();
|
ActiveForm::end();
|
||||||
Modal::end();
|
Modal::end();
|
||||||
|
|
||||||
|
Modal::begin([
|
||||||
|
'title' => '<h4>搜素</h4>',
|
||||||
|
'id' => 'searchModal',
|
||||||
|
'size' => 'modal-xl',
|
||||||
|
]);
|
||||||
|
echo <<<EOL
|
||||||
|
<div class="input-group mb-3">
|
||||||
|
<input type="text" id="filesearch-keyword" class="form-control" name="keyword" value="" minlength="3" placeholder="在这里输入搜素关键词(长度要求不小于3位)" aria-label="keyword" aria-required="true" required>
|
||||||
|
<input type="hidden" id="filesearch-directory" class="form-control" name="directory" value="$directory" readonly>
|
||||||
|
<button class="btn btn-outline-primary" type="button" id="btnSearch">搜素</button>
|
||||||
|
</div>
|
||||||
|
<div id="loading" style="display: none">
|
||||||
|
<h5 class="card-title placeholder-glow">
|
||||||
|
<span class="placeholder col-8"></span>
|
||||||
|
</h5>
|
||||||
|
<p class="card-text placeholder-glow">
|
||||||
|
<span class="placeholder col-7"></span>
|
||||||
|
<span class="placeholder col-4"></span>
|
||||||
|
<span class="placeholder col-4"></span>
|
||||||
|
<span class="placeholder col-6"></span>
|
||||||
|
<span class="placeholder col-8"></span>
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
<div id="search-result">
|
||||||
|
|
||||||
|
</div>
|
||||||
|
EOL;
|
||||||
|
Modal::end();
|
||||||
|
|
||||||
Modal::begin([
|
Modal::begin([
|
||||||
'title' => '<h4>视频播放</h4>',
|
'title' => '<h4>视频播放</h4>',
|
||||||
'id' => 'videoModal',
|
'id' => 'videoModal',
|
||||||
|
@ -670,4 +670,44 @@ $(document).on('click', '.create-collection-btn', function () {
|
|||||||
});
|
});
|
||||||
|
|
||||||
const popoverTriggerList = document.querySelectorAll('[data-bs-toggle="popover"]')
|
const popoverTriggerList = document.querySelectorAll('[data-bs-toggle="popover"]')
|
||||||
const popoverList = [...popoverTriggerList].map(popoverTriggerEl => new bootstrap.Popover(popoverTriggerEl))
|
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 = '<table class="table"><tr><th style="display: table-cell;">文件/文件夹名</th><th>位置</th></tr>';
|
||||||
|
$.each(response.data, function(index, item) {
|
||||||
|
let path = item.relativePath;
|
||||||
|
let correctedPath = path.replace(/\\/g, '/');
|
||||||
|
table += '<tr><td style="display: table-cell;"> <i class="' + item.type+'"></i> '+item.name + '</td>' + '<td><a style="text-decoration: underline;" href="index.php?r=home%2Findex&directory=' + correctedPath + '">' + item.relativePath + '</a></td></tr>';
|
||||||
|
});
|
||||||
|
table += '</table>';
|
||||||
|
$('#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('');
|
||||||
|
});
|
Loading…
Reference in New Issue
Block a user