mirror of
https://github.com/BookStackApp/BookStack.git
synced 2025-08-07 23:03:00 +03:00
Removed use of HttpFetcher
- Fixed some existing issues in new aligned process. - Manually tested each external call scenario.
This commit is contained in:
@@ -16,7 +16,6 @@ use Illuminate\Foundation\Bus\Dispatchable;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Psr\Http\Client\ClientExceptionInterface;
|
||||
|
||||
class DispatchWebhookJob implements ShouldQueue
|
||||
{
|
||||
@@ -69,7 +68,7 @@ class DispatchWebhookJob implements ShouldQueue
|
||||
$lastError = "Response status from endpoint was {$statusCode}";
|
||||
Log::error("Webhook call to endpoint {$this->webhook->endpoint} failed with status {$statusCode}");
|
||||
}
|
||||
} catch (ClientExceptionInterface $error) {
|
||||
} catch (\Exception $error) {
|
||||
$lastError = $error->getMessage();
|
||||
Log::error("Webhook call to endpoint {$this->webhook->endpoint} failed with error \"{$lastError}\"");
|
||||
}
|
||||
|
@@ -25,4 +25,9 @@ class HttpClientHistory
|
||||
{
|
||||
return $this->requestAt($this->requestCount() - 1);
|
||||
}
|
||||
|
||||
public function all(): array
|
||||
{
|
||||
return $this->container;
|
||||
}
|
||||
}
|
||||
|
@@ -7,6 +7,7 @@ use GuzzleHttp\Handler\MockHandler;
|
||||
use GuzzleHttp\HandlerStack;
|
||||
use GuzzleHttp\Middleware;
|
||||
use GuzzleHttp\Psr7\Request as GuzzleRequest;
|
||||
use GuzzleHttp\Psr7\Response;
|
||||
use Psr\Http\Client\ClientInterface;
|
||||
|
||||
class HttpRequestService
|
||||
@@ -16,7 +17,7 @@ class HttpRequestService
|
||||
/**
|
||||
* Build a new http client for sending requests on.
|
||||
*/
|
||||
public function buildClient(int $timeout, array $options): ClientInterface
|
||||
public function buildClient(int $timeout, array $options = []): ClientInterface
|
||||
{
|
||||
$defaultOptions = [
|
||||
'timeout' => $timeout,
|
||||
@@ -40,8 +41,16 @@ class HttpRequestService
|
||||
* Returns history which can then be queried.
|
||||
* @link https://docs.guzzlephp.org/en/stable/testing.html#history-middleware
|
||||
*/
|
||||
public function mockClient(array $responses = []): HttpClientHistory
|
||||
public function mockClient(array $responses = [], bool $pad = true): HttpClientHistory
|
||||
{
|
||||
// By default, we pad out the responses with 10 successful values so that requests will be
|
||||
// properly recorded for inspection. Otherwise, we can't later check if we're received
|
||||
// too many requests.
|
||||
if ($pad) {
|
||||
$response = new Response(200, [], 'success');
|
||||
$responses = array_merge($responses, array_fill(0, 10, $response));
|
||||
}
|
||||
|
||||
$container = [];
|
||||
$history = Middleware::history($container);
|
||||
$mock = new MockHandler($responses);
|
||||
|
@@ -1,38 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace BookStack\Uploads;
|
||||
|
||||
use BookStack\Exceptions\HttpFetchException;
|
||||
|
||||
class HttpFetcher
|
||||
{
|
||||
/**
|
||||
* Fetch content from an external URI.
|
||||
*
|
||||
* @param string $uri
|
||||
*
|
||||
* @throws HttpFetchException
|
||||
*
|
||||
* @return bool|string
|
||||
*/
|
||||
public function fetch(string $uri)
|
||||
{
|
||||
$ch = curl_init();
|
||||
curl_setopt_array($ch, [
|
||||
CURLOPT_URL => $uri,
|
||||
CURLOPT_RETURNTRANSFER => 1,
|
||||
CURLOPT_CONNECTTIMEOUT => 5,
|
||||
]);
|
||||
|
||||
$data = curl_exec($ch);
|
||||
$err = curl_error($ch);
|
||||
curl_close($ch);
|
||||
|
||||
if ($err) {
|
||||
$errno = curl_errno($ch);
|
||||
throw new HttpFetchException($err, $errno);
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
}
|
@@ -3,20 +3,20 @@
|
||||
namespace BookStack\Uploads;
|
||||
|
||||
use BookStack\Exceptions\HttpFetchException;
|
||||
use BookStack\Http\HttpRequestService;
|
||||
use BookStack\Users\Models\User;
|
||||
use Exception;
|
||||
use GuzzleHttp\Psr7\Request;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Str;
|
||||
use Psr\Http\Client\ClientExceptionInterface;
|
||||
|
||||
class UserAvatars
|
||||
{
|
||||
protected $imageService;
|
||||
protected $http;
|
||||
|
||||
public function __construct(ImageService $imageService, HttpFetcher $http)
|
||||
{
|
||||
$this->imageService = $imageService;
|
||||
$this->http = $http;
|
||||
public function __construct(
|
||||
protected ImageService $imageService,
|
||||
protected HttpRequestService $http
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -112,8 +112,10 @@ class UserAvatars
|
||||
protected function getAvatarImageData(string $url): string
|
||||
{
|
||||
try {
|
||||
$imageData = $this->http->fetch($url);
|
||||
} catch (HttpFetchException $exception) {
|
||||
$client = $this->http->buildClient(5);
|
||||
$response = $client->sendRequest(new Request('GET', $url));
|
||||
$imageData = (string) $response->getBody();
|
||||
} catch (ClientExceptionInterface $exception) {
|
||||
throw new HttpFetchException(trans('errors.cannot_get_image_from_url', ['url' => $url]), $exception->getCode(), $exception);
|
||||
}
|
||||
|
||||
@@ -127,7 +129,7 @@ class UserAvatars
|
||||
{
|
||||
$fetchUrl = $this->getAvatarUrl();
|
||||
|
||||
return is_string($fetchUrl) && strpos($fetchUrl, 'http') === 0;
|
||||
return str_starts_with($fetchUrl, 'http');
|
||||
}
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user