Skip to content

Commit 12f7cfe

Browse files
committed
add file upload
1 parent 5ef1455 commit 12f7cfe

File tree

3 files changed

+36
-4
lines changed

3 files changed

+36
-4
lines changed

src/OneBot/Driver/Swoole/TopEventListener.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace OneBot\Driver\Swoole;
66

77
use Choir\Http\HttpFactory;
8+
use Choir\Http\UploadedFile;
89
use Choir\WebSocket\FrameInterface;
910
use OneBot\Driver\Coroutine\Adaptive;
1011
use OneBot\Driver\Event\Http\HttpRequestEvent;
@@ -92,7 +93,21 @@ public function onRequest(array $config, Request $request, Response $response)
9293
$request->header,
9394
$content
9495
);
95-
$req = $req->withQueryParams($request->get ?? []);
96+
$req = $req->withQueryParams($request->get ?? [])
97+
->withCookieParams($request->cookie ?? []);
98+
$uploaded = [];
99+
if (!empty($request->files)) {
100+
foreach ($request->files as $key => $value) {
101+
$upload = new UploadedFile([
102+
'key' => $key,
103+
...$value,
104+
]);
105+
$uploaded[] = $upload;
106+
}
107+
if ($uploaded !== []) {
108+
$req = $req->withUploadedFiles($uploaded);
109+
}
110+
}
96111
$event = new HttpRequestEvent($req);
97112
try {
98113
$event->setSocketConfig($config);

src/OneBot/Driver/Workerman/TopEventListener.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace OneBot\Driver\Workerman;
66

77
use Choir\Http\HttpFactory;
8+
use Choir\Http\UploadedFile;
89
use Choir\WebSocket\FrameFactory;
910
use Choir\WebSocket\FrameInterface;
1011
use OneBot\Driver\Coroutine\Adaptive;
@@ -161,12 +162,28 @@ public function onHttpRequest(array $config, TcpConnection $connection, Request
161162
}
162163
$port = $connection->getLocalPort();
163164
ob_logger()->debug('Http request from ' . $port . ': ' . $request->uri());
164-
$event = new HttpRequestEvent(HttpFactory::createServerRequest(
165+
$req = HttpFactory::createServerRequest(
165166
$request->method(),
166167
$request->uri(),
167168
$request->header(),
168169
$request->rawBody()
169-
));
170+
);
171+
$req = $req->withQueryParams($request->get() ?? [])
172+
->withCookieParams($request->cookie() ?? []);
173+
if (!empty($request->file())) {
174+
$uploaded = [];
175+
foreach ($request->file() as $key => $value) {
176+
$upload = new UploadedFile([
177+
'key' => $key,
178+
...$value,
179+
]);
180+
$uploaded[] = $upload;
181+
}
182+
if ($uploaded !== []) {
183+
$req = $req->withUploadedFiles($uploaded);
184+
}
185+
}
186+
$event = new HttpRequestEvent($req);
170187
$event->setSocketConfig($config);
171188
$send_callable = function (ResponseInterface $psr_response) use ($connection) {
172189
$response = new WorkermanResponse();

src/OneBot/global_defines.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
use ZM\Logger\ConsoleLogger;
1414

1515
const ONEBOT_VERSION = '12';
16-
const ONEBOT_LIBOB_VERSION = '0.6.3';
16+
const ONEBOT_LIBOB_VERSION = '0.6.4';
1717

1818
const ONEBOT_JSON = 1;
1919
const ONEBOT_MSGPACK = 2;

0 commit comments

Comments
 (0)