StorageController.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. namespace App\Module\File\AdminControllers;
  3. use App\Module\File\AdminControllers\Helper\FilterHelper;
  4. use App\Module\File\AdminControllers\Helper\FormHelper;
  5. use App\Module\File\AdminControllers\Helper\GridHelper;
  6. use App\Module\File\AdminControllers\Helper\ShowHelper;
  7. use App\Module\File\Config\StorageConfig;
  8. use App\Module\File\Enums\STORAGE_STATUS;
  9. use App\Module\File\Models\FileStorage;
  10. use Dcat\Admin\Form;
  11. use Dcat\Admin\Grid;
  12. use Dcat\Admin\Show;
  13. use Spatie\RouteAttributes\Attributes\Resource;
  14. use UCore\DcatAdmin\AdminController;
  15. /**
  16. * 存储管理控制器
  17. */
  18. #[Resource('file-storages', names: 'dcat.admin.file-storages')]
  19. class StorageController extends AdminController
  20. {
  21. /**
  22. * 页面标题
  23. *
  24. * @var string
  25. */
  26. protected $title = '存储管理';
  27. /**
  28. * 列表页面
  29. *
  30. * @return Grid
  31. */
  32. protected function grid()
  33. {
  34. return Grid::make(new FileStorage(), function (Grid $grid) {
  35. $helper = new GridHelper($grid, $this);
  36. $helper->columnId();
  37. $grid->column('name', '存储名称');
  38. $grid->column('disk', '存储磁盘');
  39. $grid->column('path', '存储路径');
  40. $helper->columnStorageStatus();
  41. $helper->columnCreatedAt();
  42. $helper->columnUpdatedAt();
  43. // 添加测试按钮
  44. $grid->column('test', '操作')->display(function () {
  45. return "<a href='javascript:void(0);' class='btn btn-sm btn-primary storage-test' data-id='{$this->id}'>测试连接</a>";
  46. })->unescape();
  47. // 添加JavaScript代码
  48. $grid->script = <<<JS
  49. $(function () {
  50. $('.storage-test').on('click', function () {
  51. var id = $(this).data('id');
  52. $.ajax({
  53. url: '/admin/api/storage/test/' + id,
  54. type: 'GET',
  55. success: function (data) {
  56. if (data.status) {
  57. Dcat.success('连接成功');
  58. } else {
  59. Dcat.error('连接失败: ' + data.message);
  60. }
  61. },
  62. error: function () {
  63. Dcat.error('测试连接失败');
  64. }
  65. });
  66. });
  67. });
  68. JS;
  69. // 筛选器
  70. $grid->filter(function (Grid\Filter $filter) {
  71. $helper = new FilterHelper($filter, $this);
  72. $filter->equal('id', 'ID');
  73. $filter->like('name', '存储名称');
  74. $filter->equal('disk', '存储磁盘');
  75. $helper->equalStorageStatus();
  76. });
  77. });
  78. }
  79. /**
  80. * 详情页面
  81. *
  82. * @param mixed $id
  83. * @return Show
  84. */
  85. protected function detail($id)
  86. {
  87. return Show::make($id, new FileStorage(), function (Show $show) {
  88. $helper = new ShowHelper($show, $this);
  89. $helper->fieldId();
  90. $show->field('name', '存储名称');
  91. $show->field('disk', '存储磁盘');
  92. $show->field('path', '存储路径');
  93. $helper->fieldStorageStatus();
  94. $helper->fieldCreatedAt();
  95. $helper->fieldUpdatedAt();
  96. // 添加测试按钮
  97. $show->html(function () use ($show) {
  98. return "<a href='javascript:void(0);' class='btn btn-primary storage-test' data-id='{$this->id}'>测试连接</a>";
  99. });
  100. // 添加JavaScript代码
  101. $show->script = <<<JS
  102. $(function () {
  103. $('.storage-test').on('click', function () {
  104. var id = $(this).data('id');
  105. $.ajax({
  106. url: '/admin/api/storage/test/' + id,
  107. type: 'GET',
  108. success: function (data) {
  109. if (data.status) {
  110. Dcat.success('连接成功');
  111. } else {
  112. Dcat.error('连接失败: ' + data.message);
  113. }
  114. },
  115. error: function () {
  116. Dcat.error('测试连接失败');
  117. }
  118. });
  119. });
  120. });
  121. JS;
  122. });
  123. }
  124. /**
  125. * 表单页面
  126. *
  127. * @return Form
  128. */
  129. protected function form()
  130. {
  131. return Form::make(new FileStorage(), function (Form $form) {
  132. $form->display('id');
  133. $form->text('name', '存储名称')->required();
  134. $form->text('disk', '存储磁盘')->required()->help('对应config/filesystems.php中的磁盘配置');
  135. $form->text('path', '存储路径')->required()->default('uploads')->help('存储路径,相对于磁盘根目录');
  136. $form->radio('status', '存储状态')->options(STORAGE_STATUS::getAll())->default(STORAGE_STATUS::ENABLED->value);
  137. $form->display('created_at');
  138. $form->display('updated_at');
  139. // 保存后回调
  140. $form->saved(function (Form $form) {
  141. // 清除存储配置缓存
  142. StorageConfig::clearCache();
  143. });
  144. });
  145. }
  146. /**
  147. * 测试存储连接
  148. *
  149. * @param int $id
  150. * @return \Illuminate\Http\JsonResponse
  151. */
  152. public function test($id)
  153. {
  154. $storage = FileStorage::find($id);
  155. if (!$storage) {
  156. return response()->json(['status' => false, 'message' => '存储不存在']);
  157. }
  158. try {
  159. $disk = $storage->disk;
  160. $path = $storage->path . '/test.txt';
  161. $content = 'Test file created at ' . date('Y-m-d H:i:s');
  162. // 测试写入
  163. \Storage::disk($disk)->put($path, $content);
  164. // 测试读取
  165. $readContent = \Storage::disk($disk)->get($path);
  166. // 测试删除
  167. \Storage::disk($disk)->delete($path);
  168. if ($content === $readContent) {
  169. return response()->json(['status' => true, 'message' => '连接成功']);
  170. } else {
  171. return response()->json(['status' => false, 'message' => '读取内容与写入内容不一致']);
  172. }
  173. } catch (\Exception $e) {
  174. return response()->json(['status' => false, 'message' => $e->getMessage()]);
  175. }
  176. }
  177. }