Merge pull request #820 from kiwix/814-export-of-a-pdf-fails

Fix share/export content depending on it's type
This commit is contained in:
Kelson 2024-06-19 19:10:25 +02:00 committed by GitHub
commit 1e3f9ce441
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 20 additions and 3 deletions

View File

@ -166,6 +166,10 @@ struct URLContentMetaData {
var isMediaType: Bool {
mime.hasPrefix("video/") || mime.hasPrefix("audio/")
}
var isTextType: Bool {
mime.hasPrefix("text/")
}
}
struct URLContent {

View File

@ -68,6 +68,7 @@ final class BrowserViewModel: NSObject, ObservableObject,
}
}
@Published var externalURL: URL?
private var metaData: URLContentMetaData?
private(set) var tabID: NSManagedObjectID? {
didSet {
@ -158,15 +159,27 @@ final class BrowserViewModel: NSObject, ObservableObject,
}
}
/// Get the webpage in a binary format
/// - Returns: PDF of the current page (if text type) or binary data of the content
func pdfData() async -> Data? {
if metaData?.isTextType == true {
return try? await webView.pdf()
} else if let url = await webView.url {
return ZimFileService.shared.getURLContent(url: url)?.data
}
return nil
}
private func didUpdate(title: String, url: URL) {
let zimFile: ZimFile? = {
guard let zimFileID = UUID(uuidString: url.host ?? "") else { return nil }
return try? Database.viewContext.fetch(ZimFile.fetchRequest(fileID: zimFileID)).first
}()
metaData = ZimFileService.shared.getContentMetaData(url: url)
// update view model
if title.isEmpty {
articleTitle = ZimFileService.shared.getContentMetaData(url: url)?.zimTitle ?? ""
articleTitle = metaData?.zimTitle ?? ""
} else {
articleTitle = title
}
@ -268,7 +281,7 @@ final class BrowserViewModel: NSObject, ObservableObject,
// MARK: - WKNavigationDelegate
// swiftlint:disable:next function_body_length
// swiftlint:disable:next function_body_length cyclomatic_complexity
func webView(
_ webView: WKWebView,
decidePolicyFor navigationAction: WKNavigationAction,

View File

@ -23,7 +23,7 @@ struct ShareButton: View {
guard let browserURLName = browser.webView.url?.lastPathComponent else {
return nil
}
guard let pdfData = try? await browser.webView.pdf() else {
guard let pdfData = await browser.pdfData() else {
return nil
}
return (pdfData, browserURLName)