MP3.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OC\Preview;
  8. use OCP\Files\File;
  9. use OCP\IImage;
  10. use OCP\Server;
  11. use Psr\Log\LoggerInterface;
  12. use wapmorgan\Mp3Info\Mp3Info;
  13. use function OCP\Log\logger;
  14. class MP3 extends ProviderV2 {
  15. /**
  16. * {@inheritDoc}
  17. */
  18. public function getMimeType(): string {
  19. return '/audio\/mpeg/';
  20. }
  21. /**
  22. * {@inheritDoc}
  23. */
  24. public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage {
  25. $tmpPath = $this->getLocalFile($file);
  26. if ($tmpPath === false) {
  27. Server::get(LoggerInterface::class)->error(
  28. 'Failed to get local file to generate thumbnail for: ' . $file->getPath(),
  29. ['app' => 'core']
  30. );
  31. return null;
  32. }
  33. try {
  34. $audio = new Mp3Info($tmpPath, true);
  35. /** @var string|null|false $picture */
  36. $picture = $audio->getCover();
  37. } catch (\Throwable $e) {
  38. logger('core')->info('Error while getting cover from mp3 file: ' . $e->getMessage(), [
  39. 'fileId' => $file->getId(),
  40. 'filePath' => $file->getPath(),
  41. ]);
  42. return null;
  43. } finally {
  44. $this->cleanTmpFiles();
  45. }
  46. if (is_string($picture)) {
  47. $image = new \OCP\Image();
  48. $image->loadFromData($picture);
  49. if ($image->valid()) {
  50. $image->scaleDownToFit($maxX, $maxY);
  51. return $image;
  52. }
  53. }
  54. return null;
  55. }
  56. }