Merge branch 'delete-stale-configuration-files'

This commit is contained in:
Davide De Rosa 2018-10-27 13:35:28 +02:00
commit 9cb8cf92ca
6 changed files with 36 additions and 12 deletions

View File

@ -90,13 +90,16 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UISplitViewControllerDele
fatalError("No window.rootViewController?")
}
let fm = FileManager.default
guard let parsedFile = ParsedFile.from(url, withErrorAlertIn: root) else {
try? fm.removeItem(at: url)
return true
}
// already presented: update parsed configuration
if let nav = root.presentedViewController as? UINavigationController, let wizard = nav.topViewController as? WizardHostViewController {
wizard.parsedFile = parsedFile
wizard.removesConfigurationOnCancel = true
return true
}
@ -106,6 +109,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UISplitViewControllerDele
fatalError("Expected WizardHostViewController from storyboard")
}
wizard.parsedFile = parsedFile
wizard.removesConfigurationOnCancel = true
// best effort to delegate to main vc
let split = root as? UISplitViewController

View File

@ -33,10 +33,6 @@ class Macros {
static func actionSheet(_ title: String?, _ message: String?) -> UIAlertController {
return UIAlertController(title: title, message: message, preferredStyle: .actionSheet)
}
static var isDeviceNonPlus: Bool {
return (UI_USER_INTERFACE_IDIOM() == .phone) && (UIScreen.main.scale < 3.0)
}
}
extension UIAlertController {

View File

@ -41,6 +41,8 @@ class WizardHostViewController: UITableViewController, TableModelHost, Wizard {
useSuggestedTitle()
}
}
var removesConfigurationOnCancel = false
private var createdProfile: HostConnectionProfile?
@ -134,6 +136,9 @@ class WizardHostViewController: UITableViewController, TableModelHost, Wizard {
do {
let savedURL = try TransientStore.shared.service.save(configurationURL: url, for: profile)
log.debug("Associated .ovpn configuration file to profile '\(profile.id)': \(savedURL)")
// can now delete imported file
try? FileManager.default.removeItem(at: url)
} catch let e {
log.error("Could not associate .ovpn configuration file to profile: \(e)")
}
@ -145,6 +150,9 @@ class WizardHostViewController: UITableViewController, TableModelHost, Wizard {
}
@IBAction private func close() {
if removesConfigurationOnCancel, let url = parsedFile?.url {
try? FileManager.default.removeItem(at: url)
}
dismiss(animated: true, completion: nil)
}
}

View File

@ -29,25 +29,33 @@ import SwiftyBeaver
private let log = SwiftyBeaver.self
extension ConnectionService {
func save(configurationURL: URL, for profile: ConnectionProfile) throws -> URL {
let destinationURL = targetConfigurationURL(for: profile)
func save(configurationURL: URL, for key: ProfileKey) throws -> URL {
let destinationURL = targetConfigurationURL(for: key)
let fm = FileManager.default
try? fm.removeItem(at: destinationURL)
try fm.copyItem(at: configurationURL, to: destinationURL)
return destinationURL
}
func configurationURL(for profile: ConnectionProfile) -> URL? {
let url = targetConfigurationURL(for: profile)
func save(configurationURL: URL, for profile: ConnectionProfile) throws -> URL {
return try save(configurationURL: configurationURL, for: ProfileKey(profile))
}
func configurationURL(for key: ProfileKey) -> URL? {
let url = targetConfigurationURL(for: key)
guard FileManager.default.fileExists(atPath: url.path) else {
return nil
}
return url
}
func configurationURL(for profile: ConnectionProfile) -> URL? {
return configurationURL(for: ProfileKey(profile))
}
private func targetConfigurationURL(for profile: ConnectionProfile) -> URL {
let contextURL = ConnectionService.ProfileKey(profile).contextURL(in: self)
return contextURL.appendingPathComponent(profile.id).appendingPathExtension("ovpn")
private func targetConfigurationURL(for key: ProfileKey) -> URL {
let contextURL = key.contextURL(in: self)
return contextURL.appendingPathComponent(key.id).appendingPathExtension("ovpn")
}
func pendingConfigurationURLs() -> [URL] {

View File

@ -45,7 +45,7 @@ extension ConnectionService {
throw ApplicationError.migration
}
// replace migration logic here
// put migration logic here
// TODO: remove this code after 1.0 release
let build = json["build"] as? Int ?? 0
if build <= 1084 {
@ -55,6 +55,11 @@ extension ConnectionService {
try migrateHostProfileConfigurations()
try migrateSplitProfileSerialization(&json)
}
if build <= 1107 {
let fm = FileManager.default
let inbox = fm.userURL(for: .documentDirectory, appending: "Inbox")
try? fm.removeItem(at: inbox)
}
return try JSONSerialization.data(withJSONObject: json, options: [])
}

View File

@ -248,6 +248,9 @@ class ConnectionService: Codable {
for key in pendingRemoval {
let url = key.profileURL(in: self)
try? fm.removeItem(at: url)
if let cfg = configurationURL(for: key) {
try? fm.removeItem(at: cfg)
}
}
for entry in cache.values {
if let profile = entry as? ProviderConnectionProfile {