From 12f7cfe4f4a5b78963a5cc66491d9e16742f6688 Mon Sep 17 00:00:00 2001 From: Jerry Date: Fri, 2 Jun 2023 11:35:25 +0800 Subject: [PATCH] add file upload --- src/OneBot/Driver/Swoole/TopEventListener.php | 17 ++++++++++++++- .../Driver/Workerman/TopEventListener.php | 21 +++++++++++++++++-- src/OneBot/global_defines.php | 2 +- 3 files changed, 36 insertions(+), 4 deletions(-) diff --git a/src/OneBot/Driver/Swoole/TopEventListener.php b/src/OneBot/Driver/Swoole/TopEventListener.php index f307640..0fff362 100644 --- a/src/OneBot/Driver/Swoole/TopEventListener.php +++ b/src/OneBot/Driver/Swoole/TopEventListener.php @@ -5,6 +5,7 @@ namespace OneBot\Driver\Swoole; use Choir\Http\HttpFactory; +use Choir\Http\UploadedFile; use Choir\WebSocket\FrameInterface; use OneBot\Driver\Coroutine\Adaptive; use OneBot\Driver\Event\Http\HttpRequestEvent; @@ -92,7 +93,21 @@ public function onRequest(array $config, Request $request, Response $response) $request->header, $content ); - $req = $req->withQueryParams($request->get ?? []); + $req = $req->withQueryParams($request->get ?? []) + ->withCookieParams($request->cookie ?? []); + $uploaded = []; + if (!empty($request->files)) { + foreach ($request->files as $key => $value) { + $upload = new UploadedFile([ + 'key' => $key, + ...$value, + ]); + $uploaded[] = $upload; + } + if ($uploaded !== []) { + $req = $req->withUploadedFiles($uploaded); + } + } $event = new HttpRequestEvent($req); try { $event->setSocketConfig($config); diff --git a/src/OneBot/Driver/Workerman/TopEventListener.php b/src/OneBot/Driver/Workerman/TopEventListener.php index 19feee0..b025511 100644 --- a/src/OneBot/Driver/Workerman/TopEventListener.php +++ b/src/OneBot/Driver/Workerman/TopEventListener.php @@ -5,6 +5,7 @@ namespace OneBot\Driver\Workerman; use Choir\Http\HttpFactory; +use Choir\Http\UploadedFile; use Choir\WebSocket\FrameFactory; use Choir\WebSocket\FrameInterface; use OneBot\Driver\Coroutine\Adaptive; @@ -161,12 +162,28 @@ public function onHttpRequest(array $config, TcpConnection $connection, Request } $port = $connection->getLocalPort(); ob_logger()->debug('Http request from ' . $port . ': ' . $request->uri()); - $event = new HttpRequestEvent(HttpFactory::createServerRequest( + $req = HttpFactory::createServerRequest( $request->method(), $request->uri(), $request->header(), $request->rawBody() - )); + ); + $req = $req->withQueryParams($request->get() ?? []) + ->withCookieParams($request->cookie() ?? []); + if (!empty($request->file())) { + $uploaded = []; + foreach ($request->file() as $key => $value) { + $upload = new UploadedFile([ + 'key' => $key, + ...$value, + ]); + $uploaded[] = $upload; + } + if ($uploaded !== []) { + $req = $req->withUploadedFiles($uploaded); + } + } + $event = new HttpRequestEvent($req); $event->setSocketConfig($config); $send_callable = function (ResponseInterface $psr_response) use ($connection) { $response = new WorkermanResponse(); diff --git a/src/OneBot/global_defines.php b/src/OneBot/global_defines.php index ecc9845..8b6a68a 100644 --- a/src/OneBot/global_defines.php +++ b/src/OneBot/global_defines.php @@ -13,7 +13,7 @@ use ZM\Logger\ConsoleLogger; const ONEBOT_VERSION = '12'; -const ONEBOT_LIBOB_VERSION = '0.6.3'; +const ONEBOT_LIBOB_VERSION = '0.6.4'; const ONEBOT_JSON = 1; const ONEBOT_MSGPACK = 2;