Commit 2f67a6d3 by Sarkizz

调整热更新逻辑

parent 3a10ef30
...@@ -56,10 +56,10 @@ open class MRUpdateManager: NSObject { ...@@ -56,10 +56,10 @@ open class MRUpdateManager: NSObject {
public class HotUpdateFileInfo { public class HotUpdateFileInfo {
public var fileName: String public var fileName: String
public var downloadURL: String public var downloadURL: URL
public var fileHash: String? public var fileHash: String
init(fileName: String, downloadURL: String, fileHash: String? = nil) { public init(fileName: String, downloadURL: URL, fileHash: String) {
self.fileName = fileName self.fileName = fileName
self.downloadURL = downloadURL self.downloadURL = downloadURL
self.fileHash = fileHash self.fileHash = fileHash
...@@ -105,13 +105,13 @@ open class MRUpdateManager: NSObject { ...@@ -105,13 +105,13 @@ open class MRUpdateManager: NSObject {
open func resetFilesFolder() { open func resetFilesFolder() {
asyncIfNeeded { asyncIfNeeded {
if let bundleWWW = self.bundleWWWURL { if let bundleWWW = self.bundleWWWURL {
if self.settings.sandboxSrc.isURLExists(true) { if !self.settings.sandboxSrc.isURLExists(true) {
MRFiles.syncDelete(self.settings.sandboxSrc)
} else {
self.settings.sandboxSrc.syncCreate() self.settings.sandboxSrc.syncCreate()
} }
bundleWWW.syncCopy(to: self.settings.sandboxSrc) bundleWWW.syncCopy(to: self.settings.sandboxSrc)
self.sandboxUpdateURL.syncDelete()
self.sandboxBackupURL.syncDelete()
} }
} }
} }
...@@ -129,7 +129,7 @@ extension MRUpdateManager { ...@@ -129,7 +129,7 @@ extension MRUpdateManager {
} }
} }
public func checkUpdate(update: @escaping (_ data: Any, _ complate: (_ isUpdated: Bool, _ error: Error?) -> Void) -> Void) { public func checkUpdate(update: @escaping (_ data: Any, _ complate: @escaping (_ isUpdated: Bool, _ error: Error?) -> Void) -> Void) {
if _status == .updating { if _status == .updating {
return return
} }
...@@ -167,7 +167,7 @@ extension MRUpdateManager { ...@@ -167,7 +167,7 @@ extension MRUpdateManager {
break break
case .silence: case .silence:
let operations = { let operations = {
self.silenceUpdate { (error) in self.updateFromLocal { (error) in
self.asyncInMain { self.asyncInMain {
complate?(self, error) complate?(self, error)
} }
...@@ -181,6 +181,27 @@ extension MRUpdateManager { ...@@ -181,6 +181,27 @@ extension MRUpdateManager {
break break
} }
} }
public func updateFromLocal(_ complate:((_ error: Error?) -> Void)?) {
sandboxBackupURL.syncDelete()
if let error = sandboxWWWURL.syncMoveItems(to: sandboxBackupURL) {
complate?(error)
} else {
if let error = sandboxUpdateURL.syncMoveItems(to: sandboxWWWURL) {
//rollback
sandboxBackupURL.syncMoveItems(to: sandboxWWWURL)
complate?(error)
} else {
complate?(nil)
}
}
}
public func recoverFromBundleIfFileIncomplete(_ file: URL) {
if !checkFilesIntegrity(file) {
resetFilesFolder()
}
}
} }
extension MRUpdateManager { extension MRUpdateManager {
...@@ -251,29 +272,29 @@ extension MRUpdateManager { ...@@ -251,29 +272,29 @@ extension MRUpdateManager {
let p = Float(i)/Float(infos.count) let p = Float(i)/Float(infos.count)
let fileURL = updateURL.appendingPathComponent(info.fileName) let fileURL = updateURL.appendingPathComponent(info.fileName)
if let hash = info.fileHash {
//Step 2 //Step 2
if fileURL.isURLExists(), self.isSameHash(with: fileURL, hash: hash) { if fileURL.isURLExists(), self.isSameHash(with: fileURL, hash: info.fileHash) {
progressBlock(p) progressBlock(p)
continue continue
} }
//Step 3 //Step 3
let fileURLInWWW = wwwURL.appendingPathComponent(info.fileName) let fileURLInWWW = wwwURL.appendingPathComponent(info.fileName)
if fileURLInWWW.isURLExists(), self.isSameHash(with: fileURLInWWW, hash: hash) { if fileURLInWWW.isURLExists(),
self.isSameHash(with: fileURLInWWW, hash: info.fileHash) {
self.createFolderIfNeeded(with: info.fileName, in: updateURL) self.createFolderIfNeeded(with: info.fileName, in: updateURL)
let distURL = fileURL.deletingLastPathComponent() let distURL = fileURL.deletingLastPathComponent()
if fileURLInWWW.syncCopy(to: distURL) == nil, self.isSameHash(with: fileURL, hash: hash) { if fileURLInWWW.syncCopy(to: distURL) == nil,
self.isSameHash(with: fileURL, hash: info.fileHash) {
progressBlock(p) progressBlock(p)
continue continue
} }
} }
}
fileURL.syncDelete() fileURL.syncDelete()
if let url = URL(string: info.downloadURL), if let data = try? Data(contentsOf: info.downloadURL),
let data = try? Data(contentsOf: url), data.syncWrite(to: fileURL) == nil { data.syncWrite(to: fileURL) == nil {
progressBlock(p) progressBlock(p)
} else { } else {
isUpdateError = true isUpdateError = true
...@@ -286,27 +307,6 @@ extension MRUpdateManager { ...@@ -286,27 +307,6 @@ extension MRUpdateManager {
} }
extension MRUpdateManager { extension MRUpdateManager {
private func silenceUpdate(_ complate: ((_ error: Error?) -> Void)?) {
updateFromLocal(complate)
}
private func updateFromLocal(_ complate:((_ error: Error?) -> Void)?) {
sandboxBackupURL.syncDelete()
if let error = sandboxWWWURL.syncMoveItems(to: sandboxBackupURL) {
complate?(error)
} else {
if let error = sandboxUpdateURL.syncMoveItems(to: sandboxWWWURL) {
//rollback
sandboxBackupURL.syncMoveItems(to: sandboxWWWURL)
complate?(error)
} else {
complate?(nil)
}
}
}
}
extension MRUpdateManager {
private func asyncIfNeeded(_ operation: @escaping () -> Void) { private func asyncIfNeeded(_ operation: @escaping () -> Void) {
if let queue = fileQueue { if let queue = fileQueue {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment