Skip to content

新增文件上传支持 #104

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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