MP3.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. class MP3 extends ProviderV2 {
  34. /**
  35. * {@inheritDoc}
  36. */
  37. public function getMimeType(): string {
  38. return '/audio\/mpeg/';
  39. }
  40. /**
  41. * {@inheritDoc}
  42. */
  43. public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage {
  44. $getID3 = new ID3Parser();
  45. $tmpPath = $this->getLocalFile($file);
  46. $tags = $getID3->analyze($tmpPath);
  47. $this->cleanTmpFiles();
  48. $picture = isset($tags['id3v2']['APIC'][0]['data']) ? $tags['id3v2']['APIC'][0]['data'] : null;
  49. if (is_null($picture) && isset($tags['id3v2']['PIC'][0]['data'])) {
  50. $picture = $tags['id3v2']['PIC'][0]['data'];
  51. }
  52. if (!is_null($picture)) {
  53. $image = new \OCP\Image();
  54. $image->loadFromData($picture);
  55. if ($image->valid()) {
  56. $image->scaleDownToFit($maxX, $maxY);
  57. return $image;
  58. }
  59. }
  60. return null;
  61. }
  62. }