Skip to content

Commit

Permalink
Feat: Add option for maximum bandwidth (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
RikudouSage authored Feb 1, 2025
1 parent 06a9afc commit c97ab92
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 6 deletions.
57 changes: 52 additions & 5 deletions src/Command/DownloadCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use App\Service\RetryService;
use App\Trait\EnumExceptionParserTrait;
use App\Trait\TargetDirectoryTrait;
use InvalidArgumentException;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\ProgressBar;
Expand Down Expand Up @@ -149,6 +150,12 @@ protected function configure()
mode: InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
description: "Don't download the games listed using this flag. The flag can be specified multiple times. Case insensitive, exact match.",
)
->addOption(
name: 'bandwidth',
shortcut: 'b',
mode: InputOption::VALUE_REQUIRED,
description: 'Specify the maximum download speed in bytes. You can use the k postfix for kilobytes or m postfix for megabytes (for example 200k or 4m to mean 200 kilobytes and 4 megabytes respectively)',
)
;
}

Expand Down Expand Up @@ -368,12 +375,27 @@ function (OwnedItemInfo $info) use ($timeout, $output): GameDetail {
$progress->setProgress(0);
$progress->setMessage("{$download->name} ({$download->platform}, {$download->language})");

$responses = $this->downloadManager->download($download, function (int $current, int $total) use ($startAt, $progress, $output) {
if ($total > 0) {
$progress->setMaxSteps($total + ($startAt ?? 0));
$progress->setProgress($current + ($startAt ?? 0));
$curlOptions = [];
if ($input->getOption('bandwidth') && defined('CURLOPT_MAX_RECV_SPEED_LARGE')) {
if (defined('CURLOPT_MAX_RECV_SPEED_LARGE')) {
$curlOptions[CURLOPT_MAX_RECV_SPEED_LARGE] = $this->parseBandwidth($input->getOption('bandwidth'));
} else {
$io->warning("Warning: You have specified a maximum bandwidth, but that's only available if your system has the PHP curl extension installed. Ignoring maximum bandwidth settings.");
}
}, $startAt, $timeout);
}

$responses = $this->downloadManager->download(
download: $download,
callback: function (int $current, int $total) use ($startAt, $progress, $output) {
if ($total > 0) {
$progress->setMaxSteps($total + ($startAt ?? 0));
$progress->setProgress($current + ($startAt ?? 0));
}
},
startAt: $startAt,
httpTimeout: $timeout,
curlOptions: $curlOptions,
);

$hash = $writer->getMd5HashContext($targetFile);
foreach ($responses as $response) {
Expand Down Expand Up @@ -466,4 +488,29 @@ private function dispatchSignals(): void
throw new ExitException('Application has been terminated as requested previously.');
}
}

private function parseBandwidth(string $bandwidth): int
{
if (is_numeric($bandwidth)) {
return (int) $bandwidth;
}

$lower = strtolower($bandwidth);

if (str_ends_with($lower, 'kb') || str_ends_with($lower, 'k')) {
$value = (int) $lower;
$value *= 1024;

return $value;
} else if (str_ends_with($lower, 'mb') || str_ends_with($lower, 'm')) {
$value = (int) $lower;
$value *= 1024 * 1024;

return $value;
} else if (str_ends_with($lower, 'b')) {
return (int) $lower;
}

throw new InvalidArgumentException("Unsupported bandwidth format: {$bandwidth}");
}
}
6 changes: 5 additions & 1 deletion src/Service/DownloadManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public function download(
callable $callback,
?int $startAt = null,
int $httpTimeout = 3,
array $curlOptions = [],
): ResponseStreamInterface {
$response = $this->httpClient->request(
Request::METHOD_GET,
Expand All @@ -67,7 +68,10 @@ public function download(
'auth_bearer' => (string) $this->authentication->getAuthorization(),
'headers' => $headers,
'timeout' => $httpTimeout,
]
'extra' => [
'curl' => $curlOptions,
],
],
);

return $this->httpClient->stream($response);
Expand Down

0 comments on commit c97ab92

Please sign in to comment.