Move serialization URLs to ConnectionService
Keep ProfileKey a bare struct.
This commit is contained in:
parent
5434e1abf0
commit
2197c96bd9
|
@ -54,8 +54,7 @@ extension ConnectionService {
|
|||
}
|
||||
|
||||
private func targetConfigurationURL(for key: ProfileKey) -> URL {
|
||||
let contextURL = key.contextURL(in: self)
|
||||
return contextURL.appendingPathComponent(key.id).appendingPathExtension("ovpn")
|
||||
return contextURL(key).appendingPathComponent(key.id).appendingPathExtension("ovpn")
|
||||
}
|
||||
|
||||
func pendingConfigurationURLs() -> [URL] {
|
||||
|
|
|
@ -64,24 +64,6 @@ class ConnectionService: Codable {
|
|||
id = profile.id
|
||||
}
|
||||
|
||||
func contextURL(in service: ConnectionService) -> URL {
|
||||
switch context {
|
||||
case .provider:
|
||||
return service.providersURL
|
||||
|
||||
case .host:
|
||||
return service.hostsURL
|
||||
}
|
||||
}
|
||||
|
||||
func profileURL(in service: ConnectionService) -> URL {
|
||||
return ConnectionService.url(in: contextURL(in: service), forProfileId: id)
|
||||
}
|
||||
|
||||
func profileData(in service: ConnectionService) throws -> Data {
|
||||
return try Data(contentsOf: profileURL(in: service))
|
||||
}
|
||||
|
||||
// MARK: RawRepresentable
|
||||
|
||||
var rawValue: String {
|
||||
|
@ -246,7 +228,7 @@ class ConnectionService: Codable {
|
|||
try? fm.createDirectory(at: hostsURL, withIntermediateDirectories: false, attributes: nil)
|
||||
|
||||
for key in pendingRemoval {
|
||||
let url = key.profileURL(in: self)
|
||||
let url = profileURL(key)
|
||||
try? fm.removeItem(at: url)
|
||||
if let cfg = configurationURL(for: key) {
|
||||
try? fm.removeItem(at: cfg)
|
||||
|
@ -255,7 +237,7 @@ class ConnectionService: Codable {
|
|||
for entry in cache.values {
|
||||
if let profile = entry as? ProviderConnectionProfile {
|
||||
do {
|
||||
let url = ConnectionService.url(in: providersURL, forProfileId: entry.id)
|
||||
let url = profileURL(ProfileKey(.provider, entry.id))
|
||||
let data = try encoder.encode(profile)
|
||||
try data.write(to: url)
|
||||
log.debug("Saved provider '\(profile.id)'")
|
||||
|
@ -265,7 +247,7 @@ class ConnectionService: Codable {
|
|||
}
|
||||
} else if let profile = entry as? HostConnectionProfile {
|
||||
do {
|
||||
let url = ConnectionService.url(in: hostsURL, forProfileId: entry.id)
|
||||
let url = profileURL(ProfileKey(.host, entry.id))
|
||||
let data = try encoder.encode(profile)
|
||||
try data.write(to: url)
|
||||
log.debug("Saved host '\(profile.id)'")
|
||||
|
@ -285,7 +267,7 @@ class ConnectionService: Codable {
|
|||
if let _ = profile as? PlaceholderConnectionProfile {
|
||||
let decoder = JSONDecoder()
|
||||
do {
|
||||
let data = try key.profileData(in: self)
|
||||
let data = try profileData(key)
|
||||
switch context {
|
||||
case .provider:
|
||||
profile = try decoder.decode(ProviderConnectionProfile.self, from: data)
|
||||
|
@ -306,6 +288,24 @@ class ConnectionService: Codable {
|
|||
return cache.keys.filter { $0.context == context }.map { $0.id }
|
||||
}
|
||||
|
||||
func contextURL(_ key: ProfileKey) -> URL {
|
||||
switch key.context {
|
||||
case .provider:
|
||||
return providersURL
|
||||
|
||||
case .host:
|
||||
return hostsURL
|
||||
}
|
||||
}
|
||||
|
||||
func profileURL(_ key: ProfileKey) -> URL {
|
||||
return contextURL(key).appendingPathComponent(key.id).appendingPathExtension("json")
|
||||
}
|
||||
|
||||
func profileData(_ key: ProfileKey) throws -> Data {
|
||||
return try Data(contentsOf: profileURL(key))
|
||||
}
|
||||
|
||||
private static func profileId(fromURL url: URL) -> String? {
|
||||
guard url.pathExtension == "json" else {
|
||||
return nil
|
||||
|
@ -313,10 +313,6 @@ class ConnectionService: Codable {
|
|||
return url.deletingPathExtension().lastPathComponent
|
||||
}
|
||||
|
||||
private static func url(in directory: URL, forProfileId profileId: String) -> URL {
|
||||
return directory.appendingPathComponent(profileId).appendingPathExtension("json")
|
||||
}
|
||||
|
||||
// MARK: Profiles
|
||||
|
||||
func addProfile(_ profile: ConnectionProfile, credentials: Credentials?) -> Bool {
|
||||
|
|
Loading…
Reference in New Issue