文件分享 访问页面优化

This commit is contained in:
Chenx221 2024-04-22 14:16:45 +08:00
parent f17af958e2
commit 71f0d9d519
Signed by: chenx221
GPG Key ID: D7A9EC07024C3021
3 changed files with 42 additions and 5 deletions

View File

@ -2,6 +2,7 @@
namespace app\models; namespace app\models;
use Yii;
use yii\db\ActiveQuery; use yii\db\ActiveQuery;
use yii\db\ActiveRecord; use yii\db\ActiveRecord;
@ -99,6 +100,20 @@ class Share extends ActiveRecord
{ {
$this->dl_count += 1; $this->dl_count += 1;
$this->save(true, ['dl_count']); $this->save(true, ['dl_count']);
} }
/**
* Give me the file name of the shared file/folder.
* @return string
*/
public function getShareFileName(): string
{
return basename($this->getAbsoluteFilePath());
}
public function getAbsoluteFilePath(): string
{
return Yii::getAlias(Yii::$app->params['dataDirectory']) . '/' . $this->sharer_id . '/' . $this->file_relative_path;
}
} }

View File

@ -231,4 +231,18 @@ class FileSizeHelper
return max(0, (int)$valueInMB); return max(0, (int)$valueInMB);
} }
public static function getFormatFSize(string $absolutePath): string
{
//detect path is file or folder or not exist
if (!file_exists($absolutePath)) {
return 'ERROR 文件/文件夹不存在';
}
if (is_dir($absolutePath)) {
return self::formatBytes(self::getDirectorySize($absolutePath));
} else {
return self::formatBytes(filesize($absolutePath));
}
}
} }

View File

@ -2,17 +2,25 @@
/** @var yii\web\View $this */ /** @var yii\web\View $this */
/** @var app\models\Share $model */ /** @var app\models\Share $model */
/** @var bool $isDirectory */ /** @var bool $isDirectory */
/** @var string $sharerUsername */ /** @var string $sharerUsername */
use app\assets\FontAwesomeAsset;
use app\utils\FileSizeHelper;
use yii\bootstrap5\Html; use yii\bootstrap5\Html;
use yii\helpers\Url; use yii\helpers\Url;
FontAwesomeAsset::register($this);
$this->title = '文件信息'; $this->title = '分享信息';
$this->params['breadcrumbs'][] = $this->title; $this->params['breadcrumbs'][] = $this->title;
?> ?>
<div class="share-file-info"> <div class="share-file-info">
<h1><?= Html::encode($this->title) ?></h1> <h1>
<i class="fa-solid fa-share-from-square"></i>
<?= Html::encode($this->title) ?>
</h1>
<p><?= ($isDirectory ? '文件夹' : '文件') . ': ' . $model->getShareFileName() ?></p>
<p>大小: <?= FileSizeHelper::getFormatFSize($model->getAbsoluteFilePath())?></p>
<p>分享者: <?= Html::encode($sharerUsername) ?></p> <!-- 显示分享者的用户名 --> <p>分享者: <?= Html::encode($sharerUsername) ?></p> <!-- 显示分享者的用户名 -->
<p>分享创建日期: <?= Html::encode($model->creation_date) ?></p> <p>分享创建日期: <?= Html::encode($model->creation_date) ?></p>