美化工作

This commit is contained in:
Chenx221 2024-02-11 12:39:05 +08:00
parent 9815159490
commit fab67a308a
Signed by: chenx221
GPG Key ID: D7A9EC07024C3021
2 changed files with 60 additions and 27 deletions

View File

@ -2,6 +2,7 @@
namespace app\controllers;
use app\utils\FileTypeDetector;
use Yii;
use yii\web\NotFoundHttpException;
use yii\web\Response;
@ -9,11 +10,15 @@ use yii\web\Response;
class HomeController extends \yii\web\Controller
{
/**
* diplay the page of the file manager (accepts a directory parameter like cd command)
* visit it via https://devs.chenx221.cyou:8081/index.php?r=home
*
* @return string|Response
* @throws NotFoundHttpException
*/
public function actionIndex($directory = null)
{
//Warning: Security Vulnerability: access via $directory parameter = ../ will display the internal files of the server
if (Yii::$app->user->isGuest) {
return $this->redirect(Yii::$app->user->loginUrl);
}
@ -26,6 +31,12 @@ class HomeController extends \yii\web\Controller
$parentDirectory = dirname($directory);
}
$directoryContents = $this->getDirectoryContents(join(DIRECTORY_SEPARATOR, [$rootDataDirectory, $userId, $directory ?: '.']));
foreach ($directoryContents as $key => $item) {
$relativePath = $directory ? $directory . '/' . $item : $item;
$absolutePath = Yii::getAlias('@app') . '/data/' . Yii::$app->user->id . '/' . $relativePath;
$type = FileTypeDetector::detect($absolutePath);
$directoryContents[$key] = ['name' => $item, 'type' => $type];
}
return $this->render('index', [
'directoryContents' => $directoryContents,
'parentDirectory' => $parentDirectory,
@ -54,6 +65,9 @@ class HomeController extends \yii\web\Controller
/**
* 下载指定路径下的文件
* must be provided with a relative path of the file
* download link:https://devs.chenx221.cyou:8081/index.php?r=home%2Fdownload&relativePath={the relative path of the file}
*
* @param string $relativePath 文件的相对路径
* @throws NotFoundHttpException 如果文件不存在
*/

View File

@ -3,46 +3,65 @@
/* @var $this yii\web\View */
/* @var $directoryContents array 文件和文件夹内容数组 */
/* @var $parentDirectory string 父目录 */
/* @var $directory string 当前路径 */
use yii\bootstrap5\Html;
use app\assets\FontAwesomeAsset;
$this->title = '文件管理';
$this->params['breadcrumbs'][] = $this->title;
FontAwesomeAsset::register($this);
?>
<div class="home-directory">
<h1><?= Html::encode($this->title) ?></h1>
<p>
<?= Html::a('HOME', ['home/index']) ?>
<?php if ($directory !== null): ?>
<?php
$parts = explode('/', $directory);
$path = '';
foreach ($parts as $part):
$path .= $part;
echo ' / ' . Html::a($part, ['home/index', 'directory' => $path]);
$path .= '/';
endforeach;
?>
<?php endif; ?>
</p>
<nav aria-label="breadcrumb">
<ol class="breadcrumb">
<?= Html::a('HOME', ['home/index'], ['class' => 'breadcrumb-item']) ?>
<?php if ($directory !== null): ?>
<?php
$parts = explode('/', $directory);
$path = '';
foreach ($parts as $part):
$path .= $part;
echo Html::a($part, ['home/index', 'directory' => $path], ['class' => 'breadcrumb-item']);
$path .= '/';
endforeach;
?>
<?php endif; ?>
</ol>
</nav>
<ul>
<table class="table table-hover">
<thead class="table-light">
<tr>
<th scope="col">名称</th>
<th scope="col">操作</th>
</tr>
</thead>
<tbody>
<?php foreach ($directoryContents as $item): ?>
<?php $relativePath = $directory ? $directory . '/' . $item : $item; ?>
<?php $relativePath = $directory ? $directory . '/' . $item['name'] : $item['name']; ?>
<!-- 修复错误的绝对路径问题-->
<?php $absolutePath = Yii::getAlias('@app') . '/data/' . Yii::$app->user->id . '/' . $relativePath; ?>
<?php if (is_dir($absolutePath)): ?>
<!-- 文件夹 -->
<li><?= Html::a($item, ['home/index', 'directory' => $relativePath]) ?></li>
<?php else: ?>
<!-- 文件 -->
<li><?= Html::a($item, ['home/download', 'relativePath' => $relativePath]) ?>
(<?= Html::a('下载', ['home/download', 'relativePath' => $relativePath]) ?>)
</li>
<?php endif; ?>
<tr>
<?php if (is_dir($absolutePath)): ?>
<!-- 文件夹 -->
<td>
<?= Html::tag('i', '', ['class' => $item['type']]) ?>
<?= Html::a($item['name'], ['home/index', 'directory' => $relativePath]) ?>
</td>
<td></td>
<?php else: ?>
<!-- 文件 -->
<td>
<?= Html::tag('i', '', ['class' => $item['type']]) ?>
<?= Html::a($item['name'], ['home/download', 'relativePath' => $relativePath]) ?>
</td>
<td><?= Html::a(Html::tag('i', '', ['class' => 'fa-regular fa-circle-down']), ['home/download', 'relativePath' => $relativePath]) ?></td>
<?php endif; ?>
</tr>
<?php endforeach; ?>
</ul>
</tbody>
</table>
</div>