Skip to content
This repository has been archived by the owner on Feb 5, 2024. It is now read-only.

Commit

Permalink
Fix reSmush throwing request exception
Browse files Browse the repository at this point in the history
  • Loading branch information
bvtterfly committed Jun 15, 2022
1 parent 2fc3820 commit 16f9213
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
26 changes: 16 additions & 10 deletions src/Optimizers/ReSmushOptimizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Bvtterfly\Lio\Contracts\HasConfig;
use Bvtterfly\Lio\Contracts\Image;
use Bvtterfly\Lio\Contracts\Optimizer;
use Illuminate\Http\Client\RequestException;
use Illuminate\Http\Client\Response;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Http;
Expand Down Expand Up @@ -69,7 +70,7 @@ public function setTimeout(int $timeout): Optimizer
return $this;
}

private function upload(): Response
private function upload(): ?Response
{
$file = fopen($this->imagePath, 'r');

Expand All @@ -78,11 +79,13 @@ private function upload(): Response
'exif' => $this->getExif(),
]);

return Http::attach(
'files',
$file,
basename($this->imagePath)
)->timeout($this->timeout)->retry($this->getRetry())->post(self::ENDPOINT.'?'.$params);
return rescue(
fn () => Http::attach('files', $file, basename($this->imagePath))
->timeout($this->timeout)
->retry($this->getRetry())
->post(self::ENDPOINT.'?'.$params),
fn ($e) => $e instanceof RequestException ? $e->response : null
);
}

public function run(): void
Expand All @@ -91,7 +94,7 @@ public function run(): void

$result = $this->upload();

if (! $result->successful()) {
if (! $result?->successful()) {
$this->logger?->error('Failed to upload image: ' . $result->body());

return;
Expand All @@ -103,14 +106,17 @@ public function run(): void

$destinationPath = Arr::get($json, 'dest');

$downloadResponse = Http::timeout($this->timeout)->retry($this->getRetry())->get($destinationPath);
$downloadResponse = rescue(
fn () => Http::timeout($this->timeout)->retry($this->getRetry())->get($destinationPath),
fn ($e) => $e instanceof RequestException ? $e->response : null
);

if ($downloadResponse->successful()) {
if ($downloadResponse?->successful()) {
file_put_contents($this->imagePath, $downloadResponse->body());
$this->logger->info('Image Optimized successfully');
} else {
$this->logger->error('Failed to download image from: '. $destinationPath);
$this->logger->error('Error: '. $downloadResponse->body());
$this->logger->error('Error: '. $downloadResponse?->body());
}
}

Expand Down
2 changes: 0 additions & 2 deletions tests/OptimizerChainTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,10 @@
/** @var OptimizerChain $optimizerChain */
$optimizerChain = app(OptimizerChain::class);
$optimizerChain->optimize('test.png', 'opt-test.png');
decreasedFilesystemFileSize('opt-test.png', 'test.png');
$logger = $optimizerChain->getLogger();
expect($logger)->toBeInstanceOf(ArrayLogger::class)
->getAllLinesAsString()
->toContain('reSmush')
->toContain('Image Optimized successfully')
->not
->toContain('jpegoptim');
});
Expand Down

0 comments on commit 16f9213

Please sign in to comment.