From 202e7a489096caba89303f7fdae32a57975214cf Mon Sep 17 00:00:00 2001 From: Roopesh Chander Date: Mon, 4 Mar 2019 13:50:06 +0530 Subject: Importing: Simplify TunnelImporter Signed-off-by: Roopesh Chander --- WireGuard/WireGuard/UI/TunnelImporter.swift | 121 +++++++++++++--------------- 1 file changed, 56 insertions(+), 65 deletions(-) (limited to 'WireGuard') diff --git a/WireGuard/WireGuard/UI/TunnelImporter.swift b/WireGuard/WireGuard/UI/TunnelImporter.swift index e51c70d..4fabd07 100644 --- a/WireGuard/WireGuard/UI/TunnelImporter.swift +++ b/WireGuard/WireGuard/UI/TunnelImporter.swift @@ -9,86 +9,77 @@ class TunnelImporter { completionHandler?() return } - if urls.count > 1 { - let dispatchGroup = DispatchGroup() - var configs = [TunnelConfiguration?]() - for url in urls { - if url.pathExtension.lowercased() == "zip" { - dispatchGroup.enter() - ZipImporter.importConfigFiles(from: url) { result in - if let configsInZip = result.value { - configs.append(contentsOf: configsInZip) - } - dispatchGroup.leave() + let dispatchGroup = DispatchGroup() + var configs = [TunnelConfiguration?]() + var lastFileImportErrorText: (title: String, message: String)? + for url in urls { + if url.pathExtension.lowercased() == "zip" { + dispatchGroup.enter() + ZipImporter.importConfigFiles(from: url) { result in + if let error = result.error { + lastFileImportErrorText = error.alertText } - } else { - let fileBaseName = url.deletingPathExtension().lastPathComponent.trimmingCharacters(in: .whitespacesAndNewlines) - let fileContents = try? String(contentsOf: url) - let tunnelConfiguration = try? TunnelConfiguration(fromWgQuickConfig: fileContents ?? "", called: fileBaseName) - configs.append(tunnelConfiguration) + if let configsInZip = result.value { + configs.append(contentsOf: configsInZip) + } + dispatchGroup.leave() } - } - dispatchGroup.notify(queue: .main) { - tunnelsManager.addMultiple(tunnelConfigurations: configs.compactMap { $0 }) { numberSuccessful in - if numberSuccessful == configs.count { - completionHandler?() + } else { /* if it is not a zip, we assume it is a conf */ + let fileName = url.lastPathComponent + let fileBaseName = url.deletingPathExtension().lastPathComponent.trimmingCharacters(in: .whitespacesAndNewlines) + dispatchGroup.enter() + DispatchQueue.global(qos: .userInitiated).async { + let fileContents: String + do { + fileContents = try String(contentsOf: url) + } catch let error { + if let cocoaError = error as? CocoaError, cocoaError.isFileError { + lastFileImportErrorText = (title: tr("alertCantOpenInputConfFileTitle"), message: error.localizedDescription) + } else { + lastFileImportErrorText = (title: tr("alertCantOpenInputConfFileTitle"), message: tr(format: "alertCantOpenInputConfFileMessage (%@)", fileName)) + } + DispatchQueue.main.async { + configs.append(nil) + dispatchGroup.leave() + } return } - let title = tr(format: "alertImportedFromMultipleFilesTitle (%d)", numberSuccessful) - let message = tr(format: "alertImportedFromMultipleFilesMessage (%1$d of %2$d)", numberSuccessful, configs.count) - errorPresenterType.showErrorAlert(title: title, message: message, from: sourceVC, onPresented: completionHandler) + let tunnelConfiguration = try? TunnelConfiguration(fromWgQuickConfig: fileContents, called: fileBaseName) + if tunnelConfiguration == nil { + lastFileImportErrorText = (title: tr("alertBadConfigImportTitle"), message: tr(format: "alertBadConfigImportMessage (%@)", fileName)) + } + DispatchQueue.main.async { + configs.append(tunnelConfiguration) + dispatchGroup.leave() + } } } - return } - assert(urls.count == 1) - let url = urls.first! - if url.pathExtension.lowercased() == "zip" { - ZipImporter.importConfigFiles(from: url) { result in - if let error = result.error { - errorPresenterType.showErrorAlert(error: error, from: sourceVC) + dispatchGroup.notify(queue: .main) { + tunnelsManager.addMultiple(tunnelConfigurations: configs.compactMap { $0 }) { numberSuccessful in + if !configs.isEmpty && numberSuccessful == configs.count { + completionHandler?() return } - let configs = result.value! - tunnelsManager.addMultiple(tunnelConfigurations: configs.compactMap { $0 }) { numberSuccessful in - if numberSuccessful == configs.count { - completionHandler?() - return - } - let title = tr(format: "alertImportedFromZipTitle (%d)", numberSuccessful) - let message = tr(format: "alertImportedFromZipMessage (%1$d of %2$d)", numberSuccessful, configs.count) - errorPresenterType.showErrorAlert(title: title, message: message, from: sourceVC, onPresented: completionHandler) - } - } - } else /* if (url.pathExtension == "conf") -- we assume everything else is a conf */ { - let fileName = url.lastPathComponent - let fileBaseName = url.deletingPathExtension().lastPathComponent.trimmingCharacters(in: .whitespacesAndNewlines) - let fileContents: String - do { - fileContents = try String(contentsOf: url) - } catch let error { + let title: String let message: String - if let cocoaError = error as? CocoaError, cocoaError.isFileError { - message = error.localizedDescription - } else { - message = tr(format: "alertCantOpenInputConfFileMessage (%@)", fileName) - } - errorPresenterType.showErrorAlert(title: tr("alertCantOpenInputConfFileTitle"), message: message, from: sourceVC, onPresented: completionHandler) - return - } - if let tunnelConfiguration = try? TunnelConfiguration(fromWgQuickConfig: fileContents, called: fileBaseName) { - tunnelsManager.add(tunnelConfiguration: tunnelConfiguration) { result in - if let error = result.error { - errorPresenterType.showErrorAlert(error: error, from: sourceVC, onPresented: completionHandler) + if urls.count == 1 { + if urls.first!.pathExtension.lowercased() == "zip" && !configs.isEmpty { + title = tr(format: "alertImportedFromZipTitle (%d)", numberSuccessful) + message = tr(format: "alertImportedFromZipMessage (%1$d of %2$d)", numberSuccessful, configs.count) + } else if let lastFileImportErrorText = lastFileImportErrorText { + title = lastFileImportErrorText.title + message = lastFileImportErrorText.message } else { completionHandler?() + return } + } else { + title = tr(format: "alertImportedFromMultipleFilesTitle (%d)", numberSuccessful) + message = tr(format: "alertImportedFromMultipleFilesMessage (%1$d of %2$d)", numberSuccessful, configs.count) } - } else { - errorPresenterType.showErrorAlert(title: tr("alertBadConfigImportTitle"), message: tr(format: "alertBadConfigImportMessage (%@)", fileName), - from: sourceVC, onPresented: completionHandler) + errorPresenterType.showErrorAlert(title: title, message: message, from: sourceVC, onPresented: completionHandler) } } } - } -- cgit v1.2.3-59-g8ed1b