Skip to content

Commit 5d4ed9b

Browse files
committed
优化识别封面图片的逻辑
1 parent b517366 commit 5d4ed9b

1 file changed

Lines changed: 25 additions & 13 deletions

File tree

src/main/java/com/lux032/musicautotagger/service/CoverArtService.java

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -217,30 +217,42 @@ private byte[] extractCoverFromAudioFile(File audioFile) {
217217

218218
/**
219219
* 在目录中查找封面图片
220-
* 支持的文件名: cover.jpg, cover.png, folder.jpg, folder.png, album.jpg, album.png
220+
* 支持的文件名: cover/folder/album/front/artwork/albumart/albumartsmall + jpg/jpeg/png/webp
221221
*/
222222
public byte[] findCoverInDirectory(File directory) {
223223
if (directory == null || !directory.exists() || !directory.isDirectory()) {
224224
return null;
225225
}
226226

227227
// 支持的封面文件名(优先级顺序)
228-
String[] coverNames = {"cover", "folder", "album", "front"};
228+
String[] coverNames = {"cover", "folder", "album", "front", "artwork", "albumart", "albumartsmall"};
229229
String[] extensions = {".jpg", ".jpeg", ".png", ".webp"};
230-
230+
231+
File[] files = directory.listFiles();
232+
if (files == null) {
233+
return null;
234+
}
235+
236+
// Case-insensitive match to support Linux filesystems (e.g., Cover.jpg).
231237
for (String coverName : coverNames) {
232238
for (String ext : extensions) {
233-
File coverFile = new File(directory, coverName + ext);
234-
if (coverFile.exists() && coverFile.isFile()) {
235-
try {
236-
byte[] imageData = Files.readAllBytes(coverFile.toPath());
237-
if (imageData != null && imageData.length > 0) {
238-
log.info("找到封面文件: {}", coverFile.getName());
239-
// 压缩图片到2MB以内
240-
return ImageCompressor.compressImage(imageData);
239+
String expectedName = coverName + ext;
240+
for (File file : files) {
241+
if (!file.isFile()) {
242+
continue;
243+
}
244+
String fileNameLower = file.getName().toLowerCase();
245+
if (fileNameLower.equals(expectedName)) {
246+
try {
247+
byte[] imageData = Files.readAllBytes(file.toPath());
248+
if (imageData != null && imageData.length > 0) {
249+
log.info("找到封面文件: {}", file.getName());
250+
// 压缩图片到2MB以内
251+
return ImageCompressor.compressImage(imageData);
252+
}
253+
} catch (Exception e) {
254+
log.debug("读取封面文件失败: {} - {}", file.getName(), e.getMessage());
241255
}
242-
} catch (Exception e) {
243-
log.debug("读取封面文件失败: {} - {}", coverFile.getName(), e.getMessage());
244256
}
245257
}
246258
}

0 commit comments

Comments
 (0)