|
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) { |
|
|
|
|
|
|
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); |
|
|
|
|
|
|
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
|
|
|
|
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.