[op-download]: Fixed a bug where downloaded temp file was deleted before it was used

This commit is contained in:
Magesh K
2024-12-13 20:13:07 +05:30
parent 9597c7deb6
commit 90a1f4dd83

View File

@@ -155,6 +155,10 @@ private extension DownloadAppOperation
} }
} }
func printWithTid(_ msg: String){
print("DownloadAppOperation: Thread: \(Thread.current.name ?? Thread.current.description) - " + msg)
}
func download(@Managed _ app: AppProtocol) func download(@Managed _ app: AppProtocol)
{ {
guard let sourceURL = self.sourceURL else { guard let sourceURL = self.sourceURL else {
@@ -223,11 +227,13 @@ private extension DownloadAppOperation
{ {
// Patreon app // Patreon app
fileURL = try await downloadPatreonApp(from: sourceURL) fileURL = try await downloadPatreonApp(from: sourceURL)
self.printWithTid("downloadPatreonApp: completed at \(fileURL.path)")
} }
else else
{ {
// Regular app // Regular app
fileURL = try await downloadFile(from: sourceURL) fileURL = try await downloadFile(from: sourceURL)
self.printWithTid("downloadFile: completed at \(fileURL.path)")
} }
defer { defer {
@@ -238,7 +244,9 @@ private extension DownloadAppOperation
} }
var isDirectory: ObjCBool = false var isDirectory: ObjCBool = false
guard FileManager.default.fileExists(atPath: fileURL.path, isDirectory: &isDirectory) else { throw OperationError.appNotFound(name: self.appName) } guard FileManager.default.fileExists(atPath: fileURL.path, isDirectory: &isDirectory) else {
throw OperationError.appNotFound(name: self.appName)
}
try FileManager.default.createDirectory(at: self.temporaryDirectory, withIntermediateDirectories: true, attributes: nil) try FileManager.default.createDirectory(at: self.temporaryDirectory, withIntermediateDirectories: true, attributes: nil)
@@ -265,6 +273,17 @@ private extension DownloadAppOperation
} }
guard let application = ALTApplication(fileURL: appBundleURL) else { throw OperationError.invalidApp } guard let application = ALTApplication(fileURL: appBundleURL) else { throw OperationError.invalidApp }
// perform cleanup of the temp files
if(FileManager.default.fileExists(atPath: fileURL.path)){
self.printWithTid("Removing downloaded temp file at: \(fileURL.path)")
do{
try FileManager.default.removeItem(at: fileURL)
} catch{
self.printWithTid("Removing downloaded temp error: \(error)")
}
}
completionHandler(.success(application)) completionHandler(.success(application))
} }
catch catch
@@ -287,17 +306,20 @@ private extension DownloadAppOperation
} }
let (fileURL, _) = try Result((fileURL, response), error).get() let (fileURL, _) = try Result((fileURL, response), error).get()
try? FileManager.default.removeItem(at: fileURL)
continuation.resume(returning: fileURL) continuation.resume(returning: fileURL)
// self.printWithTid("downloadtask completed: fileURL: \(fileURL) URL: \(downloadURL)")
} }
catch catch
{ {
// self.printWithTid("downloadtask Error: \(error) URL:\(downloadURL)")
continuation.resume(throwing: error) continuation.resume(throwing: error)
} }
} }
self.progress.addChild(downloadTask.progress, withPendingUnitCount: 3) self.progress.addChild(downloadTask.progress, withPendingUnitCount: 3)
downloadTask.resume() downloadTask.resume()
self.printWithTid("download started: \(downloadURL)")
} }
} }