Showing
1 changed file
with
71 additions
and
0 deletions
| @@ -244,6 +244,77 @@ $http_worker->onMessage = function ($connection, $request) { | @@ -244,6 +244,77 @@ $http_worker->onMessage = function ($connection, $request) { | ||
| 244 | return; | 244 | return; |
| 245 | } | 245 | } |
| 246 | 246 | ||
| 247 | + // 3. Generate Pre-Signed URL for Direct Upload | ||
| 248 | + if (strpos($path, '/tos/sign') === 0) { | ||
| 249 | + $query = $request->get(); | ||
| 250 | + $filename = $query['filename'] ?? 'file_' . time(); | ||
| 251 | + $ext = strtolower(pathinfo($filename, PATHINFO_EXTENSION)); | ||
| 252 | + | ||
| 253 | + // Validation - Keep it simple for demo | ||
| 254 | + $allowed = ['jpg', 'jpeg', 'png', 'gif', 'webp', 'mp4', 'pdf', 'xls', 'xlsx']; | ||
| 255 | + if (!in_array($ext, $allowed) && $ext !== '') { | ||
| 256 | + // If no extension providing, we might just allow it or fail. | ||
| 257 | + // Ideally client should provide full filename with extension. | ||
| 258 | + } | ||
| 259 | + | ||
| 260 | + // TOS Configuration (Keys from hsobs.php) | ||
| 261 | + $ak = 'AKLTZjkyMzliYjQ5N2IyNDFjNDliMTBiY2E2ZmU5ODhjNTM'; | ||
| 262 | + $sk = 'WldKbE5XUmpPRGxqWmpZM05EUTBObUpqTTJSa01qVTNNMkprWmpsbU9Uaw=='; | ||
| 263 | + $endpoint = 'tos-cn-shanghai.volces.com'; | ||
| 264 | + $region = 'cn-shanghai'; | ||
| 265 | + $bucket = 'ocxun'; | ||
| 266 | + | ||
| 267 | + try { | ||
| 268 | + $client = new TosClient([ | ||
| 269 | + 'region' => $region, | ||
| 270 | + 'endpoint' => $endpoint, | ||
| 271 | + 'ak' => $ak, | ||
| 272 | + 'sk' => $sk, | ||
| 273 | + ]); | ||
| 274 | + | ||
| 275 | + $uuid = bin2hex(random_bytes(8)); | ||
| 276 | + // TODO: User ID from token | ||
| 277 | + $userPhone = 'guest'; | ||
| 278 | + // Ensure filename is safe | ||
| 279 | + $safeName = preg_replace('/[^a-zA-Z0-9._-]/', '', $filename); | ||
| 280 | + if (!$safeName) | ||
| 281 | + $safeName = 'unnamed'; | ||
| 282 | + | ||
| 283 | + $objectKey = "clawdbot/{$userPhone}/direct_{$uuid}_{$safeName}"; | ||
| 284 | + | ||
| 285 | + // Generate Pre-Signed PUT URL (Valid for 15 mins) | ||
| 286 | + // Note: The SDK method name might vary slightly based on version, | ||
| 287 | + // but `preSignedURL` is standard for TOS PHP SDK v2. | ||
| 288 | + $input = new \Tos\Model\PreSignedURLInput( | ||
| 289 | + 'PUT', | ||
| 290 | + $bucket, | ||
| 291 | + $objectKey, | ||
| 292 | + 300 // 5 minutes validity | ||
| 293 | + ); | ||
| 294 | + | ||
| 295 | + // Add content-type if known? client will send it. | ||
| 296 | + // For simple PUT, we just sign the method and resource. | ||
| 297 | + | ||
| 298 | + $output = $client->preSignedURL($input); | ||
| 299 | + $signedUrl = $output->getSignedUrl(); | ||
| 300 | + | ||
| 301 | + // Public Access URL (Assuming bucket is public-read or we use signed Get URL) | ||
| 302 | + // For this project, we used public-read ACL in previous code, so we assume public access. | ||
| 303 | + $publicUrl = "https://{$bucket}.{$endpoint}/{$objectKey}"; | ||
| 304 | + | ||
| 305 | + $connection->send(json_encode([ | ||
| 306 | + 'ok' => true, | ||
| 307 | + 'uploadUrl' => $signedUrl, | ||
| 308 | + 'publicUrl' => $publicUrl, | ||
| 309 | + 'key' => $objectKey | ||
| 310 | + ])); | ||
| 311 | + | ||
| 312 | + } catch (Exception $e) { | ||
| 313 | + $connection->send(new \Workerman\Protocols\Http\Response(500, [], json_encode(['ok' => false, 'error' => $e->getMessage()]))); | ||
| 314 | + } | ||
| 315 | + return; | ||
| 316 | + } | ||
| 317 | + | ||
| 247 | $connection->send("Moltbot Relay HTTP Server"); | 318 | $connection->send("Moltbot Relay HTTP Server"); |
| 248 | }; | 319 | }; |
| 249 | 320 |
Please
register
or
login
to post a comment