Importing: Simplify TunnelImporter
This commit is contained in:
parent
8dbe8fb733
commit
4e476dadb3
|
@ -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)
|
||||
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
|
||||
}
|
||||
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()
|
||||
}
|
||||
} 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)
|
||||
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 {
|
||||
let title: String
|
||||
let message: String
|
||||
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
|
||||
}
|
||||
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 {
|
||||
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)
|
||||
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)
|
||||
errorPresenterType.showErrorAlert(title: title, message: message, from: sourceVC, onPresented: completionHandler)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue