From 7c9d8bd90d45c91ad70c54f464169abb91a41913 Mon Sep 17 00:00:00 2001 From: Riley Testut Date: Fri, 15 May 2020 14:55:15 -0700 Subject: [PATCH] Adds option to not cache downloaded app during installation --- AltStore/Managing Apps/AppManager.swift | 21 ++++++++++++++----- .../Operations/DownloadAppOperation.swift | 4 ++-- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/AltStore/Managing Apps/AppManager.swift b/AltStore/Managing Apps/AppManager.swift index 547948cc..239de97c 100644 --- a/AltStore/Managing Apps/AppManager.swift +++ b/AltStore/Managing Apps/AppManager.swift @@ -486,7 +486,7 @@ private extension AppManager return group } - private func _install(_ app: AppProtocol, operation: AppOperation, group: RefreshGroup, additionalEntitlements: [ALTEntitlement: Any]? = nil, completionHandler: @escaping (Result) -> Void) -> Progress + private func _install(_ app: AppProtocol, operation: AppOperation, group: RefreshGroup, additionalEntitlements: [ALTEntitlement: Any]? = nil, cacheApp: Bool = true, completionHandler: @escaping (Result) -> Void) -> Progress { let progress = Progress.discreteProgress(totalUnitCount: 100) @@ -514,13 +514,24 @@ private extension AppManager downloadingApp = storeApp } + let downloadedAppURL = context.temporaryDirectory.appendingPathComponent("Cached.app") + /* Download */ - let downloadOperation = DownloadAppOperation(app: downloadingApp, context: context) + let downloadOperation = DownloadAppOperation(app: downloadingApp, destinationURL: downloadedAppURL, context: context) downloadOperation.resultHandler = { (result) in - switch result + do { - case .failure(let error): context.error = error - case .success(let app): context.app = app + let app = try result.get() + context.app = app + + if cacheApp + { + try FileManager.default.copyItem(at: app.fileURL, to: InstalledApp.fileURL(for: app), shouldReplace: true) + } + } + catch + { + context.error = error } } progress.addChild(downloadOperation.progress, withPendingUnitCount: 25) diff --git a/AltStore/Operations/DownloadAppOperation.swift b/AltStore/Operations/DownloadAppOperation.swift index ce13bfc3..1af194d9 100644 --- a/AltStore/Operations/DownloadAppOperation.swift +++ b/AltStore/Operations/DownloadAppOperation.swift @@ -23,14 +23,14 @@ class DownloadAppOperation: ResultOperation private let session = URLSession(configuration: .default) - init(app: AppProtocol, context: AppOperationContext) + init(app: AppProtocol, destinationURL: URL, context: AppOperationContext) { self.app = app self.context = context self.bundleIdentifier = app.bundleIdentifier self.sourceURL = app.url - self.destinationURL = InstalledApp.fileURL(for: app) + self.destinationURL = destinationURL super.init()