Merge pull request #811 from kiwix/789-zim-metadata-illustration-is-not-read-properly

Fix favIcons of opened ZIM files (not added via catalog download)
This commit is contained in:
Kelson 2024-06-17 13:28:50 +02:00 committed by GitHub
commit 16a1320615
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 21 additions and 5 deletions

View File

@ -34,6 +34,7 @@
// nullable attributes
@property (nonatomic, strong, nullable) NSURL *downloadURL;
@property (nonatomic, strong, nullable) NSURL *faviconURL;
@property (nonatomic, strong, nullable) NSData *faviconData;
@property (nonatomic, strong, nullable) NSString *flavor;
// assigned attributes

View File

@ -59,6 +59,7 @@
SAFE_READ(self.downloadURL, [self getURL:_book->getUrl()]);
SAFE_READ(self.faviconURL, [self getFaviconURLFromBook:_book]);
SAFE_READ(self.faviconData, [self getFaviconDataFromBook:_book]);
SAFE_READ(self.flavor, [self getFlavorFromBook:_book]);
SAFE_READ_BOOL(self.hasDetails, _book->getTagBool("details"));
@ -99,7 +100,7 @@
return [NSURL URLWithString:[NSString stringWithUTF8String:urlString.c_str()]];
}
- (NSURL *)getFaviconURLFromBook:(kiwix::Book *)book {
- (NSURL * _Nullable)getFaviconURLFromBook:(kiwix::Book *)book {
try {
std::string url = book->getIllustrations().at(0)->url;
return [self getURL:url];
@ -108,6 +109,19 @@
}
}
- (NSData * _Nullable)getFaviconDataFromBook:(kiwix::Book *)book {
try {
std::string dataString = book->getIllustrations().at(0)->getData();
if(dataString.length() == 0) {
return nil;
}
NSData *favIconData = [NSData dataWithBytes: dataString.data() length: dataString.length()];
return favIconData;
} catch (std::exception) {
return nil;
}
}
- (NSString *)getFlavorFromBook:(kiwix::Book *)book {
NSString *flavor = [NSString stringWithUTF8String:book->getFlavour().c_str()];
return [flavor stringByReplacingOccurrencesOfString:@"_" withString:@""];

View File

@ -129,12 +129,13 @@ struct LibraryOperations {
zimFile.requiresServiceWorkers = metadata.requiresServiceWorkers
zimFile.size = metadata.size.int64Value
// Only overwrite favicon data and url if there is a new value
if let url = metadata.downloadURL { zimFile.downloadURL = url }
if let url = metadata.faviconURL { zimFile.faviconURL = url }
// Overwrite these, only if there are new values
if let faviconURL = metadata.faviconURL { zimFile.faviconURL = faviconURL }
if let faviconData = metadata.faviconData { zimFile.faviconData = faviconData }
if let downloadURL = metadata.downloadURL { zimFile.downloadURL = downloadURL }
}
//MARK: - Deletion
// MARK: - Deletion
/// Unlink a zim file from library, delete associated bookmarks, and delete the file.
/// - Parameter zimFile: the zim file to delete