MP3.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Georg Ehrke <oc.list@georgehrke.com>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Lukas Reschke <lukas@statuscode.ch>
  9. * @author Olivier Paroz <github@oparoz.com>
  10. * @author Robin Appelman <robin@icewind.nl>
  11. * @author Roeland Jago Douma <roeland@famdouma.nl>
  12. * @author Thomas Tanghus <thomas@tanghus.net>
  13. *
  14. * @license AGPL-3.0
  15. *
  16. * This code is free software: you can redistribute it and/or modify
  17. * it under the terms of the GNU Affero General Public License, version 3,
  18. * as published by the Free Software Foundation.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU Affero General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Affero General Public License, version 3,
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>
  27. *
  28. */
  29. namespace OC\Preview;
  30. use OCP\Files\File;
  31. use OCP\IImage;
  32. use wapmorgan\Mp3Info\Mp3Info;
  33. use function OCP\Log\logger;
  34. class MP3 extends ProviderV2 {
  35. /**
  36. * {@inheritDoc}
  37. */
  38. public function getMimeType(): string {
  39. return '/audio\/mpeg/';
  40. }
  41. /**
  42. * {@inheritDoc}
  43. */
  44. public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage {
  45. $tmpPath = $this->getLocalFile($file);
  46. try {
  47. $audio = new Mp3Info($tmpPath, true);
  48. /** @var string|null|false $picture */
  49. $picture = $audio->getCover();
  50. } catch (\Throwable $e) {
  51. logger('core')->info('Error while getting cover from mp3 file: ' . $e->getMessage(), [
  52. 'fileId' => $file->getId(),
  53. 'filePath' => $file->getPath(),
  54. ]);
  55. return null;
  56. } finally {
  57. $this->cleanTmpFiles();
  58. }
  59. if (is_string($picture)) {
  60. $image = new \OCP\Image();
  61. $image->loadFromData($picture);
  62. if ($image->valid()) {
  63. $image->scaleDownToFit($maxX, $maxY);
  64. return $image;
  65. }
  66. }
  67. return null;
  68. }
  69. }