4
0
mirror of https://github.com/AzuraCast/AzuraCast.git synced 2024-06-18 06:57:05 +00:00
AzuraCast/src/Entity/Repository/PodcastRepository.php

142 lines
4.1 KiB
PHP
Raw Normal View History

<?php
declare(strict_types=1);
namespace App\Entity\Repository;
use App\Doctrine\ReloadableEntityManagerInterface;
use App\Doctrine\Repository;
2021-07-19 05:53:45 +00:00
use App\Entity;
use App\Exception\StorageLocationFullException;
use App\Media\AlbumArt;
use Azura\Files\ExtendedFilesystemInterface;
use League\Flysystem\UnableToDeleteFile;
use League\Flysystem\UnableToRetrieveMetadata;
2021-07-19 05:53:45 +00:00
/**
* @extends Repository<Entity\Podcast>
*/
final class PodcastRepository extends Repository
{
public function __construct(
ReloadableEntityManagerInterface $entityManager,
private readonly PodcastEpisodeRepository $podcastEpisodeRepo,
) {
parent::__construct($entityManager);
}
2021-07-19 05:53:45 +00:00
public function fetchPodcastForStation(Entity\Station $station, string $podcastId): ?Entity\Podcast
{
return $this->fetchPodcastForStorageLocation($station->getPodcastsStorageLocation(), $podcastId);
}
public function fetchPodcastForStorageLocation(
2021-07-19 05:53:45 +00:00
Entity\StorageLocation $storageLocation,
string $podcastId
2021-07-19 05:53:45 +00:00
): ?Entity\Podcast {
return $this->repository->findOneBy(
[
'id' => $podcastId,
'storage_location' => $storageLocation,
]
);
}
/**
2021-07-19 05:53:45 +00:00
* @return Entity\Podcast[]
*/
2021-07-19 05:53:45 +00:00
public function fetchPublishedPodcastsForStation(Entity\Station $station): array
{
$podcasts = $this->em->createQuery(
<<<'DQL'
SELECT p, pe
FROM App\Entity\Podcast p
LEFT JOIN p.episodes pe
WHERE p.storage_location = :storageLocation
DQL
)->setParameter('storageLocation', $station->getPodcastsStorageLocation())
->getResult();
return array_filter(
$podcasts,
2021-07-19 05:53:45 +00:00
static function (Entity\Podcast $podcast) {
foreach ($podcast->getEpisodes() as $episode) {
if ($episode->isPublished()) {
return true;
}
}
return false;
}
);
}
public function writePodcastArt(
2021-07-19 05:53:45 +00:00
Entity\Podcast $podcast,
string $rawArtworkString,
?ExtendedFilesystemInterface $fs = null
): void {
$storageLocation = $podcast->getStorageLocation();
$fs ??= $storageLocation->getFilesystem();
$podcastArtworkString = AlbumArt::resize($rawArtworkString);
$podcastArtworkSize = strlen($podcastArtworkString);
if (!$storageLocation->canHoldFile($podcastArtworkSize)) {
throw new StorageLocationFullException();
}
2021-07-19 05:53:45 +00:00
$podcastArtworkPath = Entity\Podcast::getArtPath($podcast->getIdRequired());
$fs->write($podcastArtworkPath, $podcastArtworkString);
$storageLocation->addStorageUsed($podcastArtworkSize);
$this->em->persist($storageLocation);
$podcast->setArtUpdatedAt(time());
$this->em->persist($podcast);
}
public function removePodcastArt(
2021-07-19 05:53:45 +00:00
Entity\Podcast $podcast,
?ExtendedFilesystemInterface $fs = null
): void {
$storageLocation = $podcast->getStorageLocation();
$fs ??= $storageLocation->getFilesystem();
2021-07-19 05:53:45 +00:00
$artworkPath = Entity\Podcast::getArtPath($podcast->getIdRequired());
try {
$size = $fs->fileSize($artworkPath);
} catch (UnableToRetrieveMetadata) {
$size = 0;
}
try {
$fs->delete($artworkPath);
} catch (UnableToDeleteFile) {
}
$storageLocation->removeStorageUsed($size);
$this->em->persist($storageLocation);
$podcast->setArtUpdatedAt(0);
$this->em->persist($podcast);
}
public function delete(
2021-07-19 05:53:45 +00:00
Entity\Podcast $podcast,
?ExtendedFilesystemInterface $fs = null
): void {
$fs ??= $podcast->getStorageLocation()->getFilesystem();
foreach ($podcast->getEpisodes() as $episode) {
$this->podcastEpisodeRepo->delete($episode, $fs);
}
$this->removePodcastArt($podcast, $fs);
$this->em->remove($podcast);
$this->em->flush();
}
}