Refactoring: Consolidate file deletion into a separate function
This commit is contained in:
parent
5764fa4930
commit
205585c5d4
|
@ -16,4 +16,14 @@ extension FileManager {
|
||||||
}
|
}
|
||||||
return sharedFolderURL.appendingPathComponent("lastActivatedTunnelLog.txt")
|
return sharedFolderURL.appendingPathComponent("lastActivatedTunnelLog.txt")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static func deleteFile(at url: URL) -> Bool {
|
||||||
|
do {
|
||||||
|
try FileManager.default.removeItem(at: url)
|
||||||
|
} catch(let e) {
|
||||||
|
os_log("Failed to delete file '%{public}@': %{public}@", log: OSLog.default, type: .debug, url.absoluteString, e.localizedDescription)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,14 +27,8 @@ 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 {
|
||||||
defer {
|
|
||||||
do {
|
|
||||||
try FileManager.default.removeItem(at: url)
|
|
||||||
} catch {
|
|
||||||
os_log("Failed to remove item from Inbox: %{public}@", log: OSLog.default, type: .debug, url.absoluteString)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
mainVC?.tunnelsListVC?.importFromFile(url: url)
|
mainVC?.tunnelsListVC?.importFromFile(url: url)
|
||||||
|
_ = FileManager.deleteFile(at: url)
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -69,12 +69,9 @@ class SettingsTableViewController: UITableViewController {
|
||||||
guard let destinationDir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first else {
|
guard let destinationDir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first else {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
let destinationURL = destinationDir.appendingPathComponent("wireguard-export.zip")
|
let destinationURL = destinationDir.appendingPathComponent("wireguard-export.zip")
|
||||||
do {
|
_ = FileManager.deleteFile(at: destinationURL)
|
||||||
try FileManager.default.removeItem(at: destinationURL)
|
|
||||||
} catch {
|
|
||||||
os_log("Failed to delete file: %{public}@ : %{public}@", log: OSLog.default, type: .error, destinationURL.absoluteString, error.localizedDescription)
|
|
||||||
}
|
|
||||||
|
|
||||||
let count = tunnelsManager.numberOfTunnels()
|
let count = tunnelsManager.numberOfTunnels()
|
||||||
let tunnelConfigurations = (0 ..< count).compactMap { tunnelsManager.tunnel(at: $0).tunnelConfiguration() }
|
let tunnelConfigurations = (0 ..< count).compactMap { tunnelsManager.tunnel(at: $0).tunnelConfiguration() }
|
||||||
|
@ -102,10 +99,8 @@ class SettingsTableViewController: UITableViewController {
|
||||||
let destinationURL = destinationDir.appendingPathComponent("WireGuard_iOS_log_\(timeStampString).txt")
|
let destinationURL = destinationDir.appendingPathComponent("WireGuard_iOS_log_\(timeStampString).txt")
|
||||||
|
|
||||||
if (FileManager.default.fileExists(atPath: destinationURL.path)) {
|
if (FileManager.default.fileExists(atPath: destinationURL.path)) {
|
||||||
do {
|
let isDeleted = FileManager.deleteFile(at: destinationURL)
|
||||||
try FileManager.default.removeItem(at: destinationURL)
|
if (!isDeleted) {
|
||||||
} catch {
|
|
||||||
os_log("Failed to delete file: %{public}@ : %{public}@", log: OSLog.default, type: .error, destinationURL.absoluteString, error.localizedDescription)
|
|
||||||
showErrorAlert(title: "No log available", message: "The pre-existing log could not be cleared")
|
showErrorAlert(title: "No log available", message: "The pre-existing log could not be cleared")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -131,7 +126,7 @@ class SettingsTableViewController: UITableViewController {
|
||||||
activityVC.popoverPresentationController?.sourceRect = sourceView.bounds
|
activityVC.popoverPresentationController?.sourceRect = sourceView.bounds
|
||||||
activityVC.completionWithItemsHandler = { (_, _, _, _) in
|
activityVC.completionWithItemsHandler = { (_, _, _, _) in
|
||||||
// Remove the exported log file after the activity has completed
|
// Remove the exported log file after the activity has completed
|
||||||
try? FileManager.default.removeItem(at: destinationURL)
|
_ = FileManager.deleteFile(at: destinationURL)
|
||||||
}
|
}
|
||||||
self.present(activityVC, animated: true)
|
self.present(activityVC, animated: true)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue