From 8a359fb7a602fc616b7af7b3f89356ddf3f3cde7 Mon Sep 17 00:00:00 2001 From: Michal Smaga Date: Wed, 16 Oct 2024 11:27:54 +0200 Subject: [PATCH] Extend logic to resolve download MIMEtype by also looking at file extension --- DuckDuckGo/DownloadMetadata.swift | 2 +- DuckDuckGo/MIMEType.swift | 12 +++++++++++- DuckDuckGo/TabViewController.swift | 4 ++-- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/DuckDuckGo/DownloadMetadata.swift b/DuckDuckGo/DownloadMetadata.swift index 0b793ee795..e5dc290a38 100644 --- a/DuckDuckGo/DownloadMetadata.swift +++ b/DuckDuckGo/DownloadMetadata.swift @@ -32,7 +32,7 @@ struct DownloadMetadata { self.filename = filename self.expectedContentLength = response.expectedContentLength self.mimeTypeSource = response.mimeType ?? "" - self.mimeType = MIMEType(from: response.mimeType) + self.mimeType = MIMEType(from: response.mimeType, fileExtension: filename.pathExtension) self.url = url } } diff --git a/DuckDuckGo/MIMEType.swift b/DuckDuckGo/MIMEType.swift index 29a7c47813..93a0c8b4bf 100644 --- a/DuckDuckGo/MIMEType.swift +++ b/DuckDuckGo/MIMEType.swift @@ -33,7 +33,17 @@ enum MIMEType: String { init(from string: String?) { self = MIMEType(rawValue: string ?? "") ?? .unknown } - + + init(from string: String?, fileExtension: String?) { + let initialMIMEType = MIMEType(from: string) + + switch (initialMIMEType, fileExtension) { + case (.octetStream, "pkpass"): self = .passbook + case (.octetStream, "pkpasses"): self = .multipass + default: self = initialMIMEType + } + } + var isHTML: Bool { switch self { case .html, .xhtml: diff --git a/DuckDuckGo/TabViewController.swift b/DuckDuckGo/TabViewController.swift index 5d444d735e..2983f6ba57 100644 --- a/DuckDuckGo/TabViewController.swift +++ b/DuckDuckGo/TabViewController.swift @@ -1309,7 +1309,7 @@ extension TabViewController: WKNavigationDelegate { decidePolicyFor navigationResponse: WKNavigationResponse, decisionHandler: @escaping (WKNavigationResponsePolicy) -> Void) { - let mimeType = MIMEType(from: navigationResponse.response.mimeType) + let mimeType = MIMEType(from: navigationResponse.response.mimeType, fileExtension: navigationResponse.response.url?.pathExtension) let urlSchemeType = navigationResponse.response.url.map { SchemeHandler.schemeType(for: $0) } ?? .unknown let httpResponse = navigationResponse.response as? HTTPURLResponse @@ -1369,7 +1369,7 @@ extension TabViewController: WKNavigationDelegate { } private func shouldTriggerDownloadAction(for navigationResponse: WKNavigationResponse) -> Bool { - let mimeType = MIMEType(from: navigationResponse.response.mimeType) + let mimeType = MIMEType(from: navigationResponse.response.mimeType, fileExtension: navigationResponse.response.url?.pathExtension) let httpResponse = navigationResponse.response as? HTTPURLResponse // HTTP response has "Content-Disposition: attachment" header