From 1dac181803c5b673bfbc9729281027628c0bb97b Mon Sep 17 00:00:00 2001 From: Roopesh Chander Date: Thu, 15 Nov 2018 13:29:49 +0530 Subject: Exporting: Refactor out zip exporting into a separate class Signed-off-by: Roopesh Chander --- WireGuard/WireGuard/ZipArchive/ZipExporter.swift | 28 ++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 WireGuard/WireGuard/ZipArchive/ZipExporter.swift (limited to 'WireGuard/WireGuard/ZipArchive/ZipExporter.swift') 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) + } +} -- cgit v1.2.3-59-g8ed1b