more error handlings (downloadFile)

This commit is contained in:
Dusk 2025-03-30 13:54:36 +08:00
parent 56054b75fb
commit 7d5fd0c90c

View file

@ -16,6 +16,8 @@ struct HeartbeatApp: App {
@State private var isPairing = false
@State private var heartBeat = false
@State private var error: Int32? = nil
@State private var show_error = false
@State private var error_string = ""
@StateObject private var mount = MountingProgress.shared
let urls: [String] = [
@ -126,11 +128,23 @@ struct HeartbeatApp: App {
for (index, urlString) in urls.enumerated() {
let destinationURL = URL.documentsDirectory.appendingPathComponent(outputFiles[index])
if !fileManager.fileExists(atPath: destinationURL.path) {
downloadFile(from: urlString, to: destinationURL)
downloadFile(from: urlString, to: destinationURL){ result in
if (result != ""){
error_string = "[Download DDI Error]: " + result
show_error = true
}
}
}
}
}
.alert("An Error Occurred", isPresented: $show_error) {
Button("OK", role: .cancel) { }
} message: {
Text(error_string)
}
}
}
}
@ -439,19 +453,21 @@ public func showAlert(title: String, message: String, showOk: Bool, showTryAgain
}
}
func downloadFile(from urlString: String, to destinationURL: URL) {
func downloadFile(from urlString: String, to destinationURL: URL,completion: @escaping (String) -> Void){
let fileManager = FileManager.default
let documentsDirectory = URL.documentsDirectory
guard let url = URL(string: urlString) else {
print("Invalid URL: \(urlString)")
completion("[Internal Invalid URL error]")
return
}
let task = URLSession.shared.downloadTask(with: url) { (tempLocalUrl, response, error) in
guard let tempLocalUrl = tempLocalUrl, error == nil else {
print("Error downloading file from \(urlString): \(String(describing: error))")
completion("Are you connected to the internet? [Download Failed]")
return
}
@ -460,10 +476,13 @@ func downloadFile(from urlString: String, to destinationURL: URL) {
try fileManager.createDirectory(at: destinationURL.deletingLastPathComponent(), withIntermediateDirectories: true, attributes: nil)
try fileManager.moveItem(at: tempLocalUrl, to: destinationURL)
print("Downloaded \(urlString) to \(destinationURL.path)")
} catch {
print("Error saving file: \(error)")
}
}
task.resume()
completion("")
}