Skip to content

Commit

Permalink
add file upload
Browse files Browse the repository at this point in the history
  • Loading branch information
crazywhalecc committed Jun 2, 2023
1 parent 5ef1455 commit 12f7cfe
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
17 changes: 16 additions & 1 deletion src/OneBot/Driver/Swoole/TopEventListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
21 changes: 19 additions & 2 deletions src/OneBot/Driver/Workerman/TopEventListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/OneBot/global_defines.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 12f7cfe

Please sign in to comment.