64 lines
2.0 KiB
PHP
64 lines
2.0 KiB
PHP
<?php
|
|
|
|
namespace App\Download\Downloader;
|
|
|
|
use App\Download\Framework\Entity\Download;
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
use Symfony\Component\Process\Exception\ProcessFailedException;
|
|
use Symfony\Component\Process\Process;
|
|
|
|
class ProcessDownloader implements DownloaderInterface
|
|
{
|
|
public function __construct(
|
|
private EntityManagerInterface $entityManager,
|
|
) {}
|
|
|
|
/**
|
|
* @inheritDoc
|
|
*/
|
|
public function download(string $baseDir, string $title, string $url, ?int $downloadId): void
|
|
{
|
|
/** @var Download $downloadEntity */
|
|
$downloadEntity = $this->entityManager->getRepository(Download::class)->find($downloadId);
|
|
$downloadEntity->setProgress(0);
|
|
$this->entityManager->flush();
|
|
|
|
$process = new Process([
|
|
'/bin/sh',
|
|
'/var/www/bash/app/wget_download.sh',
|
|
$baseDir,
|
|
$title,
|
|
$url
|
|
]);
|
|
|
|
$process->setTimeout(1800); // 30 min
|
|
$process->setIdleTimeout(600); // 10 min
|
|
|
|
$process->start();
|
|
|
|
try {
|
|
$progress = 0;
|
|
$this->entityManager->flush();
|
|
$process->wait(function ($type, $buffer) use ($progress, $downloadEntity): void {
|
|
if (Process::ERR === $type) {
|
|
$pregMatchOutput = [];
|
|
preg_match('/[\d]+%/', $buffer, $pregMatchOutput);
|
|
|
|
if (!empty($pregMatchOutput)) {
|
|
if ($pregMatchOutput[0] !== $progress) {
|
|
$progress = (int) $pregMatchOutput[0];
|
|
$downloadEntity->setProgress($progress);
|
|
$this->entityManager->flush();
|
|
}
|
|
}
|
|
}
|
|
fwrite(STDOUT, $buffer);
|
|
});
|
|
$downloadEntity->setProgress(100);
|
|
} catch (ProcessFailedException $exception) {
|
|
$downloadEntity->setStatus('Failed');
|
|
}
|
|
|
|
$this->entityManager->flush();
|
|
}
|
|
} |