/
FolderCollectorModule.php
218 lines (190 loc) · 7.77 KB
/
FolderCollectorModule.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
<?php
/*
* vim:set softtabstop=4 shiftwidth=4 expandtab:
*
* LICENSE: GNU Affero General Public License, version 3 (AGPL-3.0-or-later)
* Copyright 2001 - 2022 Ampache.org
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
declare(strict_types=0);
namespace Ampache\Module\Art\Collector;
use Ampache\Config\ConfigContainerInterface;
use Ampache\Repository\Model\Album;
use Ampache\Repository\Model\Art;
use Ampache\Repository\Model\Artist;
use Ampache\Repository\Model\Song;
use Ampache\Repository\Model\Video;
use Ampache\Module\System\AmpError;
use Ampache\Module\System\Core;
use Ampache\Module\System\LegacyLogger;
use Ampache\Repository\SongRepositoryInterface;
use Psr\Log\LoggerInterface;
final class FolderCollectorModule implements CollectorModuleInterface
{
private ConfigContainerInterface $configContainer;
private LoggerInterface $logger;
private SongRepositoryInterface $songRepository;
public function __construct(
ConfigContainerInterface $configContainer,
LoggerInterface $logger,
SongRepositoryInterface $songRepository
) {
$this->configContainer = $configContainer;
$this->logger = $logger;
$this->songRepository = $songRepository;
}
/**
* This returns the art from the folder of the files
* If a limit is passed or the preferred filename is found the current
* results set is returned
*
* @param Art $art
* @param integer $limit
* @param array $data
*
* @return array
*/
public function collect(
Art $art,
int $limit = 5,
array $data = []
): array {
if (!$limit) {
$limit = 5;
}
$results = [];
$preferred = [];
// For storing which directories we've already done
$processed = [];
/* See if we are looking for a specific filename */
$preferred_filename = ($this->configContainer->get('album_art_preferred_filename')) ?: 'folder.jpg';
$artist_filename = $this->configContainer->get('artist_art_preferred_filename');
$artist_art_folder = $this->configContainer->get('artist_art_folder');
$dirs = array();
if ($art->type == 'album') {
$media = new Album($art->uid);
$songs = $this->songRepository->getByAlbum((int) $media->id);
foreach ($songs as $song_id) {
$song = new Song($song_id);
$dirs[] = Core::conv_lc_file(dirname($song->file));
}
} elseif ($art->type == 'video') {
$media = new Video($art->uid);
$dirs[] = Core::conv_lc_file(dirname($media->file));
} elseif ($art->type == 'artist') {
$media = new Artist($art->uid);
$preferred_filename = str_replace(array('<', '>', '\\', '/'), '_', $media->get_fullname());
if ($artist_art_folder) {
$dirs[] = Core::conv_lc_file($artist_art_folder);
}
// get the folders from songs as well
$songs = $this->songRepository->getByArtist((int) $media->id);
foreach ($songs as $song_id) {
$song = new Song($song_id);
// look in the directory name of the files (e.g. /mnt/Music/%artistName%/%album%)
$dirs[] = Core::conv_lc_file(dirname($song->file));
// look one level up (e.g. /mnt/Music/%artistName%)
$dirs[] = Core::conv_lc_file(dirname($song->file, 2));
}
}
foreach ($dirs as $dir) {
if (isset($processed[$dir])) {
continue;
}
$this->logger->notice(
"gather_folder: Opening $dir and checking for " . $art->type . " Art",
[LegacyLogger::CONTEXT_TYPE => __CLASS__]
);
/* Open up the directory */
$handle = opendir($dir);
if (!$handle) {
AmpError::add('general', T_('Unable to open') . ' ' . $dir);
$this->logger->warning(
"gather_folder: Opening $dir and checking for " . $art->type . " Art",
[LegacyLogger::CONTEXT_TYPE => __CLASS__]
);
continue;
}
$processed[$dir] = true;
// Recurse through this dir and create the files array
while (false !== ($file = readdir($handle))) {
$extension = pathinfo($file);
$extension = $extension['extension'] ?? '';
// Make sure it looks like an image file
if (!in_array($extension, Art::VALID_TYPES)) {
continue;
}
$full_filename = $dir . '/' . $file;
// Make sure it's got something in it
if (!Core::get_filesize($full_filename)) {
$this->logger->debug(
"gather_folder: Opening $dir and checking for " . $art->type . " Art",
[LegacyLogger::CONTEXT_TYPE => __CLASS__]
);
continue;
}
// Regularize for mime type
if ($extension == 'jpg') {
$extension = 'jpeg';
}
// Take an md5sum so we don't show duplicate files.
$index = md5($full_filename);
if (
(
$file == $preferred_filename ||
pathinfo($file, PATHINFO_FILENAME) == $preferred_filename) ||
(
$file == $artist_filename ||
pathinfo($file, PATHINFO_FILENAME) == $artist_filename
)
) {
// We found the preferred filename and so we're done.
$this->logger->debug(
"gather_folder: Found preferred image file: $file",
[LegacyLogger::CONTEXT_TYPE => __CLASS__]
);
$preferred[$index] = [
'file' => $full_filename,
'mime' => 'image/' . $extension,
'title' => 'Folder'
];
break;
}
if ($art->type !== 'artist') {
$this->logger->debug(
"gather_folder: Found image file: $file",
[LegacyLogger::CONTEXT_TYPE => __CLASS__]
);
$results[$index] = [
'file' => $full_filename,
'mime' => 'image/' . $extension,
'title' => 'Folder'
];
}
} // end while reading dir
closedir($handle);
} // end foreach dirs
if (!empty($preferred)) {
// We found our favorite filename somewhere, so we need
// to dump the other, less sexy ones.
$results = $preferred;
}
if ($limit && count($results) > $limit) {
$results = array_slice($results, 0, $limit);
}
return array_values($results);
}
}