clean attachment download job

pull/500/head
ryanzhao 4 years ago
parent f99fa58acc
commit 70e38e11d8

@ -60,10 +60,6 @@ public final class AttachmentDownloadJob : NSObject, Job, NSCoding { // NSObject
// MARK: Running // MARK: Running
public func execute() { public func execute() {
execute(completion: nil)
}
public func execute(completion: (() -> Void)?) {
if let id = id { if let id = id {
JobQueue.currentlyExecutingJobs.insert(id) JobQueue.currentlyExecutingJobs.insert(id)
} }
@ -71,11 +67,9 @@ public final class AttachmentDownloadJob : NSObject, Job, NSCoding { // NSObject
if TSAttachment.fetch(uniqueId: attachmentID) is TSAttachmentStream { if TSAttachment.fetch(uniqueId: attachmentID) is TSAttachmentStream {
// FIXME: It's not clear * how * this happens, but apparently we can get to this point // FIXME: It's not clear * how * this happens, but apparently we can get to this point
// from time to time with an already downloaded attachment. // from time to time with an already downloaded attachment.
completion?()
return handleSuccess() return handleSuccess()
} }
guard let pointer = TSAttachment.fetch(uniqueId: attachmentID) as? TSAttachmentPointer else { guard let pointer = TSAttachment.fetch(uniqueId: attachmentID) as? TSAttachmentPointer else {
completion?()
return handleFailure(error: Error.noAttachment) return handleFailure(error: Error.noAttachment)
} }
let storage = SNMessagingKitConfiguration.shared.storage let storage = SNMessagingKitConfiguration.shared.storage
@ -98,10 +92,8 @@ public final class AttachmentDownloadJob : NSObject, Job, NSCoding { // NSObject
storage.setAttachmentState(to: .failed, for: pointer, associatedWith: self.tsMessageID, using: transaction) storage.setAttachmentState(to: .failed, for: pointer, associatedWith: self.tsMessageID, using: transaction)
}, completion: { }) }, completion: { })
// This usually indicates a file that has expired on the server, so there's no need to retry. // This usually indicates a file that has expired on the server, so there's no need to retry.
completion?()
self.handlePermanentFailure(error: error) self.handlePermanentFailure(error: error)
} else { } else {
completion?()
self.handleFailure(error: error) self.handleFailure(error: error)
} }
} }
@ -111,7 +103,6 @@ public final class AttachmentDownloadJob : NSObject, Job, NSCoding { // NSObject
} }
OpenGroupAPIV2.download(file, from: v2OpenGroup.room, on: v2OpenGroup.server).done(on: DispatchQueue.global(qos: .userInitiated)) { data in OpenGroupAPIV2.download(file, from: v2OpenGroup.room, on: v2OpenGroup.server).done(on: DispatchQueue.global(qos: .userInitiated)) { data in
self.handleDownloadedAttachment(data: data, temporaryFilePath: temporaryFilePath, pointer: pointer, failureHandler: handleFailure) self.handleDownloadedAttachment(data: data, temporaryFilePath: temporaryFilePath, pointer: pointer, failureHandler: handleFailure)
completion?()
}.catch(on: DispatchQueue.global()) { error in }.catch(on: DispatchQueue.global()) { error in
handleFailure(error) handleFailure(error)
} }
@ -122,7 +113,6 @@ public final class AttachmentDownloadJob : NSObject, Job, NSCoding { // NSObject
let useOldServer = pointer.downloadURL.contains(FileServerAPIV2.oldServer) let useOldServer = pointer.downloadURL.contains(FileServerAPIV2.oldServer)
FileServerAPIV2.download(file, useOldServer: useOldServer).done(on: DispatchQueue.global(qos: .userInitiated)) { data in FileServerAPIV2.download(file, useOldServer: useOldServer).done(on: DispatchQueue.global(qos: .userInitiated)) { data in
self.handleDownloadedAttachment(data: data, temporaryFilePath: temporaryFilePath, pointer: pointer, failureHandler: handleFailure) self.handleDownloadedAttachment(data: data, temporaryFilePath: temporaryFilePath, pointer: pointer, failureHandler: handleFailure)
completion?()
}.catch(on: DispatchQueue.global()) { error in }.catch(on: DispatchQueue.global()) { error in
handleFailure(error) handleFailure(error)
} }
@ -172,4 +162,3 @@ public final class AttachmentDownloadJob : NSObject, Job, NSCoding { // NSObject
delegate?.handleJobFailed(self, with: error) delegate?.handleJobFailed(self, with: error)
} }
} }

Loading…
Cancel
Save