Skip to content

Commit

Permalink
fix: Album cover defaults to the back cover if there are two embedded…
Browse files Browse the repository at this point in the history
… images in a file (#1121)

Ref #1111
  • Loading branch information
Feichtmeier authored Jan 19, 2025
1 parent 5d68b82 commit 23442cd
Showing 1 changed file with 36 additions and 6 deletions.
42 changes: 36 additions & 6 deletions lib/local_audio/local_cover_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import 'package:path/path.dart' as p;
import 'package:audio_metadata_reader/audio_metadata_reader.dart';
import 'package:collection/collection.dart';

import '../extensions/string_x.dart';

class LocalCoverService {
final _propertiesChangedController = StreamController<bool>.broadcast();
Stream<bool> get propertiesChanged => _propertiesChangedController.stream;
Expand All @@ -20,13 +22,17 @@ class LocalCoverService {
final file = File(path);
if (file.existsSync() && albumId.isNotEmpty == true) {
final metadata = readMetadata(file, getImage: true);
var bytesFromMetadata =
metadata.pictures.firstWhereOrNull((e) => e.bytes.isNotEmpty)?.bytes;
var bytesFromMetadata = metadata.pictures
.firstWhereOrNull(
(e) =>
(e.bytes.isNotEmpty &&
e.pictureType == PictureType.coverFront) ||
e.bytes.isNotEmpty,
)
?.bytes;

if (bytesFromMetadata == null) {
final maybeImageInFolder = _commonCoverInFolderNames.firstWhereOrNull(
(e) => _maybeImageInFolderExists(file: file, suffix: e),
);
final maybeImageInFolder = _getImageInFolder(file);
if (maybeImageInFolder != null) {
bytesFromMetadata = File(maybeImageInFolder).readAsBytesSync();
}
Expand All @@ -46,7 +52,31 @@ class LocalCoverService {
return null;
}

static const _commonCoverInFolderNames = ['front', 'cover', 'album'];
String? _getImageInFolder(File file) =>
_commonCoverInFolderNames.firstWhereOrNull(
(e) => _maybeImageInFolderExists(file: file, suffix: e),
) ??
_commonCoverInFolderNames
.map((e) => e.capitalized)
.toList()
.firstWhereOrNull(
(e) => _maybeImageInFolderExists(file: file, suffix: e),
);

static const _commonCoverInFolderNames = [
'front.jpg',
'front.png',
'front.jpeg',
'cover.jpg',
'cover.png',
'cover.jpeg',
'album.jpg',
'album.png',
'album.jpeg',
'back.jpg',
'back.png',
'back.jpeg',
];

bool _maybeImageInFolderExists({
required File file,
Expand Down

0 comments on commit 23442cd

Please sign in to comment.