summaryrefslogtreecommitdiffhomepage
path: root/ios/MullvadVPN/RelayCache/RelayCacheIO.swift
diff options
context:
space:
mode:
authorsajacl <sajaclvishkai@gmail.com>2022-10-21 13:27:05 +0200
committersajacl <sajaclvishkai@gmail.com>2022-10-21 15:16:32 +0200
commit8ad5803493f26eefaa4d2e72980919e3b7233cd7 (patch)
treef543cabaa48d6e942b79500d0129d9606484f713 /ios/MullvadVPN/RelayCache/RelayCacheIO.swift
parentd3ef8d298dbd3ceee5bc2dfbe0c25d610e664c5f (diff)
downloadmullvadvpn-8ad5803493f26eefaa4d2e72980919e3b7233cd7.tar.xz
mullvadvpn-8ad5803493f26eefaa4d2e72980919e3b7233cd7.zip
Move RelayCache.IO and CachedRelays into new RelayCache.framework
Moved RelayCache.IO and CachedRelays into new RelayCache.framework Moved prebuild script to build phase script. Renamed script, renamed/moved RelayCache.IO to RelayCache. Renamed RelayCache.Tracker to RelayCacheTracker. Set APPLICATION_EXTENSION_API_ONLY to true for RelayCache framework. Updated gitignore. Removed relays.json from git. Removed relays.json from RelayCache framework Removed RelayCache/FetchResult, moved/renamed code into RelayCacheTracker file. Renamed CachedRelaysFetchResult to RelaysFetchResult. Changed access level for RelaysFetchResult and NoCachedRelaysError.
Diffstat (limited to 'ios/MullvadVPN/RelayCache/RelayCacheIO.swift')
-rw-r--r--ios/MullvadVPN/RelayCache/RelayCacheIO.swift114
1 files changed, 0 insertions, 114 deletions
diff --git a/ios/MullvadVPN/RelayCache/RelayCacheIO.swift b/ios/MullvadVPN/RelayCache/RelayCacheIO.swift
deleted file mode 100644
index d2e9e91070..0000000000
--- a/ios/MullvadVPN/RelayCache/RelayCacheIO.swift
+++ /dev/null
@@ -1,114 +0,0 @@
-//
-// RelayCacheIO.swift
-// RelayCacheIO
-//
-// Created by pronebird on 27/07/2021.
-// Copyright © 2021 Mullvad VPN AB. All rights reserved.
-//
-
-import Foundation
-import MullvadREST
-
-extension RelayCache {
- enum IO {}
-}
-
-extension RelayCache.IO {
- /// The default cache file location bound by app group container.
- static func defaultCacheFileURL(
- forSecurityApplicationGroupIdentifier appGroupIdentifier: String
- ) -> URL? {
- let containerURL = FileManager.default.containerURL(
- forSecurityApplicationGroupIdentifier: appGroupIdentifier
- )
-
- return containerURL?.appendingPathComponent("relays.json")
- }
-
- /// The path to pre-bundled `relays.json` file.
- static var preBundledRelaysFileURL: URL? {
- return Bundle.main.url(forResource: "relays", withExtension: "json")
- }
-
- /// Safely read the cache file from disk using file coordinator.
- static func read(cacheFileURL: URL) throws -> RelayCache.CachedRelays {
- var result: Result<RelayCache.CachedRelays, Error>?
- let fileCoordinator = NSFileCoordinator(filePresenter: nil)
-
- let accessor = { (fileURLForReading: URL) in
- result = Result {
- let data = try Data(contentsOf: fileURLForReading)
- return try JSONDecoder().decode(RelayCache.CachedRelays.self, from: data)
- }
- }
-
- var error: NSError?
- fileCoordinator.coordinate(
- readingItemAt: cacheFileURL,
- options: [.withoutChanges],
- error: &error,
- byAccessor: accessor
- )
-
- if let error = error {
- result = .failure(error)
- }
-
- return try result!.get()
- }
-
- /// Safely read the cache file from disk using file coordinator and fallback to prebundled
- /// relays in case if the relay cache file is missing.
- static func readWithFallback(cacheFileURL: URL, preBundledRelaysFileURL: URL)
- throws -> RelayCache.CachedRelays
- {
- do {
- return try Self.read(cacheFileURL: cacheFileURL)
- } catch {
- if error is DecodingError || (error as? CocoaError)?.code == .fileReadNoSuchFile {
- return try Self.readPrebundledRelays(fileURL: preBundledRelaysFileURL)
- } else {
- throw error
- }
- }
- }
-
- /// Read pre-bundled relays file from disk.
- static func readPrebundledRelays(fileURL: URL) throws -> RelayCache.CachedRelays {
- let data = try Data(contentsOf: fileURL)
- let relays = try REST.Coding.makeJSONDecoder()
- .decode(REST.ServerRelaysResponse.self, from: data)
-
- return RelayCache.CachedRelays(
- relays: relays,
- updatedAt: Date(timeIntervalSince1970: 0)
- )
- }
-
- /// Safely write the cache file on disk using file coordinator.
- static func write(cacheFileURL: URL, record: RelayCache.CachedRelays) throws {
- var result: Result<Void, Error>?
- let fileCoordinator = NSFileCoordinator(filePresenter: nil)
-
- let accessor = { (fileURLForWriting: URL) in
- result = Result {
- let data = try JSONEncoder().encode(record)
- try data.write(to: fileURLForWriting)
- }
- }
-
- var error: NSError?
- fileCoordinator.coordinate(
- writingItemAt: cacheFileURL,
- options: [.forReplacing],
- error: &error,
- byAccessor: accessor
- )
-
- if let error = error {
- result = .failure(error)
- }
-
- try result?.get()
- }
-}