Importing: Import from zip in a background thread
This commit is contained in:
parent
b2b2818e2e
commit
1a419526e7
|
@ -160,11 +160,8 @@ class TunnelsListTableViewController: UIViewController {
|
||||||
func importFromFile(url: URL) {
|
func importFromFile(url: URL) {
|
||||||
guard let tunnelsManager = tunnelsManager else { return }
|
guard let tunnelsManager = tunnelsManager else { return }
|
||||||
if (url.pathExtension == "zip") {
|
if (url.pathExtension == "zip") {
|
||||||
let zipImporter = ZipImporter(url: url)
|
ZipImporter.importConfigFiles(from: url) { (configs, error) in
|
||||||
let configs: [TunnelConfiguration?]
|
if let error = error {
|
||||||
do {
|
|
||||||
configs = try zipImporter.importConfigFiles()
|
|
||||||
} catch (let error) {
|
|
||||||
ErrorPresenter.showErrorAlert(error: error, from: self)
|
ErrorPresenter.showErrorAlert(error: error, from: self)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -175,6 +172,7 @@ class TunnelsListTableViewController: UIViewController {
|
||||||
self?.showErrorAlert(title: "Created \(numberSuccessful) tunnels",
|
self?.showErrorAlert(title: "Created \(numberSuccessful) tunnels",
|
||||||
message: "Created \(numberSuccessful) of \(configs.count) tunnels from zip archive")
|
message: "Created \(numberSuccessful) of \(configs.count) tunnels from zip archive")
|
||||||
}
|
}
|
||||||
|
}
|
||||||
} else /* if (url.pathExtension == "conf") -- we assume everything else is a conf */ {
|
} else /* if (url.pathExtension == "conf") -- we assume everything else is a conf */ {
|
||||||
let fileBaseName = url.deletingPathExtension().lastPathComponent.trimmingCharacters(in: .whitespacesAndNewlines)
|
let fileBaseName = url.deletingPathExtension().lastPathComponent.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||||
if let fileContents = try? String(contentsOf: url),
|
if let fileContents = try? String(contentsOf: url),
|
||||||
|
|
|
@ -8,13 +8,11 @@ enum ZipImporterError: Error {
|
||||||
}
|
}
|
||||||
|
|
||||||
class ZipImporter {
|
class ZipImporter {
|
||||||
let url: URL
|
static func importConfigFiles(from url: URL, completion: @escaping ([TunnelConfiguration?], Error?) -> Void) {
|
||||||
init(url: URL) {
|
DispatchQueue.global(qos: .userInitiated).async {
|
||||||
self.url = url
|
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() {
|
for (i, unarchivedFile) in unarchivedFiles.enumerated().reversed() {
|
||||||
let fileBaseName = URL(string: unarchivedFile.fileName)?.deletingPathExtension().lastPathComponent
|
let fileBaseName = URL(string: unarchivedFile.fileName)?.deletingPathExtension().lastPathComponent
|
||||||
|
@ -28,6 +26,10 @@ class ZipImporter {
|
||||||
if (unarchivedFiles.isEmpty) {
|
if (unarchivedFiles.isEmpty) {
|
||||||
throw ZipImporterError.noTunnelsInZipArchive
|
throw ZipImporterError.noTunnelsInZipArchive
|
||||||
}
|
}
|
||||||
|
} catch (let error) {
|
||||||
|
DispatchQueue.main.async { completion([], error) }
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
unarchivedFiles.sort { $0.fileName < $1.fileName }
|
unarchivedFiles.sort { $0.fileName < $1.fileName }
|
||||||
var configs = Array<TunnelConfiguration?>(repeating: nil, count: unarchivedFiles.count)
|
var configs = Array<TunnelConfiguration?>(repeating: nil, count: unarchivedFiles.count)
|
||||||
|
@ -43,6 +45,7 @@ class ZipImporter {
|
||||||
}
|
}
|
||||||
configs[i] = tunnelConfig
|
configs[i] = tunnelConfig
|
||||||
}
|
}
|
||||||
return configs
|
DispatchQueue.main.async { completion(configs, nil) }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue