Skip to content

Commit

Permalink
Extend logic to resolve download MIMEtype by also looking at file ext…
Browse files Browse the repository at this point in the history
…ension
  • Loading branch information
miasma13 committed Oct 16, 2024
1 parent 83cdecf commit 8a359fb
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
2 changes: 1 addition & 1 deletion DuckDuckGo/DownloadMetadata.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
12 changes: 11 additions & 1 deletion DuckDuckGo/MIMEType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions DuckDuckGo/TabViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 8a359fb

Please sign in to comment.