Remove .ovpn of deleted profile

This commit is contained in:
Davide De Rosa 2018-10-27 12:39:22 +02:00
parent 8961bfe03e
commit dcecc84bda
2 changed files with 18 additions and 7 deletions

View File

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

View File

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