Skip to content

Commit

Permalink
Merge pull request #811 from kiwix/789-zim-metadata-illustration-is-n…
Browse files Browse the repository at this point in the history
…ot-read-properly

Fix favIcons of opened ZIM files (not added via catalog download)
  • Loading branch information
kelson42 committed Jun 17, 2024
2 parents dcc86ed + 3a48a8e commit 16a1320
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
1 change: 1 addition & 0 deletions Model/Entities/ZimFileMetaData/ZimFileMetaData.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 15 additions & 1 deletion Model/Entities/ZimFileMetaData/ZimFileMetaData.mm
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ - (nullable instancetype)initWithBook:(void *)book {

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"));
Expand Down Expand Up @@ -99,7 +100,7 @@ - (NSURL *)getURL:(std::string)urlString {
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];
Expand All @@ -108,6 +109,19 @@ - (NSURL *)getFaviconURLFromBook:(kiwix::Book *)book {
}
}

- (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:@""];
Expand Down
9 changes: 5 additions & 4 deletions SwiftUI/Model/LibraryOperations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 16a1320

Please sign in to comment.