[Tag Reading] Moved external cover art reader

Moved external cover art reader to a place where it can be used for any
format, even formats unsupported by Metadata Reader interfaces.

Signed-off-by: Christopher Snowhill <kode54@gmail.com>
lastfm
Christopher Snowhill 2022-07-02 14:59:19 -07:00
parent b72d5a4c26
commit 7f1c337ee8
3 changed files with 75 additions and 61 deletions

View File

@ -642,6 +642,19 @@ static NSString *xmlEscapeString(NSString * string) {
return [[decoder alloc] init]; return [[decoder alloc] init];
} }
+ (BOOL)isCoverFile:(NSString *)fileName {
for(NSString *coverFileName in [PluginController coverNames]) {
if([[[[fileName lastPathComponent] stringByDeletingPathExtension] lowercaseString] hasSuffix:coverFileName]) {
return true;
}
}
return false;
}
+ (NSArray *)coverNames {
return @[@"cover", @"folder", @"album", @"front"];
}
- (NSDictionary *)metadataForURL:(NSURL *)url skipCue:(BOOL)skip { - (NSDictionary *)metadataForURL:(NSURL *)url skipCue:(BOOL)skip {
NSString *urlScheme = [url scheme]; NSString *urlScheme = [url scheme];
if([urlScheme isEqualToString:@"http"] || if([urlScheme isEqualToString:@"http"] ||
@ -651,40 +664,76 @@ static NSString *xmlEscapeString(NSString * string) {
NSDictionary *cacheData = cache_access_metadata(url); NSDictionary *cacheData = cache_access_metadata(url);
if(cacheData) return cacheData; if(cacheData) return cacheData;
NSString *ext = [url pathExtension]; do {
NSArray *readers = [metadataReaders objectForKey:[ext lowercaseString]]; NSString *ext = [url pathExtension];
NSString *classString; NSArray *readers = [metadataReaders objectForKey:[ext lowercaseString]];
if(readers) { NSString *classString;
if([readers count] > 1) { if(readers) {
if(skip) { if([readers count] > 1) {
NSMutableArray *_readers = [readers mutableCopy]; if(skip) {
for(int i = 0; i < [_readers count];) { NSMutableArray *_readers = [readers mutableCopy];
if([[_readers objectAtIndex:i] isEqualToString:@"CueSheetMetadataReader"]) for(int i = 0; i < [_readers count];) {
[_readers removeObjectAtIndex:i]; if([[_readers objectAtIndex:i] isEqualToString:@"CueSheetMetadataReader"])
else [_readers removeObjectAtIndex:i];
++i; else
++i;
}
cacheData = [CogMetadataReaderMulti metadataForURL:url readers:_readers];
break;
} }
cacheData = [CogMetadataReaderMulti metadataForURL:url readers:_readers]; cacheData = [CogMetadataReaderMulti metadataForURL:url readers:readers];
cache_insert_metadata(url, cacheData); break;
return cacheData; } else {
classString = [readers objectAtIndex:0];
} }
cacheData = [CogMetadataReaderMulti metadataForURL:url readers:readers];
cache_insert_metadata(url, cacheData);
return cacheData;
} else { } else {
classString = [readers objectAtIndex:0]; cacheData = nil;
break;
} }
} else {
return nil; if(skip && [classString isEqualToString:@"CueSheetMetadataReader"]) {
cacheData = nil;
break;
}
Class metadataReader = NSClassFromString(classString);
cacheData = [metadataReader metadataForURL:url];
} while(0);
if(cacheData == nil) {
cacheData = [NSDictionary dictionary];
} }
if(skip && [classString isEqualToString:@"CueSheetMetadataReader"]) { if(cacheData) {
return nil; NSData *image = [cacheData objectForKey:@"albumArt"];
if(nil == image) {
// Try to load image from external file
NSString *path = [[url path] stringByDeletingLastPathComponent];
// Gather list of candidate image files
NSArray *fileNames = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:nil];
NSArray *types = @[@"jpg", @"jpeg", @"png", @"gif", @"webp", @"avif"];
NSArray *imageFileNames = [fileNames pathsMatchingExtensions:types];
for(NSString *fileName in imageFileNames) {
if([PluginController isCoverFile:fileName]) {
image = [NSData dataWithContentsOfFile:[path stringByAppendingPathComponent:fileName]];
break;
}
}
if(image) {
NSMutableDictionary *data = [cacheData mutableCopy];
[data setValue:image forKey:@"albumArt"];
cacheData = data;
}
}
} }
Class metadataReader = NSClassFromString(classString);
cacheData = [metadataReader metadataForURL:url];
cache_insert_metadata(url, cacheData); cache_insert_metadata(url, cacheData);
return cacheData; return cacheData;
} }

View File

@ -13,7 +13,4 @@
@interface TagLibMetadataReader : NSObject <CogMetadataReader> { @interface TagLibMetadataReader : NSObject <CogMetadataReader> {
} }
+ (BOOL)isCoverFile:(NSString *)fileName;
+ (NSArray *)coverNames;
@end @end

View File

@ -196,25 +196,6 @@
} }
} }
if(nil == image) {
// Try to load image from external file
NSString *path = [[url path] stringByDeletingLastPathComponent];
// Gather list of candidate image files
NSArray *fileNames = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:nil];
NSArray *types = @[@"jpg", @"jpeg", @"png", @"gif", @"webp", @"avif"];
NSArray *imageFileNames = [fileNames pathsMatchingExtensions:types];
for(NSString *fileName in imageFileNames) {
if([TagLibMetadataReader isCoverFile:fileName]) {
image = [NSData dataWithContentsOfFile:[path stringByAppendingPathComponent:fileName]];
break;
}
}
}
if(nil != image) { if(nil != image) {
[dict setObject:image forKey:@"albumArt"]; [dict setObject:image forKey:@"albumArt"];
} }
@ -225,19 +206,6 @@
return dict; return dict;
} }
+ (BOOL)isCoverFile:(NSString *)fileName {
for(NSString *coverFileName in [TagLibMetadataReader coverNames]) {
if([[[[fileName lastPathComponent] stringByDeletingPathExtension] lowercaseString] hasSuffix:coverFileName]) {
return true;
}
}
return false;
}
+ (NSArray *)coverNames {
return @[@"cover", @"folder", @"album", @"front"];
}
+ (NSArray *)fileTypes { + (NSArray *)fileTypes {
// May be a way to get a list of supported formats // May be a way to get a list of supported formats
return @[@"ape", @"asf", @"wma", @"ogg", @"opus", @"mpc", @"flac", @"mp3", @"tak", @"ac3", @"apl", @"dts", @"dtshd", @"tta", @"wav", @"aif", @"aiff", @"wv", @"wvp"]; return @[@"ape", @"asf", @"wma", @"ogg", @"opus", @"mpc", @"flac", @"mp3", @"tak", @"ac3", @"apl", @"dts", @"dtshd", @"tta", @"wav", @"aif", @"aiff", @"wv", @"wvp"];