entityManager->getRepository(Download::class)->find($downloadId); $downloadEntity->setProgress(0); $this->entityManager->flush(); $downloadPreferences = $downloadEntity->getUser()->getDownloadPreferences(); $path = $this->getDownloadPath($mediaType, $title, $downloadPreferences); $process = (new Process([ 'wget', $url ]))->setWorkingDirectory($path); $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(); } public function getDownloadPath(string $mediaType, string $title, array $downloadPreferences): string { if ($mediaType === 'movies') { if ((bool) $downloadPreferences['movie_folder']->getPreferenceValue() === true) { return $this->mediaFiles->createMovieDirectory($title); } return $this->mediaFiles->getMoviesPath(); } if ($mediaType === 'tvshows') { return $this->mediaFiles->createTvShowDirectory($title); } throw new \Exception("There is no download path for media type: $mediaType"); } }