Completed
Push — master ( 90c853...9b930c )
by Lukas
08:55
created

MP3::getThumbnail()   B

Complexity

Conditions 6
Paths 12

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 24
rs 8.5125
cc 6
eloc 15
nc 12
nop 5
1
<?php
2
/**
3
 * @author Georg Ehrke <[email protected]>
4
 * @author Joas Schilling <[email protected]>
5
 * @author Olivier Paroz <[email protected]>
6
 * @author Thomas Tanghus <[email protected]>
7
 *
8
 * @copyright Copyright (c) 2016, ownCloud, Inc.
9
 * @license AGPL-3.0
10
 *
11
 * This code is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU Affero General Public License, version 3,
13
 * as published by the Free Software Foundation.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
 * GNU Affero General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Affero General Public License, version 3,
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
22
 *
23
 */
24
namespace OC\Preview;
25
26
use ID3Parser\ID3Parser;
27
28
class MP3 extends Provider {
29
	/**
30
	 * {@inheritDoc}
31
	 */
32
	public function getMimeType() {
33
		return '/audio\/mpeg/';
34
	}
35
36
	/**
37
	 * {@inheritDoc}
38
	 */
39
	public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview) {
40
		$getID3 = new ID3Parser();
41
42
		$tmpPath = $fileview->toTmpFile($path);
43
		$tags = $getID3->analyze($tmpPath);
44
		unlink($tmpPath);
45
		$picture = isset($tags['id3v2']['APIC'][0]['data']) ? $tags['id3v2']['APIC'][0]['data'] : null;
46
		if(is_null($picture) && isset($tags['id3v2']['PIC'][0]['data'])) {
47
			$picture = $tags['id3v2']['PIC'][0]['data'];
48
		}
49
50
		if(!is_null($picture)) {
51
			$image = new \OC_Image();
52
			$image->loadFromData($picture);
53
54
			if ($image->valid()) {
55
				$image->scaleDownToFit($maxX, $maxY);
56
57
				return $image;
58
			}
59
		}
60
61
		return $this->getNoCoverThumbnail();
62
	}
63
64
	/**
65
	 * Generates a default image when the file has no cover
66
	 *
67
	 * @return bool|\OCP\IImage false if the default image is missing or invalid
68
	 */
69
	private function getNoCoverThumbnail() {
70
		$icon = \OC::$SERVERROOT . '/core/img/filetypes/audio.png';
71
72
		if(!file_exists($icon)) {
73
			return false;
74
		}
75
76
		$image = new \OC_Image();
77
		$image->loadFromFile($icon);
78
		return $image->valid() ? $image : false;
79
	}
80
81
}
82