1
0

MP3.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 ID3Parser\ID3Parser;
  31. use OCP\Files\File;
  32. use OCP\IImage;
  33. use Psr\Log\LoggerInterface;
  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. $getID3 = new ID3Parser();
  46. $tmpPath = $this->getLocalFile($file);
  47. try {
  48. $tags = $getID3->analyze($tmpPath);
  49. } catch (\Throwable $e) {
  50. \OC::$server->get(LoggerInterface::class)->info($e->getMessage(), [
  51. 'exception' => $e,
  52. 'app' => 'core',
  53. ]);
  54. return null;
  55. } finally {
  56. $this->cleanTmpFiles();
  57. }
  58. $picture = isset($tags['id3v2']['APIC'][0]['data']) ? $tags['id3v2']['APIC'][0]['data'] : null;
  59. if (is_null($picture) && isset($tags['id3v2']['PIC'][0]['data'])) {
  60. $picture = $tags['id3v2']['PIC'][0]['data'];
  61. }
  62. if (!is_null($picture)) {
  63. $image = new \OCP\Image();
  64. $image->loadFromData($picture);
  65. if ($image->valid()) {
  66. $image->scaleDownToFit($maxX, $maxY);
  67. return $image;
  68. }
  69. }
  70. return null;
  71. }
  72. }