|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* ownCloud - Music app |
|
5
|
|
|
* |
|
6
|
|
|
* @author Morris Jobke |
|
7
|
|
|
* @copyright 2013 Morris Jobke <[email protected]> |
|
8
|
|
|
* |
|
9
|
|
|
* This library is free software; you can redistribute it and/or |
|
10
|
|
|
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE |
|
11
|
|
|
* License as published by the Free Software Foundation; either |
|
12
|
|
|
* version 3 of the License, or any later version. |
|
13
|
|
|
* |
|
14
|
|
|
* This library is distributed in the hope that it will be useful, |
|
15
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
16
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
17
|
|
|
* GNU AFFERO GENERAL PUBLIC LICENSE for more details. |
|
18
|
|
|
* |
|
19
|
|
|
* You should have received a copy of the GNU Affero General Public |
|
20
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>. |
|
21
|
|
|
* |
|
22
|
|
|
*/ |
|
23
|
|
|
|
|
24
|
|
|
namespace OCA\Music\BusinessLayer; |
|
25
|
|
|
|
|
26
|
|
|
use \OCA\Music\AppFramework\BusinessLayer\BusinessLayer; |
|
27
|
|
|
use \OCA\Music\AppFramework\Core\Logger; |
|
28
|
|
|
|
|
29
|
|
|
use \OCA\Music\Db\TrackMapper; |
|
30
|
|
|
use \OCA\Music\Db\Track; |
|
31
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
class TrackBusinessLayer extends BusinessLayer { |
|
34
|
|
|
|
|
35
|
|
|
private $logger; |
|
36
|
|
|
|
|
37
|
|
|
public function __construct(TrackMapper $trackMapper, Logger $logger){ |
|
38
|
|
|
parent::__construct($trackMapper); |
|
39
|
|
|
$this->logger = $logger; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Returns all tracks filtered by artist |
|
44
|
|
|
* @param string $artistId the id of the artist |
|
45
|
|
|
* @param string $userId the name of the user |
|
46
|
|
|
* @return array of tracks |
|
47
|
|
|
*/ |
|
48
|
|
|
public function findAllByArtist($artistId, $userId){ |
|
49
|
|
|
return $this->mapper->findAllByArtist($artistId, $userId); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Returns all tracks filtered by album |
|
54
|
|
|
* @param string $albumId the id of the track |
|
55
|
|
|
* @param string $userId the name of the user |
|
56
|
|
|
* @return \OCA\Music\Db\Track[] tracks |
|
57
|
|
|
*/ |
|
58
|
|
|
public function findAllByAlbum($albumId, $userId, $artistId = null){ |
|
59
|
|
|
return $this->mapper->findAllByAlbum($albumId, $userId, $artistId); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Returns the track for a file id |
|
64
|
|
|
* @param string $fileId the file id of the track |
|
65
|
|
|
* @param string $userId the name of the user |
|
66
|
|
|
* @return \OCA\Music\Db\Track track |
|
67
|
|
|
*/ |
|
68
|
|
|
public function findByFileId($fileId, $userId){ |
|
69
|
|
|
return $this->mapper->findByFileId($fileId, $userId); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Returns file IDs of all indexed tracks of the user |
|
74
|
|
|
* @param string $userId |
|
75
|
|
|
* @return int[] |
|
76
|
|
|
*/ |
|
77
|
|
|
public function findAllFileIds($userId){ |
|
78
|
|
|
return $this->mapper->findAllFileIds($userId); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Adds a track if it does not exist already or updates an existing track |
|
83
|
|
|
* @param string $title the title of the track |
|
84
|
|
|
* @param string $number the number of the track |
|
85
|
|
|
* @param string $artistId the artist id of the track |
|
86
|
|
|
* @param string $albumId the album id of the track |
|
87
|
|
|
* @param string $fileId the file id of the track |
|
88
|
|
|
* @param string $mimetype the mimetype of the track |
|
89
|
|
|
* @param string $userId the name of the user |
|
90
|
|
|
* @param int $length track length in seconds |
|
91
|
|
|
* @param int $bitrate track bitrate in bits (not kbits) |
|
92
|
|
|
* @return \OCA\Music\Db\Track The added/updated track |
|
93
|
|
|
*/ |
|
94
|
|
|
public function addOrUpdateTrack( |
|
95
|
|
|
$title, $number, $artistId, $albumId, $fileId, |
|
96
|
|
|
$mimetype, $userId, $length=null, $bitrate=null){ |
|
97
|
|
|
$track = new Track(); |
|
98
|
|
|
$track->setTitle($title); |
|
99
|
|
|
$track->setNumber($number); |
|
100
|
|
|
$track->setArtistId($artistId); |
|
101
|
|
|
$track->setAlbumId($albumId); |
|
102
|
|
|
$track->setFileId($fileId); |
|
103
|
|
|
$track->setMimetype($mimetype); |
|
104
|
|
|
$track->setUserId($userId); |
|
105
|
|
|
$track->setLength($length); |
|
106
|
|
|
$track->setBitrate($bitrate); |
|
107
|
|
|
return $this->mapper->insertOrUpdate($track); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Deletes a track |
|
112
|
|
|
* @param int|int[] $fileIds one or multiple tracks |
|
113
|
|
|
* @param string|null $userId the name of the user; if omitted, the tracks matching the |
|
114
|
|
|
* $fileId are deleted from all users |
|
115
|
|
|
* @return False if no such track was found; otherwise array of six arrays |
|
116
|
|
|
* (named 'deletedTracks', 'remainingAlbums', 'remainingArtists', 'obsoleteAlbums', |
|
117
|
|
|
* 'obsoleteArtists', and 'affectedUsers'). These contain the track, album, artist, and |
|
118
|
|
|
* user IDs of the deleted tracks. The 'obsolete' entities are such which no longer |
|
119
|
|
|
* have any tracks while 'remaining' entities have some left. |
|
120
|
|
|
*/ |
|
121
|
|
|
public function deleteTracks($fileIds, $userId = null){ |
|
122
|
|
|
if(!is_array($fileIds)){ |
|
123
|
|
|
$fileIds = [$fileIds]; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
$tracks = ($userId !== null) |
|
127
|
|
|
? $this->mapper->findByFileIds($fileIds, $userId) |
|
128
|
|
|
: $this->mapper->findAllByFileIds($fileIds); |
|
129
|
|
|
|
|
130
|
|
|
if(count($tracks) === 0){ |
|
131
|
|
|
$result = false; |
|
132
|
|
|
} |
|
133
|
|
|
else{ |
|
134
|
|
|
// delete all the matching tracks |
|
135
|
|
|
$trackIds = array_map(function($t) { return $t->getId(); }, $tracks); |
|
136
|
|
|
$this->deleteById($trackIds); |
|
137
|
|
|
|
|
138
|
|
|
// find all distinct albums, artists, and users of the deleted tracks |
|
139
|
|
|
$artists = []; |
|
140
|
|
|
$albums = []; |
|
141
|
|
|
$users = []; |
|
142
|
|
|
foreach($tracks as $track){ |
|
143
|
|
|
$artists[$track->getArtistId()] = 1; |
|
144
|
|
|
$albums[$track->getAlbumId()] = 1; |
|
145
|
|
|
$users[$track->getUserId()] = 1; |
|
146
|
|
|
} |
|
147
|
|
|
$artists = array_keys($artists); |
|
148
|
|
|
$albums = array_keys($albums); |
|
149
|
|
|
$users = array_keys($users); |
|
150
|
|
|
|
|
151
|
|
|
// categorize each artist as 'remaining' or 'obsolete' |
|
152
|
|
|
$remainingArtists = []; |
|
153
|
|
|
$obsoleteArtists = []; |
|
154
|
|
|
foreach($artists as $artistId){ |
|
155
|
|
|
$result = $this->mapper->countByArtist($artistId); |
|
156
|
|
|
if($result === '0'){ |
|
157
|
|
|
$obsoleteArtists[] = $artistId; |
|
158
|
|
|
}else{ |
|
159
|
|
|
$remainingArtists[] = $artistId; |
|
160
|
|
|
} |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
// categorize each album as 'remaining' or 'obsolete' |
|
164
|
|
|
$remainingAlbums = []; |
|
165
|
|
|
$obsoleteAlbums = []; |
|
166
|
|
|
foreach($albums as $albumId){ |
|
167
|
|
|
$result = $this->mapper->countByAlbum($albumId); |
|
168
|
|
|
if($result === '0'){ |
|
169
|
|
|
$obsoleteAlbums[] = $albumId; |
|
170
|
|
|
}else{ |
|
171
|
|
|
$remainingAlbums[] = $albumId; |
|
172
|
|
|
} |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
$result = [ |
|
176
|
|
|
'deletedTracks' => $trackIds, |
|
177
|
|
|
'remainingAlbums' => $remainingAlbums, |
|
178
|
|
|
'remainingArtists' => $remainingArtists, |
|
179
|
|
|
'obsoleteAlbums' => $obsoleteAlbums, |
|
180
|
|
|
'obsoleteArtists' => $obsoleteArtists, |
|
181
|
|
|
'affectedUsers' => $users |
|
182
|
|
|
]; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
return $result; |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* Returns all tracks filtered by name (of track/album/artist) |
|
190
|
|
|
* @param string $name the name of the track/album/artist |
|
191
|
|
|
* @param string $userId the name of the user |
|
192
|
|
|
* @return \OCA\Music\Db\Track[] tracks |
|
193
|
|
|
*/ |
|
194
|
|
|
public function findAllByNameRecursive($name, $userId){ |
|
195
|
|
|
return $this->mapper->findAllByNameRecursive($name, $userId); |
|
196
|
|
|
} |
|
197
|
|
|
} |
|
198
|
|
|
|