[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,6 +664,7 @@ 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;
do {
NSString *ext = [url pathExtension]; NSString *ext = [url pathExtension];
NSArray *readers = [metadataReaders objectForKey:[ext lowercaseString]]; NSArray *readers = [metadataReaders objectForKey:[ext lowercaseString]];
NSString *classString; NSString *classString;
@ -665,26 +679,61 @@ static NSString *xmlEscapeString(NSString * string) {
++i; ++i;
} }
cacheData = [CogMetadataReaderMulti metadataForURL:url readers:_readers]; cacheData = [CogMetadataReaderMulti metadataForURL:url readers:_readers];
cache_insert_metadata(url, cacheData); break;
return cacheData;
} }
cacheData = [CogMetadataReaderMulti metadataForURL:url readers:readers]; cacheData = [CogMetadataReaderMulti metadataForURL:url readers:readers];
cache_insert_metadata(url, cacheData); break;
return cacheData;
} else { } else {
classString = [readers objectAtIndex:0]; classString = [readers objectAtIndex:0];
} }
} else { } else {
return nil; cacheData = nil;
break;
} }
if(skip && [classString isEqualToString:@"CueSheetMetadataReader"]) { if(skip && [classString isEqualToString:@"CueSheetMetadataReader"]) {
return nil; cacheData = nil;
break;
} }
Class metadataReader = NSClassFromString(classString); Class metadataReader = NSClassFromString(classString);
cacheData = [metadataReader metadataForURL:url]; cacheData = [metadataReader metadataForURL:url];
} while(0);
if(cacheData == nil) {
cacheData = [NSDictionary dictionary];
}
if(cacheData) {
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;
}
}
}
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"];