Fix/improve some poor filename parsing

This commit is contained in:
Davide De Rosa 2018-10-27 10:40:13 +02:00
parent 02c8e7b6ea
commit 804585f13d
5 changed files with 26 additions and 8 deletions

View File

@ -44,6 +44,6 @@ extension ConnectionService {
private func targetConfigurationURL(for profile: ConnectionProfile) -> URL { private func targetConfigurationURL(for profile: ConnectionProfile) -> URL {
let contextURL = ConnectionService.ProfileKey(profile).contextURL(in: self) let contextURL = ConnectionService.ProfileKey(profile).contextURL(in: self)
return contextURL.appendingPathComponent("\(profile.id).ovpn") return contextURL.appendingPathComponent(profile.id).appendingPathExtension("ovpn")
} }
} }

View File

@ -145,7 +145,7 @@ extension ConnectionService {
// provider["id"] = id // provider["id"] = id
// provider.removeValue(forKey: "name") // provider.removeValue(forKey: "name")
let url = providersParentURL.appendingPathComponent("\(id).json") let url = providersParentURL.appendingPathComponent(id).appendingPathExtension("json")
let data = try JSONSerialization.data(withJSONObject: provider, options: []) let data = try JSONSerialization.data(withJSONObject: provider, options: [])
try data.write(to: url) try data.write(to: url)
} else if var host = p["host"] as? [String: Any] { } else if var host = p["host"] as? [String: Any] {
@ -155,7 +155,7 @@ extension ConnectionService {
// host["id"] = id // host["id"] = id
// host.removeValue(forKey: "title") // host.removeValue(forKey: "title")
let url = hostsParentURL.appendingPathComponent("\(id).json") let url = hostsParentURL.appendingPathComponent(id).appendingPathExtension("json")
let data = try JSONSerialization.data(withJSONObject: host, options: []) let data = try JSONSerialization.data(withJSONObject: host, options: [])
try data.write(to: url) try data.write(to: url)
} }

View File

@ -304,15 +304,14 @@ class ConnectionService: Codable {
} }
private static func profileId(fromURL url: URL) -> String? { private static func profileId(fromURL url: URL) -> String? {
let filename = url.lastPathComponent guard url.pathExtension == "json" else {
guard let extRange = filename.range(of: ".json") else {
return nil return nil
} }
return String(filename[filename.startIndex..<extRange.lowerBound]) return url.deletingPathExtension().lastPathComponent
} }
private static func url(in directory: URL, forProfileId profileId: String) -> URL { private static func url(in directory: URL, forProfileId profileId: String) -> URL {
return directory.appendingPathComponent("\(profileId).json") return directory.appendingPathComponent(profileId).appendingPathExtension("json")
} }
// MARK: Profiles // MARK: Profiles

View File

@ -203,7 +203,7 @@ class InfrastructureFactory {
} }
private func cacheURL(for name: Infrastructure.Name) -> URL { private func cacheURL(for name: Infrastructure.Name) -> URL {
return cachePath.appendingPathComponent("\(name.webName).json") return cachePath.appendingPathComponent(name.webName).appendingPathExtension("json")
} }
private func cacheModificationDate(for name: Infrastructure.Name) -> Date? { private func cacheModificationDate(for name: Infrastructure.Name) -> Date? {

View File

@ -58,4 +58,23 @@ class ConnectionServiceTests: XCTestCase {
XCTAssert(activeProfile.parameters.sessionConfiguration.cipher == .aes256cbc) XCTAssert(activeProfile.parameters.sessionConfiguration.cipher == .aes256cbc)
XCTAssert(activeProfile.parameters.sessionConfiguration.ca.pem == "bogus+ca") XCTAssert(activeProfile.parameters.sessionConfiguration.ca.pem == "bogus+ca")
} }
func testPathExtension() {
XCTAssertTrue(privateTestPathExtension("file:///foo/bar/johndoe.json"))
XCTAssertFalse(privateTestPathExtension("file:///foo/bar/break.json.johndoe.json"))
}
private func privateTestPathExtension(_ string: String) -> Bool {
let url = URL(string: string)!
let filename = url.lastPathComponent
guard let extRange = filename.range(of: ".json") else {
return false
}
guard url.pathExtension == "json" else {
return false
}
let name1 = String(filename[filename.startIndex..<extRange.lowerBound])
let name2 = url.deletingPathExtension().lastPathComponent
return name1 == name2
}
} }