Phase 2: Replace PeerTubeService HTTP client with Guzzle

This commit is contained in:
Pontoporeia
2026-05-20 01:07:41 +02:00
parent 5e75cacad7
commit ba57820016
4 changed files with 67 additions and 85 deletions

View File

@@ -1,5 +1,8 @@
<?php
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
/**
* PeerTubeService
*
@@ -18,7 +21,7 @@
* per process lifetime.
*
* Upload uses the simple multipart upload API:
* POST /api/v1/videos/upload — multipart form with CURLFile
* POST /api/v1/videos/upload — multipart form upload via Guzzle
*/
class PeerTubeService
{
@@ -28,6 +31,9 @@ class PeerTubeService
/** @var array<string,int> In-memory channel name → ID cache. */
private static array $channelCache = [];
/** @var Client|null Shared Guzzle client (lazy-init). */
private static ?Client $httpClient = null;
// -------------------------------------------------------------------------
// DB CRUD
// -------------------------------------------------------------------------
@@ -179,26 +185,27 @@ class PeerTubeService
$token = self::obtainToken($s);
$baseUrl = $s['instance_url'];
$mimeType = (new \finfo(FILEINFO_MIME_TYPE))->file($filePath);
// ── Simple multipart upload (non-resumable) ──
$uploadUrl = $baseUrl . '/api/v1/videos/upload';
$postFields = [
'channelId' => $channelId,
'name' => $title,
'privacy' => (int)$s['privacy'],
'commentsEnabled' => true,
'category' => 15,
'videofile' => new \CURLFile($filePath, $mimeType, $originalName),
$multipart = [
['name' => 'channelId', 'contents' => $channelId],
['name' => 'name', 'contents' => $title],
['name' => 'privacy', 'contents' => (int)$s['privacy']],
['name' => 'commentsEnabled', 'contents' => 'true'],
['name' => 'category', 'contents' => '15'],
['name' => 'videofile', 'contents' => fopen($filePath, 'r'), 'filename' => $originalName],
];
if ($description !== '') {
$postFields['description'] = $description;
$multipart[] = ['name' => 'description', 'contents' => $description];
}
$resp = self::httpRequest($uploadUrl, 'POST', $postFields, [
'Authorization: Bearer ' . $token,
], 600);
$resp = self::httpRequest($uploadUrl, 'POST', [
'headers' => ['Authorization' => 'Bearer ' . $token],
'multipart' => $multipart,
'timeout' => 600,
]);
if ($resp['status'] < 200 || $resp['status'] >= 300) {
$errJson = json_decode($resp['body'], true);
@@ -235,9 +242,10 @@ class PeerTubeService
try {
$token = self::obtainToken($s);
$url = $s['instance_url'] . '/api/v1/videos/' . urlencode($uuid);
$resp = self::httpRequest($url, 'GET', '', [
'Authorization: Bearer ' . $token,
], 10);
$resp = self::httpRequest($url, 'GET', [
'headers' => ['Authorization' => 'Bearer ' . $token],
'timeout' => 10,
]);
if ($resp['status'] !== 200) {
return null;
}
@@ -283,9 +291,10 @@ class PeerTubeService
try {
$token = self::obtainToken($s);
$url = rtrim($s['instance_url'], '/') . '/api/v1/video-channels/' . urlencode($name);
$resp = self::httpRequest($url, 'GET', '', [
'Authorization: Bearer ' . $token,
], 10);
$resp = self::httpRequest($url, 'GET', [
'headers' => ['Authorization' => 'Bearer ' . $token],
'timeout' => 10,
]);
if ($resp['status'] !== 200) {
return null;
@@ -319,7 +328,7 @@ class PeerTubeService
}
$url = rtrim($instanceUrl, '/') . '/api/v1/oauth-clients/local';
$response = self::httpRequest($url, 'GET', '', [], 10);
$response = self::httpRequest($url, 'GET', ['timeout' => 10]);
$json = json_decode($response['body'], true);
if ($response['status'] !== 200 || empty($json['client_id'])) {
@@ -352,9 +361,9 @@ class PeerTubeService
'password' => $s['password'],
]);
$response = self::httpRequest($tokenUrl, 'POST', $body, [
'Content-Type: application/x-www-form-urlencoded',
'Content-Length: ' . strlen($body),
$response = self::httpRequest($tokenUrl, 'POST', [
'headers' => ['Content-Type' => 'application/x-www-form-urlencoded'],
'body' => $body,
]);
$json = json_decode($response['body'], true);
@@ -370,71 +379,44 @@ class PeerTubeService
// HTTP helper
// -------------------------------------------------------------------------
// -------------------------------------------------------------------------
// HTTP helper
// -------------------------------------------------------------------------
/**
* Shared Guzzle HTTP client (lazy-init with SSL verification, HTTP/2, 15s connect timeout).
*/
private static function client(): Client
{
if (self::$httpClient === null) {
self::$httpClient = new Client([
'http_errors' => false,
'allow_redirects' => false,
'connect_timeout' => 15,
'version' => 2.0, // HTTP/2
]);
}
return self::$httpClient;
}
/**
* Minimal cURL HTTP helper.
* Perform an HTTP request via Guzzle.
*
* @param array $options Guzzle request options (headers, body, multipart, timeout, etc.)
* @return array{status:int, body:string, headers:array<string,string>}
*/
public static function httpRequest(
string $url,
string $method,
string|array $body,
array $headers,
int $timeout = 300
): array {
if (!function_exists('curl_init')) {
throw new \RuntimeException('L\'extension PHP cURL est requise pour l\'intégration PeerTube.');
public static function httpRequest(string $url, string $method, array $options = []): array
{
try {
$response = self::client()->request($method, $url, $options);
$headers = [];
foreach ($response->getHeaders() as $name => $values) {
$headers[strtolower($name)] = end($values);
}
return [
'status' => $response->getStatusCode(),
'body' => (string)$response->getBody(),
'headers' => $headers,
];
} catch (GuzzleException $e) {
throw new \RuntimeException('Erreur réseau PeerTube : ' . $e->getMessage(), 0, $e);
}
$ch = curl_init($url);
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => $timeout,
CURLOPT_CONNECTTIMEOUT => 15,
CURLOPT_FOLLOWLOCATION => false, // Must be false to capture Location header
CURLOPT_MAXREDIRS => 3,
CURLOPT_SSL_VERIFYPEER => true,
CURLOPT_SSL_VERIFYHOST => 2,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_2_0,
CURLOPT_HEADERFUNCTION => function ($ch, $headerLine) use (&$responseHeaders) {
$len = strlen($headerLine);
$parts = explode(':', $headerLine, 2);
if (count($parts) === 2) {
$responseHeaders[strtolower(trim($parts[0]))] = trim($parts[1]);
}
return $len;
},
]);
$responseHeaders = [];
if ($method === 'POST') {
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
} elseif ($method === 'PUT' || $method === 'PATCH') {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
} elseif ($method === 'DELETE') {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
} elseif ($method === 'HEAD') {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'HEAD');
curl_setopt($ch, CURLOPT_NOBODY, true);
}
$responseBody = curl_exec($ch);
$status = (int)curl_getinfo($ch, CURLINFO_HTTP_CODE);
$error = curl_error($ch);
if ($responseBody === false && $method !== 'HEAD') {
throw new \RuntimeException('Erreur réseau PeerTube : ' . $error);
}
return ['status' => $status, 'body' => (string)$responseBody, 'headers' => $responseHeaders];
}
// -------------------------------------------------------------------------