Importing: Simplify TunnelImporter
Signed-off-by: Roopesh Chander <roop@roopc.net>
This commit is contained in:
@@ -9,86 +9,77 @@ class TunnelImporter {
|
|||||||
completionHandler?()
|
completionHandler?()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if urls.count > 1 {
|
let dispatchGroup = DispatchGroup()
|
||||||
let dispatchGroup = DispatchGroup()
|
var configs = [TunnelConfiguration?]()
|
||||||
var configs = [TunnelConfiguration?]()
|
var lastFileImportErrorText: (title: String, message: String)?
|
||||||
for url in urls {
|
for url in urls {
|
||||||
if url.pathExtension.lowercased() == "zip" {
|
if url.pathExtension.lowercased() == "zip" {
|
||||||
dispatchGroup.enter()
|
dispatchGroup.enter()
|
||||||
ZipImporter.importConfigFiles(from: url) { result in
|
ZipImporter.importConfigFiles(from: url) { result in
|
||||||
if let configsInZip = result.value {
|
if let error = result.error {
|
||||||
configs.append(contentsOf: configsInZip)
|
lastFileImportErrorText = error.alertText
|
||||||
|
}
|
||||||
|
if let configsInZip = result.value {
|
||||||
|
configs.append(contentsOf: configsInZip)
|
||||||
|
}
|
||||||
|
dispatchGroup.leave()
|
||||||
|
}
|
||||||
|
} 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 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()
|
dispatchGroup.leave()
|
||||||
}
|
}
|
||||||
} 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)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
dispatchGroup.notify(queue: .main) {
|
|
||||||
tunnelsManager.addMultiple(tunnelConfigurations: configs.compactMap { $0 }) { numberSuccessful in
|
|
||||||
if numberSuccessful == configs.count {
|
|
||||||
completionHandler?()
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
assert(urls.count == 1)
|
dispatchGroup.notify(queue: .main) {
|
||||||
let url = urls.first!
|
tunnelsManager.addMultiple(tunnelConfigurations: configs.compactMap { $0 }) { numberSuccessful in
|
||||||
if url.pathExtension.lowercased() == "zip" {
|
if !configs.isEmpty && numberSuccessful == configs.count {
|
||||||
ZipImporter.importConfigFiles(from: url) { result in
|
completionHandler?()
|
||||||
if let error = result.error {
|
|
||||||
errorPresenterType.showErrorAlert(error: error, from: sourceVC)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
let configs = result.value!
|
let title: String
|
||||||
tunnelsManager.addMultiple(tunnelConfigurations: configs.compactMap { $0 }) { numberSuccessful in
|
let message: String
|
||||||
if numberSuccessful == configs.count {
|
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?()
|
completionHandler?()
|
||||||
return
|
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 message: String
|
|
||||||
if let cocoaError = error as? CocoaError, cocoaError.isFileError {
|
|
||||||
message = error.localizedDescription
|
|
||||||
} else {
|
} else {
|
||||||
message = tr(format: "alertCantOpenInputConfFileMessage (%@)", fileName)
|
title = tr(format: "alertImportedFromMultipleFilesTitle (%d)", numberSuccessful)
|
||||||
|
message = tr(format: "alertImportedFromMultipleFilesMessage (%1$d of %2$d)", numberSuccessful, configs.count)
|
||||||
}
|
}
|
||||||
errorPresenterType.showErrorAlert(title: tr("alertCantOpenInputConfFileTitle"), message: message, from: sourceVC, onPresented: completionHandler)
|
errorPresenterType.showErrorAlert(title: title, 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)
|
|
||||||
} else {
|
|
||||||
completionHandler?()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
errorPresenterType.showErrorAlert(title: tr("alertBadConfigImportTitle"), message: tr(format: "alertBadConfigImportMessage (%@)", fileName),
|
|
||||||
from: sourceVC, onPresented: completionHandler)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user