AzuraCast/src/Sync/Task/CleanupStorageTask.php

124 lines
3.6 KiB
PHP
Raw Normal View History

<?php
2021-07-19 07:53:45 +02:00
declare(strict_types=1);
namespace App\Sync\Task;
use App\Entity;
use Exception;
2021-03-31 18:42:24 +02:00
use League\Flysystem\StorageAttributes;
use Symfony\Component\Finder\Finder;
2022-05-08 20:05:02 +02:00
use Throwable;
2020-11-30 10:55:30 +01:00
class CleanupStorageTask extends AbstractTask
{
public static function getSchedulePattern(): string
{
return '24 * * * *';
}
public function run(bool $force = false): void
{
foreach ($this->iterateStations() as $station) {
2022-01-08 06:32:59 +01:00
try {
/** @var Entity\Station $station */
$this->cleanStationTempFiles($station);
2022-05-08 20:05:02 +02:00
} catch (Throwable $e) {
2022-01-08 06:32:59 +01:00
$this->logger->error($e->getMessage(), [
'station' => (string)$station,
]);
}
}
$storageLocations = $this->iterateStorageLocations(Entity\Enums\StorageLocationTypes::StationMedia);
foreach ($storageLocations as $storageLocation) {
2022-01-08 06:32:59 +01:00
try {
/** @var Entity\StorageLocation $storageLocation */
$this->cleanMediaStorageLocation($storageLocation);
2022-05-08 20:05:02 +02:00
} catch (Throwable $e) {
2022-01-08 06:32:59 +01:00
$this->logger->error($e->getMessage(), [
'storageLocation' => (string)$storageLocation,
]);
}
}
}
protected function cleanStationTempFiles(Entity\Station $station): void
{
$tempDir = $station->getRadioTempDir();
$finder = new Finder();
$finder
->files()
->in($tempDir)
->date('before 2 days ago');
foreach ($finder as $file) {
$file_path = $file->getRealPath();
2021-07-19 07:53:45 +02:00
if (false !== $file_path) {
@unlink($file_path);
}
}
}
protected function cleanMediaStorageLocation(Entity\StorageLocation $storageLocation): void
{
$fs = $storageLocation->getFilesystem();
$allUniqueIdsRaw = $this->em->createQuery(
<<<'DQL'
SELECT sm.unique_id
FROM App\Entity\StationMedia sm
WHERE sm.storage_location = :storageLocation
DQL
)->setParameter('storageLocation', $storageLocation)
->getArrayResult();
$allUniqueIds = [];
foreach ($allUniqueIdsRaw as $row) {
$allUniqueIds[$row['unique_id']] = $row['unique_id'];
}
2020-11-27 02:26:46 +01:00
if (0 === count($allUniqueIds)) {
$this->logger->notice(
2022-05-08 20:05:02 +02:00
sprintf('Skipping storage location %s: no media found.', $storageLocation)
2020-11-27 02:26:46 +01:00
);
return;
}
$removed = [
'albumart' => 0,
'waveform' => 0,
];
$cleanupDirs = [
'albumart' => Entity\StationMedia::DIR_ALBUM_ART,
'waveform' => Entity\StationMedia::DIR_WAVEFORMS,
];
foreach ($cleanupDirs as $key => $dirBase) {
try {
2021-06-08 08:40:49 +02:00
/** @var StorageAttributes $row */
foreach ($fs->listContents($dirBase, true) as $row) {
$path = $row->path();
$filename = pathinfo($path, PATHINFO_FILENAME);
if (!isset($allUniqueIds[$filename])) {
$fs->delete($path);
$removed[$key]++;
}
}
} catch (Exception $e) {
$this->logger->error(
sprintf('Filesystem error: %s', $e->getMessage()),
[
'exception' => $e,
]
);
}
}
$this->logger->info('Storage directory cleanup completed.', $removed);
}
}