| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- <?php
- namespace App\Module\File\AdminControllers;
- use App\Module\File\AdminControllers\Helper\FilterHelper;
- use App\Module\File\AdminControllers\Helper\FormHelper;
- use App\Module\File\AdminControllers\Helper\GridHelper;
- use App\Module\File\AdminControllers\Helper\ShowHelper;
- use App\Module\File\Config\StorageConfig;
- use App\Module\File\Enums\STORAGE_STATUS;
- use App\Module\File\Models\FileStorage;
- use Dcat\Admin\Form;
- use Dcat\Admin\Grid;
- use Dcat\Admin\Show;
- use Spatie\RouteAttributes\Attributes\Resource;
- use UCore\DcatAdmin\AdminController;
- /**
- * 存储管理控制器
- */
- #[Resource('file-storages', names: 'dcat.admin.file-storages')]
- class StorageController extends AdminController
- {
- /**
- * 页面标题
- *
- * @var string
- */
- protected $title = '存储管理';
- /**
- * 列表页面
- *
- * @return Grid
- */
- protected function grid()
- {
- return Grid::make(new FileStorage(), function (Grid $grid) {
- $helper = new GridHelper($grid, $this);
- $helper->columnId();
- $grid->column('name', '存储名称');
- $grid->column('disk', '存储磁盘');
- $grid->column('path', '存储路径');
- $helper->columnStorageStatus();
- $helper->columnCreatedAt();
- $helper->columnUpdatedAt();
- // 添加测试按钮
- $grid->column('test', '操作')->display(function () {
- return "<a href='javascript:void(0);' class='btn btn-sm btn-primary storage-test' data-id='{$this->id}'>测试连接</a>";
- })->unescape();
- // 添加JavaScript代码
- $grid->script = <<<JS
- $(function () {
- $('.storage-test').on('click', function () {
- var id = $(this).data('id');
- $.ajax({
- url: '/admin/api/storage/test/' + id,
- type: 'GET',
- success: function (data) {
- if (data.status) {
- Dcat.success('连接成功');
- } else {
- Dcat.error('连接失败: ' + data.message);
- }
- },
- error: function () {
- Dcat.error('测试连接失败');
- }
- });
- });
- });
- JS;
- // 筛选器
- $grid->filter(function (Grid\Filter $filter) {
- $helper = new FilterHelper($filter, $this);
- $filter->equal('id', 'ID');
- $filter->like('name', '存储名称');
- $filter->equal('disk', '存储磁盘');
- $helper->equalStorageStatus();
- });
- });
- }
- /**
- * 详情页面
- *
- * @param mixed $id
- * @return Show
- */
- protected function detail($id)
- {
- return Show::make($id, new FileStorage(), function (Show $show) {
- $helper = new ShowHelper($show, $this);
- $helper->fieldId();
- $show->field('name', '存储名称');
- $show->field('disk', '存储磁盘');
- $show->field('path', '存储路径');
- $helper->fieldStorageStatus();
- $helper->fieldCreatedAt();
- $helper->fieldUpdatedAt();
- // 添加测试按钮
- $show->html(function () use ($show) {
- return "<a href='javascript:void(0);' class='btn btn-primary storage-test' data-id='{$this->id}'>测试连接</a>";
- });
- // 添加JavaScript代码
- $show->script = <<<JS
- $(function () {
- $('.storage-test').on('click', function () {
- var id = $(this).data('id');
- $.ajax({
- url: '/admin/api/storage/test/' + id,
- type: 'GET',
- success: function (data) {
- if (data.status) {
- Dcat.success('连接成功');
- } else {
- Dcat.error('连接失败: ' + data.message);
- }
- },
- error: function () {
- Dcat.error('测试连接失败');
- }
- });
- });
- });
- JS;
- });
- }
- /**
- * 表单页面
- *
- * @return Form
- */
- protected function form()
- {
- return Form::make(new FileStorage(), function (Form $form) {
- $form->display('id');
- $form->text('name', '存储名称')->required();
- $form->text('disk', '存储磁盘')->required()->help('对应config/filesystems.php中的磁盘配置');
- $form->text('path', '存储路径')->required()->default('uploads')->help('存储路径,相对于磁盘根目录');
- $form->radio('status', '存储状态')->options(STORAGE_STATUS::getAll())->default(STORAGE_STATUS::ENABLED->value);
- $form->display('created_at');
- $form->display('updated_at');
- // 保存后回调
- $form->saved(function (Form $form) {
- // 清除存储配置缓存
- StorageConfig::clearCache();
- });
- });
- }
- /**
- * 测试存储连接
- *
- * @param int $id
- * @return \Illuminate\Http\JsonResponse
- */
- public function test($id)
- {
- $storage = FileStorage::find($id);
- if (!$storage) {
- return response()->json(['status' => false, 'message' => '存储不存在']);
- }
- try {
- $disk = $storage->disk;
- $path = $storage->path . '/test.txt';
- $content = 'Test file created at ' . date('Y-m-d H:i:s');
- // 测试写入
- \Storage::disk($disk)->put($path, $content);
- // 测试读取
- $readContent = \Storage::disk($disk)->get($path);
- // 测试删除
- \Storage::disk($disk)->delete($path);
- if ($content === $readContent) {
- return response()->json(['status' => true, 'message' => '连接成功']);
- } else {
- return response()->json(['status' => false, 'message' => '读取内容与写入内容不一致']);
- }
- } catch (\Exception $e) {
- return response()->json(['status' => false, 'message' => $e->getMessage()]);
- }
- }
- }
|