session-ios/SessionMessagingKit/Jobs/AttachmentUploadJob.swift

109 lines
4.5 KiB
Swift
Raw Normal View History

2020-11-09 00:58:47 +01:00
import SessionUtilitiesKit
2020-11-17 06:23:13 +01:00
public final class AttachmentUploadJob : NSObject, Job, NSCoding { // NSObject/NSCoding conformance is needed for YapDatabase compatibility
2020-11-23 23:49:31 +01:00
public let attachmentID: String
2020-11-24 04:10:32 +01:00
public let threadID: String
2020-11-26 23:07:24 +01:00
public let message: Message
public let messageSendJobID: String
2020-11-24 10:09:23 +01:00
public var delegate: JobDelegate?
2020-11-17 06:23:13 +01:00
public var id: String?
2020-11-08 03:12:38 +01:00
public var failureCount: UInt = 0
2020-11-23 05:37:25 +01:00
public enum Error : LocalizedError {
case noAttachment
public var errorDescription: String? {
switch self {
case .noAttachment: return "No such attachment."
}
}
}
// MARK: Settings
public class var collection: String { return "AttachmentUploadJobCollection" }
2020-11-08 03:12:38 +01:00
public static let maxFailureCount: UInt = 20
2020-11-23 05:08:01 +01:00
// MARK: Initialization
2020-11-26 23:07:24 +01:00
public init(attachmentID: String, threadID: String, message: Message, messageSendJobID: String) {
2020-11-23 05:37:25 +01:00
self.attachmentID = attachmentID
self.threadID = threadID
2020-11-26 23:07:24 +01:00
self.message = message
self.messageSendJobID = messageSendJobID
2020-11-23 05:37:25 +01:00
}
2020-11-23 05:08:01 +01:00
// MARK: Coding
2020-11-23 05:37:25 +01:00
public init?(coder: NSCoder) {
guard let attachmentID = coder.decodeObject(forKey: "attachmentID") as! String?,
2020-11-24 06:18:58 +01:00
let threadID = coder.decodeObject(forKey: "threadID") as! String?,
2020-11-26 23:07:24 +01:00
let message = coder.decodeObject(forKey: "message") as! Message?,
let messageSendJobID = coder.decodeObject(forKey: "messageSendJobID") as! String?,
2020-11-24 06:18:58 +01:00
let id = coder.decodeObject(forKey: "id") as! String? else { return nil }
2020-11-23 05:37:25 +01:00
self.attachmentID = attachmentID
self.threadID = threadID
2020-11-26 23:07:24 +01:00
self.message = message
self.messageSendJobID = messageSendJobID
2020-11-24 06:18:58 +01:00
self.id = id
self.failureCount = coder.decodeObject(forKey: "failureCount") as! UInt? ?? 0
2020-11-23 05:37:25 +01:00
}
2020-11-23 05:37:25 +01:00
public func encode(with coder: NSCoder) {
coder.encode(attachmentID, forKey: "attachmentID")
coder.encode(threadID, forKey: "threadID")
2020-11-26 23:07:24 +01:00
coder.encode(message, forKey: "message")
coder.encode(messageSendJobID, forKey: "messageSendJobID")
2020-11-24 06:18:58 +01:00
coder.encode(id, forKey: "id")
coder.encode(failureCount, forKey: "failureCount")
2020-11-23 05:37:25 +01:00
}
// MARK: Running
2020-11-23 05:37:25 +01:00
public func execute() {
guard let stream = TSAttachmentStream.fetch(uniqueId: attachmentID) else {
return handleFailure(error: Error.noAttachment)
}
guard !stream.isUploaded else { return handleSuccess() } // Should never occur
let openGroup = Configuration.shared.storage.getOpenGroup(for: threadID)
let server = openGroup?.server ?? FileServerAPI.server
2020-11-23 05:46:53 +01:00
// FIXME: A lot of what's currently happening in FileServerAPI should really be happening here
2020-11-23 05:37:25 +01:00
FileServerAPI.uploadAttachment(stream, with: attachmentID, to: server).done(on: DispatchQueue.global(qos: .userInitiated)) { // Intentionally capture self
self.handleSuccess()
}.catch(on: DispatchQueue.global(qos: .userInitiated)) { error in
2020-11-23 05:46:53 +01:00
if let error = error as? Error, case .noAttachment = error {
self.handlePermanentFailure(error: error)
} else if let error = error as? DotNetAPI.Error, !error.isRetryable {
self.handlePermanentFailure(error: error)
} else {
self.handleFailure(error: error)
}
2020-11-23 05:37:25 +01:00
}
}
2020-11-08 03:12:38 +01:00
private func handleSuccess() {
delegate?.handleJobSucceeded(self)
2020-11-26 23:07:24 +01:00
Configuration.shared.storage.resumeMessageSendJobIfNeeded(messageSendJobID)
2020-11-08 03:12:38 +01:00
}
2020-11-23 05:46:53 +01:00
private func handlePermanentFailure(error: Swift.Error) {
delegate?.handleJobFailedPermanently(self, with: error)
2020-11-26 23:07:24 +01:00
failAssociatedMessageSendJob(with: error)
2020-11-23 05:46:53 +01:00
}
2020-11-23 05:46:53 +01:00
private func handleFailure(error: Swift.Error) {
2020-11-08 03:12:38 +01:00
delegate?.handleJobFailed(self, with: error)
2020-11-26 23:07:24 +01:00
if failureCount + 1 == AttachmentUploadJob.maxFailureCount {
failAssociatedMessageSendJob(with: error)
}
}
private func failAssociatedMessageSendJob(with error: Swift.Error) {
let storage = Configuration.shared.storage
let messageSendJob = storage.getMessageSendJob(for: messageSendJobID)
storage.withAsync({ transaction in // Intentionally capture self
MessageSender.handleFailedMessageSend(self.message, with: error, using: transaction)
if let messageSendJob = messageSendJob {
storage.markJobAsFailed(messageSendJob, using: transaction)
}
}, completion: { })
2020-11-08 03:12:38 +01:00
}
}