columnId();
$grid->column('name', '存储名称');
$grid->column('disk', '存储磁盘');
$grid->column('path', '存储路径');
$helper->columnStorageStatus();
$helper->columnCreatedAt();
$helper->columnUpdatedAt();
// 添加测试按钮
$grid->column('test', '操作')->display(function () {
return "测试连接";
})->unescape();
// 添加JavaScript代码
$grid->script = <<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 "测试连接";
});
// 添加JavaScript代码
$show->script = <<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()]);
}
}
}