Import of zip.
This commit is contained in:
parent
273f30d882
commit
dd899fa63d
|
@ -32,8 +32,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
|
||||||
os_log("Failed to remove item from Inbox: %{public}@", log: Log.general, type: .error, url.absoluteString)
|
os_log("Failed to remove item from Inbox: %{public}@", log: Log.general, type: .error, url.absoluteString)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
guard url.pathExtension == "conf" else { return false }
|
if url.pathExtension == "conf" {
|
||||||
|
|
||||||
do {
|
do {
|
||||||
try appCoordinator.importConfig(config: url)
|
try appCoordinator.importConfig(config: url)
|
||||||
} catch {
|
} catch {
|
||||||
|
@ -41,5 +40,16 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
|
} else if url.pathExtension == "zip" {
|
||||||
|
do {
|
||||||
|
try appCoordinator.importConfigs(configZip: url)
|
||||||
|
} catch {
|
||||||
|
os_log("Unable to import config: %{public}@", log: Log.general, type: .error, url.absoluteString)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -99,17 +99,36 @@ class AppCoordinator: RootViewCoordinator {
|
||||||
|
|
||||||
func importConfig(config: URL) throws {
|
func importConfig(config: URL) throws {
|
||||||
do {
|
do {
|
||||||
let configString = try String(contentsOf: config)
|
try importConfig(configString: try String(contentsOf: config), title: config.deletingPathExtension().lastPathComponent)
|
||||||
|
} catch {
|
||||||
|
throw AppCoordinatorError.configImportError(msg: "Failed")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func importConfig(configString: String, title: String) throws {
|
||||||
|
do {
|
||||||
let addContext = persistentContainer.newBackgroundContext()
|
let addContext = persistentContainer.newBackgroundContext()
|
||||||
let tunnel = try Tunnel.fromConfig(configString, context: addContext)
|
let tunnel = try Tunnel.fromConfig(configString, context: addContext)
|
||||||
let title = config.deletingPathExtension().lastPathComponent
|
|
||||||
tunnel.title = title
|
tunnel.title = title
|
||||||
addContext.saveContext()
|
addContext.saveContext()
|
||||||
self.saveTunnel(tunnel)
|
self.saveTunnel(tunnel)
|
||||||
} catch {
|
} catch {
|
||||||
throw AppCoordinatorError.configImportError(msg: "Failed")
|
throw AppCoordinatorError.configImportError(msg: "Failed")
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func importConfigs(configZip: URL) throws {
|
||||||
|
if let archive = Archive(url: configZip, accessMode: .read) {
|
||||||
|
for entry in archive {
|
||||||
|
var entryData = Data(capacity: 0)
|
||||||
|
_ = try archive.extract(entry) { (data) in
|
||||||
|
entryData.append(data)
|
||||||
|
}
|
||||||
|
if let config = String(data: entryData, encoding: .utf8) {
|
||||||
|
try importConfig(configString: config, title: entry.path)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// swiftlint:disable next function_body_length
|
// swiftlint:disable next function_body_length
|
||||||
|
|
|
@ -79,11 +79,6 @@
|
||||||
<key>UTExportedTypeDeclarations</key>
|
<key>UTExportedTypeDeclarations</key>
|
||||||
<array>
|
<array>
|
||||||
<dict>
|
<dict>
|
||||||
<key>UTTypeTagSpecification</key>
|
|
||||||
<dict>
|
|
||||||
<key>public.filename-extension</key>
|
|
||||||
<string>conf</string>
|
|
||||||
</dict>
|
|
||||||
<key>UTTypeConformsTo</key>
|
<key>UTTypeConformsTo</key>
|
||||||
<array>
|
<array>
|
||||||
<string>public.data</string>
|
<string>public.data</string>
|
||||||
|
@ -97,19 +92,19 @@
|
||||||
</array>
|
</array>
|
||||||
<key>UTTypeIdentifier</key>
|
<key>UTTypeIdentifier</key>
|
||||||
<string>com.wireguard.config.quick</string>
|
<string>com.wireguard.config.quick</string>
|
||||||
|
<key>UTTypeTagSpecification</key>
|
||||||
|
<dict>
|
||||||
|
<key>public.filename-extension</key>
|
||||||
|
<string>conf</string>
|
||||||
|
</dict>
|
||||||
</dict>
|
</dict>
|
||||||
</array>
|
</array>
|
||||||
<key>UTImportedTypeDeclarations</key>
|
<key>UTImportedTypeDeclarations</key>
|
||||||
<array>
|
<array>
|
||||||
<dict>
|
<dict>
|
||||||
<key>UTTypeTagSpecification</key>
|
|
||||||
<dict>
|
|
||||||
<key>public.filename-extension</key>
|
|
||||||
<string>conf</string>
|
|
||||||
</dict>
|
|
||||||
<key>UTTypeConformsTo</key>
|
<key>UTTypeConformsTo</key>
|
||||||
<array>
|
<array>
|
||||||
<string>public.data</string>
|
<string>com.pkware.zip-archive</string>
|
||||||
</array>
|
</array>
|
||||||
<key>UTTypeDescription</key>
|
<key>UTTypeDescription</key>
|
||||||
<string>WireGuard configuration</string>
|
<string>WireGuard configuration</string>
|
||||||
|
@ -120,6 +115,28 @@
|
||||||
</array>
|
</array>
|
||||||
<key>UTTypeIdentifier</key>
|
<key>UTTypeIdentifier</key>
|
||||||
<string>com.wireguard.config.quick</string>
|
<string>com.wireguard.config.quick</string>
|
||||||
|
<key>UTTypeTagSpecification</key>
|
||||||
|
<dict>
|
||||||
|
<key>public.filename-extension</key>
|
||||||
|
<string>conf</string>
|
||||||
|
</dict>
|
||||||
|
</dict>
|
||||||
|
<dict>
|
||||||
|
<key>UTTypeConformsTo</key>
|
||||||
|
<array>
|
||||||
|
<string>public.data</string>
|
||||||
|
</array>
|
||||||
|
<key>UTTypeDescription</key>
|
||||||
|
<string>WireGuard configurations</string>
|
||||||
|
<key>UTTypeIconFiles</key>
|
||||||
|
<array>
|
||||||
|
<string>icon_20pt.png</string>
|
||||||
|
<string>icon_60pt@3x.png</string>
|
||||||
|
</array>
|
||||||
|
<key>UTTypeIdentifier</key>
|
||||||
|
<string>com.wireguard.config.quick.confs</string>
|
||||||
|
<key>public.filename-extension</key>
|
||||||
|
<string>zip</string>
|
||||||
</dict>
|
</dict>
|
||||||
</array>
|
</array>
|
||||||
</dict>
|
</dict>
|
||||||
|
|
Loading…
Reference in New Issue