diff options
author | Roopesh Chander <roop@roopc.net> | 2018-12-29 01:36:46 +0530 |
---|---|---|
committer | Roopesh Chander <roop@roopc.net> | 2019-01-14 14:52:29 +0530 |
commit | 6a27626fc003399b1f542b7ef64ea838001375dc (patch) | |
tree | 97306ad99b7c4fdd429a464068d3caa457f3e47e /WireGuard/WireGuard/UI/TunnelImporter.swift | |
parent | macOS: Add tunnel management menu items (diff) | |
download | wireguard-apple-6a27626fc003399b1f542b7ef64ea838001375dc.tar.xz wireguard-apple-6a27626fc003399b1f542b7ef64ea838001375dc.zip |
iOS: Refactor importFromFile
So that it can be used in macOS as well
Signed-off-by: Roopesh Chander <roop@roopc.net>
Diffstat (limited to 'WireGuard/WireGuard/UI/TunnelImporter.swift')
-rw-r--r-- | WireGuard/WireGuard/UI/TunnelImporter.swift | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/WireGuard/WireGuard/UI/TunnelImporter.swift b/WireGuard/WireGuard/UI/TunnelImporter.swift new file mode 100644 index 0000000..4a31504 --- /dev/null +++ b/WireGuard/WireGuard/UI/TunnelImporter.swift @@ -0,0 +1,43 @@ +// SPDX-License-Identifier: MIT +// Copyright © 2018 WireGuard LLC. All Rights Reserved. + +import Foundation + +class TunnelImporter { + static func importFromFile(url: URL, into tunnelsManager: TunnelsManager, sourceVC: AnyObject?, errorPresenterType: ErrorPresenterProtocol.Type, completionHandler: (() -> Void)? = nil) { + if url.pathExtension == "zip" { + ZipImporter.importConfigFiles(from: url) { result in + if let error = result.error { + errorPresenterType.showErrorAlert(error: error, from: sourceVC) + 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 fileBaseName = url.deletingPathExtension().lastPathComponent.trimmingCharacters(in: .whitespacesAndNewlines) + if let fileContents = try? String(contentsOf: url), + 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("alertUnableToImportTitle"), message: tr("alertUnableToImportMessage"), + from: sourceVC, onPresented: completionHandler) + } + } + } + +} |