aboutsummaryrefslogtreecommitdiffstats
path: root/WireGuard/WireGuard/ZipArchive
diff options
context:
space:
mode:
authorRoopesh Chander <roop@roopc.net>2018-11-14 19:03:33 +0530
committerRoopesh Chander <roop@roopc.net>2018-11-15 13:39:56 +0530
commitb1ae13652cbcc6da016ea0f04b6cd5a9f89eb70a (patch)
treefb2066239cbac07ca9abf98d1674d81cd78fadb6 /WireGuard/WireGuard/ZipArchive
parentImporting: Refactor out zip importing into a separate class (diff)
downloadwireguard-apple-b1ae13652cbcc6da016ea0f04b6cd5a9f89eb70a.tar.xz
wireguard-apple-b1ae13652cbcc6da016ea0f04b6cd5a9f89eb70a.zip
Importing: Import from zip in a background thread
Signed-off-by: Roopesh Chander <roop@roopc.net>
Diffstat (limited to 'WireGuard/WireGuard/ZipArchive')
-rw-r--r--WireGuard/WireGuard/ZipArchive/ZipImporter.swift63
1 files changed, 33 insertions, 30 deletions
diff --git a/WireGuard/WireGuard/ZipArchive/ZipImporter.swift b/WireGuard/WireGuard/ZipArchive/ZipImporter.swift
index 7ee5c32f..4c439d46 100644
--- a/WireGuard/WireGuard/ZipArchive/ZipImporter.swift
+++ b/WireGuard/WireGuard/ZipArchive/ZipImporter.swift
@@ -8,41 +8,44 @@ enum ZipImporterError: Error {
}
class ZipImporter {
- let url: URL
- init(url: URL) {
- self.url = url
- }
+ static func importConfigFiles(from url: URL, completion: @escaping ([TunnelConfiguration?], Error?) -> Void) {
+ DispatchQueue.global(qos: .userInitiated).async {
+ var unarchivedFiles: [(fileName: String, contents: Data)]
+ do {
+ unarchivedFiles = try ZipArchive.unarchive(url: url, requiredFileExtensions: ["conf"])
- func importConfigFiles() throws -> [TunnelConfiguration?] {
- var unarchivedFiles: [(fileName: String, contents: Data)] = try ZipArchive.unarchive(url: url, requiredFileExtensions: ["conf"])
+ for (i, unarchivedFile) in unarchivedFiles.enumerated().reversed() {
+ let fileBaseName = URL(string: unarchivedFile.fileName)?.deletingPathExtension().lastPathComponent
+ if let trimmedName = fileBaseName?.trimmingCharacters(in: .whitespacesAndNewlines), !trimmedName.isEmpty {
+ unarchivedFiles[i].fileName = trimmedName
+ } else {
+ unarchivedFiles.remove(at: i)
+ }
+ }
- for (i, unarchivedFile) in unarchivedFiles.enumerated().reversed() {
- let fileBaseName = URL(string: unarchivedFile.fileName)?.deletingPathExtension().lastPathComponent
- if let trimmedName = fileBaseName?.trimmingCharacters(in: .whitespacesAndNewlines), !trimmedName.isEmpty {
- unarchivedFiles[i].fileName = trimmedName
- } else {
- unarchivedFiles.remove(at: i)
+ if (unarchivedFiles.isEmpty) {
+ throw ZipImporterError.noTunnelsInZipArchive
+ }
+ } catch (let error) {
+ DispatchQueue.main.async { completion([], error) }
+ return
}
- }
-
- if (unarchivedFiles.isEmpty) {
- throw ZipImporterError.noTunnelsInZipArchive
- }
- unarchivedFiles.sort { $0.fileName < $1.fileName }
- var configs = Array<TunnelConfiguration?>(repeating: nil, count: unarchivedFiles.count)
- for (i, file) in unarchivedFiles.enumerated() {
- if (i > 0 && file == unarchivedFiles[i - 1]) {
- continue
- }
- guard let fileContents = String(data: file.contents, encoding: .utf8) else {
- continue
- }
- guard let tunnelConfig = try? WgQuickConfigFileParser.parse(fileContents, name: file.fileName) else {
- continue
+ unarchivedFiles.sort { $0.fileName < $1.fileName }
+ var configs = Array<TunnelConfiguration?>(repeating: nil, count: unarchivedFiles.count)
+ for (i, file) in unarchivedFiles.enumerated() {
+ if (i > 0 && file == unarchivedFiles[i - 1]) {
+ continue
+ }
+ guard let fileContents = String(data: file.contents, encoding: .utf8) else {
+ continue
+ }
+ guard let tunnelConfig = try? WgQuickConfigFileParser.parse(fileContents, name: file.fileName) else {
+ continue
+ }
+ configs[i] = tunnelConfig
}
- configs[i] = tunnelConfig
+ DispatchQueue.main.async { completion(configs, nil) }
}
- return configs
}
}