Skip to content

Commit fd584e6

Browse files
authored
Fix: Remove files that are larger than expected (#74)
1 parent d7c1ebc commit fd584e6

3 files changed

Lines changed: 35 additions & 11 deletions

File tree

src/Command/DownloadCommand.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use App\Enum\NamingConvention;
99
use App\Enum\Setting;
1010
use App\Exception\ExitException;
11+
use App\Exception\ForceRetryException;
1112
use App\Exception\InvalidValueException;
1213
use App\Exception\TooManyRetriesException;
1314
use App\Exception\UnreadableFileException;
@@ -33,6 +34,8 @@
3334
use Symfony\Component\Console\Input\InputOption;
3435
use Symfony\Component\Console\Output\OutputInterface;
3536
use Symfony\Component\Console\Style\SymfonyStyle;
37+
use Symfony\Component\HttpClient\Exception\ClientException;
38+
use Symfony\Component\HttpFoundation\Response;
3639

3740
#[AsCommand('download')]
3841
final class DownloadCommand extends Command
@@ -303,10 +306,19 @@ protected function execute(InputInterface $input, OutputInterface $output): int
303306
} catch (UnreadableFileException) {
304307
$hash = hash_init('md5');
305308
}
306-
foreach ($responses as $response) {
307-
$chunk = $response->getContent();
308-
$writer->writeChunk($targetFile, $chunk, $chunkSize);
309-
hash_update($hash, $chunk);
309+
try {
310+
foreach ($responses as $response) {
311+
$chunk = $response->getContent();
312+
$writer->writeChunk($targetFile, $chunk, $chunkSize);
313+
hash_update($hash, $chunk);
314+
}
315+
} catch (ClientException $e) {
316+
if ($e->getCode() === Response::HTTP_REQUESTED_RANGE_NOT_SATISFIABLE) {
317+
$writer->remove($targetFile);
318+
throw new ForceRetryException();
319+
}
320+
321+
throw $e;
310322
}
311323
$hash = hash_final($hash);
312324
$writer->finalizeWriting($targetFile, $hash);
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace App\Exception;
4+
5+
use RuntimeException;
6+
7+
final class ForceRetryException extends RuntimeException
8+
{
9+
}

src/Service/RetryService.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace App\Service;
44

5+
use App\Exception\ForceRetryException;
56
use App\Exception\TooManyRetriesException;
67
use Exception;
78
use Throwable;
@@ -27,13 +28,15 @@ public function retry(callable $callable, int $maxRetries, int $retryDelay, ?arr
2728

2829
return;
2930
} catch (Exception $e) {
30-
$thrown[] = $e;
31-
++$retries;
32-
if (!$this->matches($e, $exceptions)) {
33-
throw $e;
34-
}
35-
if ($ignoreExceptions && $this->matches($e, $ignoreExceptions)) {
36-
throw $e;
31+
if (!$e instanceof ForceRetryException) {
32+
$thrown[] = $e;
33+
++$retries;
34+
if (!$this->matches($e, $exceptions)) {
35+
throw $e;
36+
}
37+
if ($ignoreExceptions && $this->matches($e, $ignoreExceptions)) {
38+
throw $e;
39+
}
3740
}
3841
sleep($retryDelay);
3942
}

0 commit comments

Comments
 (0)