Move host configurations to "Hosts" subdirectory
Without "host." prefix, now unnecessary.
This commit is contained in:
parent
8e1b67d151
commit
2aae3499de
|
@ -37,7 +37,9 @@ class AppConstants {
|
|||
|
||||
static let infrastructureCacheDirectory = "Infrastructures"
|
||||
|
||||
static let profileConfigurationsDirectory = "Configurations"
|
||||
static let providersDirectory = "Providers"
|
||||
|
||||
static let hostsDirectory = "Hosts"
|
||||
}
|
||||
|
||||
class VPN {
|
||||
|
|
|
@ -46,15 +46,19 @@ extension ConnectionService {
|
|||
}
|
||||
|
||||
// replace migration logic here
|
||||
// TODO: remove this code after 1.0 release
|
||||
let build = json["build"] as? Int ?? 0
|
||||
if build <= 1084 {
|
||||
try migrateToWrappedSessionConfiguration(&json)
|
||||
try migrateToBaseConfiguration(&json)
|
||||
try migrateToBuildNumber(&json)
|
||||
try migrateHostProfileConfigurations()
|
||||
}
|
||||
|
||||
return try JSONSerialization.data(withJSONObject: json, options: [])
|
||||
}
|
||||
|
||||
// MARK: Atomic migrations
|
||||
|
||||
static func migrateToWrappedSessionConfiguration(_ json: inout [String: Any]) throws {
|
||||
guard let profiles = json["profiles"] as? [[String: Any]] else {
|
||||
|
@ -96,6 +100,31 @@ extension ConnectionService {
|
|||
json["build"] = GroupConstants.App.buildNumber
|
||||
}
|
||||
|
||||
static func migrateHostProfileConfigurations() throws {
|
||||
let fm = FileManager.default
|
||||
let oldDirectory = fm.userURL(for: .documentDirectory, appending: "Configurations")
|
||||
guard fm.fileExists(atPath: oldDirectory.path) else {
|
||||
return
|
||||
}
|
||||
|
||||
let newDirectory = fm.userURL(for: .documentDirectory, appending: AppConstants.Store.hostsDirectory)
|
||||
try fm.moveItem(at: oldDirectory, to: newDirectory)
|
||||
let list = try fm.contentsOfDirectory(at: newDirectory, includingPropertiesForKeys: nil, options: [])
|
||||
let prefix = "host."
|
||||
for url in list {
|
||||
let filename = url.lastPathComponent
|
||||
guard filename.hasPrefix(prefix) else {
|
||||
continue
|
||||
}
|
||||
let postPrefixIndex = filename.index(filename.startIndex, offsetBy: prefix.count)
|
||||
let newFilename = String(filename[postPrefixIndex..<filename.endIndex])
|
||||
var newURL = url
|
||||
newURL.deleteLastPathComponent()
|
||||
newURL.appendPathComponent(newFilename)
|
||||
try fm.moveItem(at: url, to: newURL)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: Helpers
|
||||
|
||||
private static func migrateSessionConfiguration(in map: inout [String: Any]) {
|
||||
|
|
|
@ -25,28 +25,48 @@
|
|||
|
||||
import Foundation
|
||||
|
||||
protocol ProfileConfigurationSource {
|
||||
var id: String { get }
|
||||
|
||||
var profileDirectory: String { get }
|
||||
}
|
||||
|
||||
extension ProfileConfigurationSource {
|
||||
var profileConfigurationPath: String {
|
||||
return "\(profileDirectory)/\(id).ovpn"
|
||||
}
|
||||
}
|
||||
|
||||
extension ProviderConnectionProfile: ProfileConfigurationSource {
|
||||
var profileDirectory: String {
|
||||
return AppConstants.Store.providersDirectory
|
||||
}
|
||||
}
|
||||
|
||||
extension HostConnectionProfile: ProfileConfigurationSource {
|
||||
var profileDirectory: String {
|
||||
return AppConstants.Store.hostsDirectory
|
||||
}
|
||||
}
|
||||
|
||||
class ProfileConfigurationFactory {
|
||||
static let shared = ProfileConfigurationFactory(withDirectory: AppConstants.Store.profileConfigurationsDirectory)
|
||||
|
||||
private let cachePath: URL
|
||||
static let shared = ProfileConfigurationFactory()
|
||||
|
||||
private let configurationsPath: URL
|
||||
|
||||
private init(withDirectory directory: String) {
|
||||
private init() {
|
||||
let fm = FileManager.default
|
||||
cachePath = fm.userURL(for: .cachesDirectory, appending: directory)
|
||||
configurationsPath = fm.userURL(for: .documentDirectory, appending: directory)
|
||||
try? fm.createDirectory(at: cachePath, withIntermediateDirectories: false, attributes: nil)
|
||||
configurationsPath = fm.userURL(for: .documentDirectory, appending: nil)
|
||||
try? fm.createDirectory(at: configurationsPath, withIntermediateDirectories: false, attributes: nil)
|
||||
}
|
||||
|
||||
func save(url: URL, for profile: ConnectionProfile) throws -> URL {
|
||||
func save(url: URL, for profile: ProfileConfigurationSource) throws -> URL {
|
||||
let savedUrl = targetConfigurationURL(for: profile)
|
||||
try FileManager.default.copyItem(at: url, to: savedUrl)
|
||||
return savedUrl
|
||||
}
|
||||
|
||||
func configurationURL(for profile: ConnectionProfile) -> URL? {
|
||||
func configurationURL(for profile: ProfileConfigurationSource) -> URL? {
|
||||
let url = targetConfigurationURL(for: profile)
|
||||
guard FileManager.default.fileExists(atPath: url.path) else {
|
||||
return nil
|
||||
|
@ -54,8 +74,7 @@ class ProfileConfigurationFactory {
|
|||
return url
|
||||
}
|
||||
|
||||
private func targetConfigurationURL(for profile: ConnectionProfile) -> URL {
|
||||
let filename = "\(profile.id).ovpn"
|
||||
return configurationsPath.appendingPathComponent(filename)
|
||||
private func targetConfigurationURL(for profile: ProfileConfigurationSource) -> URL {
|
||||
return configurationsPath.appendingPathComponent(profile.profileConfigurationPath)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue