diff options
author | Roopesh Chander <roop@roopc.net> | 2018-11-15 13:29:49 +0530 |
---|---|---|
committer | Roopesh Chander <roop@roopc.net> | 2018-11-15 13:39:56 +0530 |
commit | 1dac181803c5b673bfbc9729281027628c0bb97b (patch) | |
tree | b7070145b20df45ec7cd2ff63528fc08540c0a34 /WireGuard/WireGuard | |
parent | Exporting: No need to check for duplicate names - we disallow it at creation time itself (diff) | |
download | wireguard-apple-1dac181803c5b673bfbc9729281027628c0bb97b.tar.xz wireguard-apple-1dac181803c5b673bfbc9729281027628c0bb97b.zip |
Exporting: Refactor out zip exporting into a separate class
Signed-off-by: Roopesh Chander <roop@roopc.net>
Diffstat (limited to 'WireGuard/WireGuard')
-rw-r--r-- | WireGuard/WireGuard/UI/iOS/ErrorPresenter.swift | 6 | ||||
-rw-r--r-- | WireGuard/WireGuard/UI/iOS/SettingsTableViewController.swift | 32 | ||||
-rw-r--r-- | WireGuard/WireGuard/ZipArchive/ZipExporter.swift | 28 |
3 files changed, 45 insertions, 21 deletions
diff --git a/WireGuard/WireGuard/UI/iOS/ErrorPresenter.swift b/WireGuard/WireGuard/UI/iOS/ErrorPresenter.swift index abc0083..8aa0c1a 100644 --- a/WireGuard/WireGuard/UI/iOS/ErrorPresenter.swift +++ b/WireGuard/WireGuard/UI/iOS/ErrorPresenter.swift @@ -32,6 +32,12 @@ class ErrorPresenter { case ZipImporterError.noTunnelsInZipArchive: return ("No tunnels in zip archive", "No .conf tunnel files were found inside the zip archive.") + // Exporting a zip file + case ZipArchiveError.cantOpenOutputZipFileForWriting: + return ("Unable to create zip archive", "Could not create a zip file in the app's document directory.") + case ZipExporterError.noTunnelsToExport: + return ("Nothing to export", "There are no tunnels to export") + default: os_log("ErrorPresenter: Error not presented: %{public}@", log: OSLog.default, type: .error, "\(error)") return nil diff --git a/WireGuard/WireGuard/UI/iOS/SettingsTableViewController.swift b/WireGuard/WireGuard/UI/iOS/SettingsTableViewController.swift index 4024213..dc2e27e 100644 --- a/WireGuard/WireGuard/UI/iOS/SettingsTableViewController.swift +++ b/WireGuard/WireGuard/UI/iOS/SettingsTableViewController.swift @@ -63,20 +63,7 @@ class SettingsTableViewController: UITableViewController { } func exportConfigurationsAsZipFile(sourceView: UIView) { - guard let tunnelsManager = tunnelsManager, tunnelsManager.numberOfTunnels() > 0 else { - showErrorAlert(title: "Nothing to export", message: "There are no tunnels to export") - return - } - var inputsToArchiver: [(fileName: String, contents: Data)] = [] - for i in 0 ..< tunnelsManager.numberOfTunnels() { - guard let tunnelConfiguration = tunnelsManager.tunnel(at: i).tunnelConfiguration() else { continue } - if let contents = WgQuickConfigFileWriter.writeConfigFile(from: tunnelConfiguration) { - let name = tunnelConfiguration.interface.name - assert(name != tunnelsManager.tunnel(at: i - 1).name) - inputsToArchiver.append((fileName: "\(name).conf", contents: contents)) - } - } - + guard let tunnelsManager = tunnelsManager else { return } guard let destinationDir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first else { return } @@ -87,16 +74,19 @@ class SettingsTableViewController: UITableViewController { os_log("Failed to delete file: %{public}@ : %{public}@", log: OSLog.default, type: .error, destinationURL.absoluteString, error.localizedDescription) } + let count = tunnelsManager.numberOfTunnels() + let tunnelConfigurations = (0 ..< count).compactMap { tunnelsManager.tunnel(at: $0).tunnelConfiguration() } do { - try ZipArchive.archive(inputs: inputsToArchiver, to: destinationURL) - let activityVC = UIActivityViewController(activityItems: [destinationURL], applicationActivities: nil) - // popoverPresentationController shall be non-nil on the iPad - activityVC.popoverPresentationController?.sourceView = sourceView - activityVC.popoverPresentationController?.sourceRect = sourceView.bounds - present(activityVC, animated: true) + try ZipExporter.exportConfigFiles(tunnelConfigurations: tunnelConfigurations, to: destinationURL) } catch (let error) { - showErrorAlert(title: "Unable to export", message: "There was an error exporting the tunnel configuration archive: \(String(describing: error))") + ErrorPresenter.showErrorAlert(error: error, from: self) } + + let activityVC = UIActivityViewController(activityItems: [destinationURL], applicationActivities: nil) + // popoverPresentationController shall be non-nil on the iPad + activityVC.popoverPresentationController?.sourceView = sourceView + activityVC.popoverPresentationController?.sourceRect = sourceView.bounds + present(activityVC, animated: true) } func showErrorAlert(title: String, message: String) { diff --git a/WireGuard/WireGuard/ZipArchive/ZipExporter.swift b/WireGuard/WireGuard/ZipArchive/ZipExporter.swift new file mode 100644 index 0000000..d0cf9a7 --- /dev/null +++ b/WireGuard/WireGuard/ZipArchive/ZipExporter.swift @@ -0,0 +1,28 @@ +// SPDX-License-Identifier: MIT +// Copyright © 2018 WireGuard LLC. All Rights Reserved. + +import UIKit + +enum ZipExporterError: Error { + case noTunnelsToExport +} + +class ZipExporter { + static func exportConfigFiles(tunnelConfigurations: [TunnelConfiguration], to destinationURL: URL) throws { + + guard (!tunnelConfigurations.isEmpty) else { throw ZipExporterError.noTunnelsToExport } + + var inputsToArchiver: [(fileName: String, contents: Data)] = [] + + var lastTunnelName: String = "" + for tunnelConfiguration in tunnelConfigurations { + if let contents = WgQuickConfigFileWriter.writeConfigFile(from: tunnelConfiguration) { + let name = tunnelConfiguration.interface.name + if (name.isEmpty || name == lastTunnelName) { continue } + inputsToArchiver.append((fileName: "\(name).conf", contents: contents)) + lastTunnelName = name + } + } + try ZipArchive.archive(inputs: inputsToArchiver, to: destinationURL) + } +} |