MP3.php 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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 wapmorgan\Mp3Info\Mp3Info;
  11. use function OCP\Log\logger;
  12. class MP3 extends ProviderV2 {
  13. /**
  14. * {@inheritDoc}
  15. */
  16. public function getMimeType(): string {
  17. return '/audio\/mpeg/';
  18. }
  19. /**
  20. * {@inheritDoc}
  21. */
  22. public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage {
  23. $tmpPath = $this->getLocalFile($file);
  24. try {
  25. $audio = new Mp3Info($tmpPath, true);
  26. /** @var string|null|false $picture */
  27. $picture = $audio->getCover();
  28. } catch (\Throwable $e) {
  29. logger('core')->info('Error while getting cover from mp3 file: ' . $e->getMessage(), [
  30. 'fileId' => $file->getId(),
  31. 'filePath' => $file->getPath(),
  32. ]);
  33. return null;
  34. } finally {
  35. $this->cleanTmpFiles();
  36. }
  37. if (is_string($picture)) {
  38. $image = new \OCP\Image();
  39. $image->loadFromData($picture);
  40. if ($image->valid()) {
  41. $image->scaleDownToFit($maxX, $maxY);
  42. return $image;
  43. }
  44. }
  45. return null;
  46. }
  47. }