|
7 | 7 |
|
8 | 8 | use App\Exceptions\ImageProcessorServiceException;
|
9 | 9 | use App\Models\Beatmapset;
|
| 10 | +use GuzzleHttp\Client; |
| 11 | +use GuzzleHttp\Exception\GuzzleException; |
10 | 12 |
|
11 | 13 | class ImageProcessorService
|
12 | 14 | {
|
@@ -42,15 +44,19 @@ public function process($method, $src)
|
42 | 44 | $src = preg_replace('/https?:\/\//', '', $src);
|
43 | 45 | try {
|
44 | 46 | $tmpFile = tmpfile();
|
45 |
| - $bytesWritten = fwrite($tmpFile, file_get_contents($this->endpoint."/{$method}/{$src}")); |
46 |
| - } catch (\ErrorException $e) { |
47 |
| - if (strpos($e->getMessage(), 'HTTP request failed!') !== false) { |
48 |
| - throw new ImageProcessorServiceException('HTTP request failed!'); |
49 |
| - } elseif (strpos($e->getMessage(), 'Connection refused') !== false) { |
50 |
| - throw new ImageProcessorServiceException('Connection refused.'); |
51 |
| - } else { |
52 |
| - throw $e; |
| 47 | + $bytesWritten = fwrite( |
| 48 | + $tmpFile, |
| 49 | + (new Client())->request('GET', "{$this->endpoint}/{$method}/{$src}")->getBody()->getContents(), |
| 50 | + ); |
| 51 | + } catch (GuzzleException $e) { |
| 52 | + if (str_contains($e->getMessage(), 'VipsJpeg: Premature end of input file')) { |
| 53 | + throw new ImageProcessorServiceException( |
| 54 | + 'Invalid image file', |
| 55 | + ImageProcessorServiceException::INVALID_IMAGE, |
| 56 | + $e, |
| 57 | + ); |
53 | 58 | }
|
| 59 | + throw new ImageProcessorServiceException('HTTP request failed!', 0, $e); |
54 | 60 | }
|
55 | 61 |
|
56 | 62 | if ($bytesWritten === false || $bytesWritten < 100) {
|
|
0 commit comments