Skip to content

Commit 00565ed

Browse files
authored
Merge pull request #11121 from nanaya/optim-error
Ignore invalid beatmap image
2 parents a868182 + 8f0dda1 commit 00565ed

File tree

3 files changed

+24
-10
lines changed

3 files changed

+24
-10
lines changed

app/Exceptions/ImageProcessorServiceException.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@
99

1010
class ImageProcessorServiceException extends Exception
1111
{
12-
// doesn't really contain anything
12+
const INVALID_IMAGE = 1;
1313
}

app/Libraries/ImageProcessorService.php

+14-8
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
use App\Exceptions\ImageProcessorServiceException;
99
use App\Models\Beatmapset;
10+
use GuzzleHttp\Client;
11+
use GuzzleHttp\Exception\GuzzleException;
1012

1113
class ImageProcessorService
1214
{
@@ -42,15 +44,19 @@ public function process($method, $src)
4244
$src = preg_replace('/https?:\/\//', '', $src);
4345
try {
4446
$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+
);
5358
}
59+
throw new ImageProcessorServiceException('HTTP request failed!', 0, $e);
5460
}
5561

5662
if ($bytesWritten === false || $bytesWritten < 100) {

app/Models/Beatmapset.php

+9-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
use App\Enums\Ruleset;
99
use App\Exceptions\BeatmapProcessorException;
10+
use App\Exceptions\ImageProcessorServiceException;
1011
use App\Exceptions\InvariantException;
1112
use App\Jobs\CheckBeatmapsetCovers;
1213
use App\Jobs\EsDocument;
@@ -553,7 +554,14 @@ public function regenerateCovers(array $sizesToRegenerate = null)
553554
$processor = new ImageProcessorService();
554555

555556
// upload optimized full-size version
556-
$optimized = $processor->optimize($this->coverURL('raw', $timestamp));
557+
try {
558+
$optimized = $processor->optimize($this->coverURL('raw', $timestamp));
559+
} catch (ImageProcessorServiceException $e) {
560+
if ($e->getCode() === ImageProcessorServiceException::INVALID_IMAGE) {
561+
return false;
562+
}
563+
throw $e;
564+
}
557565
$this->storeCover('fullsize.jpg', get_stream_filename($optimized));
558566

559567
// use thumbnailer to generate (and then upload) all our variants

0 commit comments

Comments
 (0)