Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[iOS] Handle non-passbook attachment downloads correctly #25651

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ extension BrowserViewController: WKDownloadDelegate {
let temporaryDir = NSTemporaryDirectory()
let fileName = temporaryDir + "/" + suggestedFilename
let url = URL(fileURLWithPath: fileName)

// WKDownload will fail with a -3000 error code if the file already exists at the given path
if await AsyncFileManager.default.fileExists(atPath: url.path(percentEncoded: false)) {
try? await AsyncFileManager.default.removeItem(at: url)
}

let pendingDownload = WebKitDownload(
fileURL: url,
response: response,
Expand Down Expand Up @@ -70,8 +76,6 @@ extension BrowserViewController: WKDownloadDelegate {
return
}

downloadQueue.download(downloadInfo, didFinishDownloadingTo: downloadInfo.fileURL)

let response = URLResponse(
url: downloadInfo.fileURL,
mimeType: downloadInfo.response.mimeType,
Expand All @@ -80,6 +84,7 @@ extension BrowserViewController: WKDownloadDelegate {
)

if downloadInfo.response.mimeType == MIMEType.passbook {
downloadQueue.download(downloadInfo, didFinishDownloadingTo: downloadInfo.fileURL)
if let passbookHelper = OpenPassBookHelper(
request: nil,
response: response,
Expand All @@ -89,12 +94,26 @@ extension BrowserViewController: WKDownloadDelegate {
) {
Task {
await passbookHelper.open()
try await AsyncFileManager.default.removeItem(at: downloadInfo.fileURL)
}
}
return
}

// Handle non-passbook downloads the same as HTTPDownload
let filename = downloadInfo.filename
let location = downloadInfo.fileURL
let temporaryLocation = FileManager.default.temporaryDirectory
.appending(component: "\(filename)-\(location.lastPathComponent)")
try? FileManager.default.moveItem(at: location, to: temporaryLocation)
Task {
try await AsyncFileManager.default.removeItem(at: downloadInfo.fileURL)
do {
let destination = try await downloadInfo.uniqueDownloadPathForFilename(filename)
try await AsyncFileManager.default.moveItem(at: temporaryLocation, to: destination)
downloadQueue.download(downloadInfo, didFinishDownloadingTo: destination)
} catch {
downloadQueue.download(downloadInfo, didCompleteWithError: error)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -756,13 +756,14 @@ extension BrowserViewController: WKNavigationDelegate {
return .download
}

if response.mimeType == MIMEType.passbook {
return .download
}

// Check if this response should be handed off to Passbook.
if shouldDownloadNavigationResponse {
shouldDownloadNavigationResponse = false

if response.mimeType == MIMEType.passbook {
return .download
}
return .download
}

// If the content type is not HTML, create a temporary document so it can be downloaded and
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class Download: NSObject {
func pause() {}
func resume() {}

fileprivate func uniqueDownloadPathForFilename(_ filename: String) async throws -> URL {
func uniqueDownloadPathForFilename(_ filename: String) async throws -> URL {
let downloadsPath = try await AsyncFileManager.default.downloadsPath()
let basePath = downloadsPath.appending(path: filename)
let fileExtension = basePath.pathExtension
Expand Down
Loading