Completed
Push — master ( 81153d...2a6722 )
by Thomas
52s
created

MP3::getThumbnail()   C

Complexity

Conditions 8
Paths 48

Size

Total Lines 34
Code Lines 22

Duplication

Lines 9
Ratio 26.47 %

Importance

Changes 0
Metric Value
cc 8
eloc 22
nc 48
nop 4
dl 9
loc 34
rs 5.3846
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author Georg Ehrke <[email protected]>
4
 * @author Hendrik Leppelsack <[email protected]>
5
 * @author Joas Schilling <[email protected]>
6
 * @author Lukas Reschke <[email protected]>
7
 * @author Olivier Paroz <[email protected]>
8
 * @author Thomas Tanghus <[email protected]>
9
 *
10
 * @copyright Copyright (c) 2018, ownCloud GmbH
11
 * @license AGPL-3.0
12
 *
13
 * This code is free software: you can redistribute it and/or modify
14
 * it under the terms of the GNU Affero General Public License, version 3,
15
 * as published by the Free Software Foundation.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20
 * GNU Affero General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License, version 3,
23
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
24
 *
25
 */
26
namespace OC\Preview;
27
28
use ID3Parser\ID3Parser;
29
use OCP\Files\File;
30
use OCP\Preview\IProvider2;
31
32
class MP3 implements IProvider2 {
33
	/**
34
	 * {@inheritDoc}
35
	 */
36
	public function getMimeType() {
37
		return '/audio\/mpeg/';
38
	}
39
40
	/**
41
	 * {@inheritDoc}
42
	 */
43
	public function getThumbnail(File $file, $maxX, $maxY, $scalingUp) {
44
45
		$useFileDirectly = (!$file->isEncrypted() && !$file->isMounted());
46 View Code Duplication
		if ($useFileDirectly) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
47
			$absPath = $file->getStorage()->getLocalFile($file->getInternalPath());
48
		} else {
49
			$absPath = \OC::$server->getTempManager()->getTemporaryFile();
50
51
			$handle = $file->fopen('rb');
52
			file_put_contents($absPath, $handle);
53
			fclose($handle);
54
		}
55
56
		$getID3 = new ID3Parser();
57
		$tags = $getID3->analyze($absPath);
0 ignored issues
show
Security Bug introduced by
It seems like $absPath defined by $file->getStorage()->get...ile->getInternalPath()) on line 47 can also be of type false; however, ID3Parser\ID3Parser::analyze() does only seem to accept string, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
58
		unlink($absPath);
59
		$picture = isset($tags['id3v2']['APIC'][0]['data']) ? $tags['id3v2']['APIC'][0]['data'] : null;
60
		if(is_null($picture) && isset($tags['id3v2']['PIC'][0]['data'])) {
61
			$picture = $tags['id3v2']['PIC'][0]['data'];
62
		}
63
64
		if(!is_null($picture)) {
65
			$image = new \OC_Image();
66
			$image->loadFromData($picture);
67
68
			if ($image->valid()) {
69
				$image->scaleDownToFit($maxX, $maxY);
70
71
				return $image;
72
			}
73
		}
74
75
		return $this->getNoCoverThumbnail();
76
	}
77
78
	/**
79
	 * Generates a default image when the file has no cover
80
	 *
81
	 * @return bool|\OCP\IImage false if the default image is missing or invalid
82
	 */
83
	private function getNoCoverThumbnail() {
84
		$icon = \OC::$SERVERROOT . '/core/img/filetypes/audio.svg';
85
86
		if(!file_exists($icon)) {
87
			return false;
88
		}
89
90
		$image = new \OC_Image();
91
		$image->loadFromFile($icon);
92
		return $image->valid() ? $image : false;
93
	}
94
95
	/**
96
	 * Check if a preview can be generated for $path
97
	 *
98
	 * @param File $file
99
	 * @return bool
100
	 * @since 10.1.0
101
	 */
102
	public function isAvailable(File $file) {
103
		return true;
104
	}
105
}
106