mirror of
https://github.com/lkddi/Xboard.git
synced 2026-04-28 06:47:24 +08:00
feat: add plugin upload functionality and optimize plugin logic
This commit is contained in:
@@ -192,4 +192,56 @@ class PluginController extends Controller
|
||||
], 400);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传插件
|
||||
*/
|
||||
public function upload(Request $request)
|
||||
{
|
||||
$request->validate([
|
||||
'file' => [
|
||||
'required',
|
||||
'file',
|
||||
'mimes:zip',
|
||||
'max:10240', // 最大10MB
|
||||
]
|
||||
], [
|
||||
'file.required' => '请选择插件包文件',
|
||||
'file.file' => '无效的文件类型',
|
||||
'file.mimes' => '插件包必须是zip格式',
|
||||
'file.max' => '插件包大小不能超过10MB'
|
||||
]);
|
||||
|
||||
try {
|
||||
$this->pluginManager->upload($request->file('file'));
|
||||
return response()->json([
|
||||
'message' => '插件上传成功'
|
||||
]);
|
||||
} catch (\Exception $e) {
|
||||
return response()->json([
|
||||
'message' => '插件上传失败:' . $e->getMessage()
|
||||
], 400);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除插件
|
||||
*/
|
||||
public function delete(Request $request)
|
||||
{
|
||||
$request->validate([
|
||||
'code' => 'required|string'
|
||||
]);
|
||||
|
||||
try {
|
||||
$this->pluginManager->delete($request->input('code'));
|
||||
return response()->json([
|
||||
'message' => '插件删除成功'
|
||||
]);
|
||||
} catch (\Exception $e) {
|
||||
return response()->json([
|
||||
'message' => '插件删除失败:' . $e->getMessage()
|
||||
], 400);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,7 @@ use App\Http\Controllers\Controller;
|
||||
use App\Services\ThemeService;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\File;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class ThemeController extends Controller
|
||||
{
|
||||
@@ -75,7 +76,7 @@ class ThemeController extends Controller
|
||||
} catch (ApiException $e) {
|
||||
throw $e;
|
||||
} catch (\Exception $e) {
|
||||
\Log::error('Theme upload failed', [
|
||||
Log::error('Theme upload failed', [
|
||||
'error' => $e->getMessage(),
|
||||
'file' => $request->file('file')?->getClientOriginalName()
|
||||
]);
|
||||
|
||||
@@ -210,6 +210,8 @@ class AdminRoute
|
||||
'prefix' => 'plugin'
|
||||
], function ($router) {
|
||||
$router->get('/getPlugins', [\App\Http\Controllers\V2\Admin\PluginController::class, 'index']);
|
||||
$router->post('/upload', [\App\Http\Controllers\V2\Admin\PluginController::class, 'upload']);
|
||||
$router->post('/delete', [\App\Http\Controllers\V2\Admin\PluginController::class, 'delete']);
|
||||
$router->post('install', [\App\Http\Controllers\V2\Admin\PluginController::class, 'install']);
|
||||
$router->post('uninstall', [\App\Http\Controllers\V2\Admin\PluginController::class, 'uninstall']);
|
||||
$router->post('enable', [\App\Http\Controllers\V2\Admin\PluginController::class, 'enable']);
|
||||
|
||||
@@ -80,11 +80,12 @@ class HookManager
|
||||
* 移除钩子监听器
|
||||
*
|
||||
* @param string $hook 钩子名称
|
||||
* @param callable|null $callback 回调函数
|
||||
* @return void
|
||||
*/
|
||||
public static function remove(string $hook): void
|
||||
public static function remove(string $hook, ?callable $callback = null): void
|
||||
{
|
||||
Eventy::removeAction($hook);
|
||||
Eventy::removeFilter($hook);
|
||||
Eventy::removeAction($hook, $callback);
|
||||
Eventy::removeFilter($hook, $callback);
|
||||
}
|
||||
}
|
||||
@@ -144,6 +144,31 @@ class PluginManager
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除插件
|
||||
*
|
||||
* @param string $pluginCode
|
||||
* @return bool
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function delete(string $pluginCode): bool
|
||||
{
|
||||
// 先卸载插件
|
||||
if (Plugin::where('code', $pluginCode)->exists()) {
|
||||
$this->uninstall($pluginCode);
|
||||
}
|
||||
|
||||
$pluginPath = $this->pluginPath . '/' . $pluginCode;
|
||||
if (!File::exists($pluginPath)) {
|
||||
throw new \Exception('插件不存在');
|
||||
}
|
||||
|
||||
// 删除插件目录
|
||||
File::deleteDirectory($pluginPath);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 加载插件实例
|
||||
*/
|
||||
@@ -183,4 +208,59 @@ class PluginManager
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传插件
|
||||
*
|
||||
* @param \Illuminate\Http\UploadedFile $file
|
||||
* @return bool
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function upload($file): bool
|
||||
{
|
||||
$tmpPath = storage_path('tmp/plugins');
|
||||
if (!File::exists($tmpPath)) {
|
||||
File::makeDirectory($tmpPath, 0755, true);
|
||||
}
|
||||
|
||||
$extractPath = $tmpPath . '/' . uniqid();
|
||||
$zip = new \ZipArchive();
|
||||
|
||||
if ($zip->open($file->path()) !== true) {
|
||||
throw new \Exception('无法打开插件包文件');
|
||||
}
|
||||
|
||||
$zip->extractTo($extractPath);
|
||||
$zip->close();
|
||||
|
||||
$configFile = File::glob($extractPath . '/*/config.json');
|
||||
if (empty($configFile)) {
|
||||
$configFile = File::glob($extractPath . '/config.json');
|
||||
}
|
||||
|
||||
if (empty($configFile)) {
|
||||
File::deleteDirectory($extractPath);
|
||||
throw new \Exception('插件包格式错误:缺少配置文件');
|
||||
}
|
||||
|
||||
$pluginPath = dirname(reset($configFile));
|
||||
$config = json_decode(File::get($pluginPath . '/config.json'), true);
|
||||
|
||||
if (!$this->validateConfig($config)) {
|
||||
File::deleteDirectory($extractPath);
|
||||
throw new \Exception('插件配置文件格式错误');
|
||||
}
|
||||
|
||||
$targetPath = $this->pluginPath . '/' . $config['code'];
|
||||
if (File::exists($targetPath)) {
|
||||
File::deleteDirectory($extractPath);
|
||||
throw new \Exception('插件已存在');
|
||||
}
|
||||
|
||||
File::copyDirectory($pluginPath, $targetPath);
|
||||
File::deleteDirectory($pluginPath);
|
||||
File::deleteDirectory($extractPath);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user