diff --git a/src/Controller/Api/Stations/FilesController.php b/src/Controller/Api/Stations/FilesController.php index 7ba0637fc..bdf1d2a42 100644 --- a/src/Controller/Api/Stations/FilesController.php +++ b/src/Controller/Api/Stations/FilesController.php @@ -245,13 +245,10 @@ class FilesController extends AbstractStationApiCrudController } if ($record instanceof Entity\StationMedia) { + $this->mediaRepo->writeToFile($record); $this->em->persist($record); $this->em->flush(); - if ($this->mediaRepo->writeToFile($record)) { - $record->updateSongId(); - } - if (null !== $custom_fields) { $this->customFieldsRepo->setCustomFields($record, $custom_fields); } diff --git a/src/Entity/Repository/StationMediaRepository.php b/src/Entity/Repository/StationMediaRepository.php index 19e066549..c9217d48a 100644 --- a/src/Entity/Repository/StationMediaRepository.php +++ b/src/Entity/Repository/StationMediaRepository.php @@ -345,14 +345,24 @@ class StationMediaRepository extends Repository } // Write tags to the Media file. + $media->setMtime(time() + 5); + $media->updateSongId(); + return $fs->withLocalFile( $media->getPath(), - function ($path) use ($media, $metadata) { - if ($this->metadataManager->writeMetadata($metadata, $path)) { - $media->setMtime(time() + 5); + function ($path) use ($metadata) { + try { + $this->metadataManager->writeMetadata($metadata, $path); return true; + } catch (CannotProcessMediaException $e) { + $this->logger->error( + $e->getMessage(), + [ + 'exception' => $e, + ] + ); + return false; } - return false; } ); } diff --git a/src/Media/GetId3/GetId3MetadataManager.php b/src/Media/GetId3/GetId3MetadataManager.php index 304f7aa50..f5fd0c40f 100644 --- a/src/Media/GetId3/GetId3MetadataManager.php +++ b/src/Media/GetId3/GetId3MetadataManager.php @@ -24,11 +24,13 @@ class GetId3MetadataManager implements MetadataManagerInterface $info = $id3->analyze($path); if (!empty($info['error'])) { - throw new CannotProcessMediaException(sprintf( - 'Cannot process media file at path "%s": %s', - pathinfo($path, PATHINFO_FILENAME), - json_encode($info['error'], JSON_THROW_ON_ERROR) - )); + throw new CannotProcessMediaException( + sprintf( + 'Cannot process media file at path "%s": %s', + pathinfo($path, PATHINFO_FILENAME), + json_encode($info['error'], JSON_THROW_ON_ERROR) + ) + ); } $metadata = new Metadata(); @@ -121,6 +123,15 @@ class GetId3MetadataManager implements MetadataManagerInterface $tagwriter->tag_data = $tagData; - return $tagwriter->WriteTags(); + $writeTagsResult = $tagwriter->WriteTags(); + + if (false === $writeTagsResult) { + throw CannotProcessMediaException::forPath( + $path, + implode(', ', $tagwriter->errors) + ); + } + + return true; } }