Merge branch 'migrate-to-app-group-documents'

This commit is contained in:
Davide De Rosa 2019-03-18 10:09:31 +01:00
commit 8adc125f9b
5 changed files with 59 additions and 4 deletions

View File

@ -97,7 +97,7 @@ class AppConstants {
private static let fileName = "Debug.log" private static let fileName = "Debug.log"
static var fileURL: URL { static var fileURL: URL {
return FileManager.default.userURL(for: .cachesDirectory, appending: fileName) return GroupConstants.App.cachesURL.appendingPathComponent(fileName)
} }
private static let console: ConsoleDestination = { private static let console: ConsoleDestination = {

View File

@ -53,6 +53,25 @@ class GroupConstants {
static let tunnelIdentifier = "com.algoritmico.macos.Passepartout.Tunnel" static let tunnelIdentifier = "com.algoritmico.macos.Passepartout.Tunnel"
#endif #endif
private static var containerURL: URL {
guard let url = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: appGroup) else {
fatalError("Unable to access App Group container")
}
return url
}
static let documentsURL: URL = {
let url = containerURL.appendingPathComponent("Documents", isDirectory: true)
try? FileManager.default.createDirectory(at: url, withIntermediateDirectories: true, attributes: nil)
return url
}()
static let cachesURL: URL = {
let url = containerURL.appendingPathComponent("Library/Caches", isDirectory: true)
try? FileManager.default.createDirectory(at: url, withIntermediateDirectories: true, attributes: nil)
return url
}()
} }
class VPN { class VPN {

View File

@ -58,7 +58,11 @@ class ConnectionService: Codable {
var directory: String? = nil var directory: String? = nil
var rootURL: URL { var rootURL: URL {
return FileManager.default.userURL(for: .documentDirectory, appending: directory) var url = GroupConstants.App.documentsURL
if let directory = directory {
url.appendPathComponent(directory)
}
return url
} }
private var providersURL: URL { private var providersURL: URL {

View File

@ -36,7 +36,7 @@ class TransientStore {
static let shared = TransientStore() static let shared = TransientStore()
private static var serviceURL: URL { private static var serviceURL: URL {
return FileManager.default.userURL(for: .documentDirectory, appending: AppConstants.Store.serviceFilename) return GroupConstants.App.documentsURL.appendingPathComponent(AppConstants.Store.serviceFilename)
} }
let service: ConnectionService let service: ConnectionService
@ -51,6 +51,7 @@ class TransientStore {
} }
private init() { private init() {
TransientStore.migrateDocumentsToAppGroup()
// this must be graceful // this must be graceful
ConnectionService.migrateJSON(from: TransientStore.serviceURL, to: TransientStore.serviceURL) ConnectionService.migrateJSON(from: TransientStore.serviceURL, to: TransientStore.serviceURL)
@ -85,4 +86,35 @@ class TransientStore {
service.saveProfiles() service.saveProfiles()
} }
} }
//
private static func migrateDocumentsToAppGroup() {
var hasMigrated = false
let oldDocumentsURL = FileManager.default.userURL(for: .documentDirectory, appending: nil)
let newDocumentsURL = GroupConstants.App.documentsURL
log.debug("App documentsURL: \(oldDocumentsURL)")
log.debug("Group documentsURL: \(newDocumentsURL)")
let fm = FileManager.default
do {
for c in try fm.contentsOfDirectory(atPath: oldDocumentsURL.path) {
guard c != "Inbox" else {
continue
}
let old = oldDocumentsURL.appendingPathComponent(c)
let new = newDocumentsURL.appendingPathComponent(c)
log.verbose("Move:")
log.verbose("\tFROM: \(old)")
log.verbose("\tTO: \(new)")
try fm.moveItem(at: old, to: new)
hasMigrated = true
}
} catch let e {
hasMigrated = false
log.error("Could not migrate documents to App Group: \(e)")
}
if hasMigrated {
log.debug("Documents migrated to App Group")
}
}
} }

View File

@ -75,7 +75,7 @@ class InfrastructureFactory {
} }
self.bundle = bundle self.bundle = bundle
cachePath = FileManager.default.userURL(for: .cachesDirectory, appending: nil) cachePath = GroupConstants.App.cachesURL
cache = [:] cache = [:]
lastUpdate = [:] lastUpdate = [:]
} }