clean up a bit download service

This commit is contained in:
dece 2024-09-01 00:31:23 +02:00
parent 3d355039fe
commit e97bbc1bc1
4 changed files with 43 additions and 29 deletions

View file

@ -2,8 +2,6 @@
namespace App\Command;
use App\Constants;
use App\Entity\Episode;
use App\Repository\PodcastRepository;
use App\Service\DownloadService;
use Doctrine\ORM\EntityManagerInterface;
@ -24,7 +22,7 @@ class DownloadCommand extends Command
{
public function __construct(
protected DownloadService $downloadService,
protected EntityManagerInterface $entityManager,
protected EntityManagerInterface $em,
protected PodcastRepository $podcastRepository,
protected ParameterBagInterface $parameterBag,
) {
@ -55,30 +53,22 @@ class DownloadCommand extends Command
return Command::FAILURE;
}
$filepath = $this->downloadService->download($url);
if (false === $filepath) {
$episode = $this->downloadService->download($url);
if (false === $episode) {
$io->error('Download failed.');
return Command::FAILURE;
}
$filename = basename($filepath);
$publicFilepath =
$this->parameterBag->get('kernel.project_dir')
.'/'
.Constants::BASE_PUBLIC_DIR
.Constants::FILES_BASE_PATH
.$filename;
rename($filepath, $publicFilepath);
$episode = new Episode();
$episode->setTitle($input->getOption('title') ?: $filename);
$episode->setDescription($input->getOption('description'));
$episode->setAudioFilename($filename);
$episode->setPodcast($podcast);
$episode->setPublicationDate(date_create());
$this->entityManager->persist($episode);
$this->entityManager->flush();
$episode->setDescription($input->getOption('description'));
// Override only if the option has been passed, because the download service sets a default title.
if (($title = $input->getOption('title')) !== null) {
$episode->setTitle($title);
}
$this->em->persist($episode);
$this->em->flush();
$io->success("New Episode '{$episode->getTitle()}' added.");

View file

@ -3,7 +3,6 @@
namespace App\Entity;
use App\Constants;
use App\Entity\Podcast;
use App\Repository\EpisodeRepository;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
@ -99,10 +98,10 @@ class Episode
public function getAudioUrl(): ?string
{
if ($this->audioFilename === null) {
if (null === $this->audioFilename) {
return null;
}
return Constants::FILES_BASE_PATH . $this->audioFilename;
return Constants::FILES_BASE_PATH.$this->audioFilename;
}
}

View file

@ -2,21 +2,46 @@
namespace App\Service;
use App\Constants;
use App\Entity\Episode;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
/** Generate episodes from external sources. */
class DownloadService
{
public function __construct(
protected LoggerInterface $logger,
protected YoutubeService $youtubeService,
) {}
#[Autowire('%kernel.project_dir%')] protected string $projectDir,
) {
}
/**
* Download the audio from this URL and return its filepath.
* Download the audio from this URL and save the corresponding episode.
*/
public function download(string $url): string|false
public function download(string $url): Episode|false
{
// For now assume every URL is a Youtube URL.
return $this->youtubeService->download($url);
$filepath = $this->youtubeService->download($url);
if (false === $filepath) {
return false;
}
$filename = basename($filepath);
$publicFilepath =
$this->projectDir
.'/'
.Constants::BASE_PUBLIC_DIR
.Constants::FILES_BASE_PATH
.$filename;
rename($filepath, $publicFilepath);
$episode = new Episode();
$episode->setTitle($filename);
$episode->setAudioFilename($filename);
$episode->setPublicationDate(date_create());
return $episode;
}
}

View file

@ -19,7 +19,7 @@ class YoutubeService
'-x',
'-O', 'after_move:filepath', '--restrict-filenames',
'-P', sys_get_temp_dir(),
escapeshellarg($url),
$url,
]);
try {
$process->mustRun();