Importing: macOS: Support importing of multiple files at a time
Signed-off-by: Roopesh Chander <roop@roopc.net>
This commit is contained in:
parent
b2a2110d8c
commit
a389bd93cb
|
@ -23,6 +23,9 @@
|
||||||
|
|
||||||
// Tunnels list alerts
|
// Tunnels list alerts
|
||||||
|
|
||||||
|
"alertImportedFromMultipleFilesTitle (%d)" = "Created %d tunnels";
|
||||||
|
"alertImportedFromMultipleFilesMessage (%1$d of %2$d)" = "Created %1$d of %2$d tunnels from imported files";
|
||||||
|
|
||||||
"alertImportedFromZipTitle (%d)" = "Created %d tunnels";
|
"alertImportedFromZipTitle (%d)" = "Created %d tunnels";
|
||||||
"alertImportedFromZipMessage (%1$d of %2$d)" = "Created %1$d of %2$d tunnels from zip archive";
|
"alertImportedFromZipMessage (%1$d of %2$d)" = "Created %1$d of %2$d tunnels from zip archive";
|
||||||
|
|
||||||
|
|
|
@ -4,7 +4,45 @@
|
||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
class TunnelImporter {
|
class TunnelImporter {
|
||||||
static func importFromFile(url: URL, into tunnelsManager: TunnelsManager, sourceVC: AnyObject?, errorPresenterType: ErrorPresenterProtocol.Type, completionHandler: (() -> Void)? = nil) {
|
static func importFromFile(urls: [URL], into tunnelsManager: TunnelsManager, sourceVC: AnyObject?, errorPresenterType: ErrorPresenterProtocol.Type, completionHandler: (() -> Void)? = nil) {
|
||||||
|
guard !urls.isEmpty else {
|
||||||
|
completionHandler?()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if urls.count > 1 {
|
||||||
|
let dispatchGroup = DispatchGroup()
|
||||||
|
var configs = [TunnelConfiguration?]()
|
||||||
|
for url in urls {
|
||||||
|
if url.pathExtension.lowercased() == "zip" {
|
||||||
|
dispatchGroup.enter()
|
||||||
|
ZipImporter.importConfigFiles(from: url) { result in
|
||||||
|
if let configsInZip = result.value {
|
||||||
|
configs.append(contentsOf: configsInZip)
|
||||||
|
}
|
||||||
|
dispatchGroup.leave()
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
let fileBaseName = url.deletingPathExtension().lastPathComponent.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||||
|
let fileContents = try? String(contentsOf: url)
|
||||||
|
let tunnelConfiguration = try? TunnelConfiguration(fromWgQuickConfig: fileContents ?? "", called: fileBaseName)
|
||||||
|
configs.append(tunnelConfiguration)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
dispatchGroup.notify(queue: .main) {
|
||||||
|
tunnelsManager.addMultiple(tunnelConfigurations: configs.compactMap { $0 }) { numberSuccessful in
|
||||||
|
if numberSuccessful == configs.count {
|
||||||
|
completionHandler?()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
let title = tr(format: "alertImportedFromMultipleFilesTitle (%d)", numberSuccessful)
|
||||||
|
let message = tr(format: "alertImportedFromMultipleFilesMessage (%1$d of %2$d)", numberSuccessful, configs.count)
|
||||||
|
errorPresenterType.showErrorAlert(title: title, message: message, from: sourceVC, onPresented: completionHandler)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
assert(urls.count == 1)
|
||||||
|
let url = urls.first!
|
||||||
if url.pathExtension.lowercased() == "zip" {
|
if url.pathExtension.lowercased() == "zip" {
|
||||||
ZipImporter.importConfigFiles(from: url) { result in
|
ZipImporter.importConfigFiles(from: url) { result in
|
||||||
if let error = result.error {
|
if let error = result.error {
|
||||||
|
|
|
@ -28,7 +28,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
|
||||||
|
|
||||||
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey: Any] = [:]) -> Bool {
|
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey: Any] = [:]) -> Bool {
|
||||||
guard let tunnelsManager = mainVC?.tunnelsManager else { return true }
|
guard let tunnelsManager = mainVC?.tunnelsManager else { return true }
|
||||||
TunnelImporter.importFromFile(url: url, into: tunnelsManager, sourceVC: mainVC, errorPresenterType: ErrorPresenter.self) {
|
TunnelImporter.importFromFile(urls: [url], into: tunnelsManager, sourceVC: mainVC, errorPresenterType: ErrorPresenter.self) {
|
||||||
_ = FileManager.deleteFile(at: url)
|
_ = FileManager.deleteFile(at: url)
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
|
|
|
@ -164,9 +164,7 @@ class TunnelsListTableViewController: UIViewController {
|
||||||
extension TunnelsListTableViewController: UIDocumentPickerDelegate {
|
extension TunnelsListTableViewController: UIDocumentPickerDelegate {
|
||||||
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
|
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
|
||||||
guard let tunnelsManager = tunnelsManager else { return }
|
guard let tunnelsManager = tunnelsManager else { return }
|
||||||
urls.forEach { url in
|
TunnelImporter.importFromFile(urls: urls, into: tunnelsManager, sourceVC: self, errorPresenterType: ErrorPresenter.self)
|
||||||
TunnelImporter.importFromFile(url: url, into: tunnelsManager, sourceVC: self, errorPresenterType: ErrorPresenter.self)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -9,12 +9,12 @@ class ImportPanelPresenter {
|
||||||
let openPanel = NSOpenPanel()
|
let openPanel = NSOpenPanel()
|
||||||
openPanel.prompt = tr("macSheetButtonImport")
|
openPanel.prompt = tr("macSheetButtonImport")
|
||||||
openPanel.allowedFileTypes = ["conf", "zip"]
|
openPanel.allowedFileTypes = ["conf", "zip"]
|
||||||
|
openPanel.allowsMultipleSelection = true
|
||||||
openPanel.beginSheetModal(for: window) { [weak tunnelsManager] response in
|
openPanel.beginSheetModal(for: window) { [weak tunnelsManager] response in
|
||||||
guard let tunnelsManager = tunnelsManager else { return }
|
guard let tunnelsManager = tunnelsManager else { return }
|
||||||
guard response == .OK else { return }
|
guard response == .OK else { return }
|
||||||
guard let url = openPanel.url else { return }
|
|
||||||
AppStorePrivacyNotice.show(from: sourceVC, into: tunnelsManager) {
|
AppStorePrivacyNotice.show(from: sourceVC, into: tunnelsManager) {
|
||||||
TunnelImporter.importFromFile(url: url, into: tunnelsManager, sourceVC: sourceVC, errorPresenterType: ErrorPresenter.self)
|
TunnelImporter.importFromFile(urls: openPanel.urls, into: tunnelsManager, sourceVC: sourceVC, errorPresenterType: ErrorPresenter.self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue