session-ios/SessionMessagingKit/Jobs/AttachmentDownloadJob.swift

122 lines
5.1 KiB
Swift
Raw Normal View History

2020-11-20 04:04:56 +01:00
import Foundation
2020-11-09 00:58:47 +01:00
import SessionUtilitiesKit
2020-11-20 04:04:56 +01:00
import SignalCoreKit
2020-11-17 06:23:13 +01:00
public final class AttachmentDownloadJob : NSObject, Job, NSCoding { // NSObject/NSCoding conformance is needed for YapDatabase compatibility
2020-11-24 10:09:23 +01:00
public let attachmentID: String
public let tsIncomingMessageID: String
2020-11-08 03:12:38 +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-20 04:04:56 +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 "AttachmentDownloadJobCollection" }
public static let maxFailureCount: UInt = 20
2020-11-20 04:04:56 +01:00
// MARK: Initialization
2020-11-23 00:24:40 +01:00
public init(attachmentID: String, tsIncomingMessageID: String) {
2020-11-20 04:04:56 +01:00
self.attachmentID = attachmentID
2020-11-23 00:24:40 +01:00
self.tsIncomingMessageID = tsIncomingMessageID
2020-11-20 04:04:56 +01:00
}
// MARK: Coding
2020-11-20 04:04:56 +01:00
public init?(coder: NSCoder) {
2020-11-23 00:24:40 +01:00
guard let attachmentID = coder.decodeObject(forKey: "attachmentID") as! String?,
2020-11-24 06:18:58 +01:00
let tsIncomingMessageID = coder.decodeObject(forKey: "tsIncomingMessageID") as! String?,
let id = coder.decodeObject(forKey: "id") as! String? else { return nil }
2020-11-20 04:04:56 +01:00
self.attachmentID = attachmentID
2020-11-23 00:24:40 +01:00
self.tsIncomingMessageID = tsIncomingMessageID
2020-11-24 06:18:58 +01:00
self.id = id
self.failureCount = coder.decodeObject(forKey: "failureCount") as! UInt? ?? 0
2020-11-20 04:04:56 +01:00
}
2020-11-20 04:04:56 +01:00
public func encode(with coder: NSCoder) {
coder.encode(attachmentID, forKey: "attachmentID")
2020-11-23 00:24:40 +01:00
coder.encode(tsIncomingMessageID, forKey: "tsIncomingMessageID")
2020-11-24 06:18:58 +01:00
coder.encode(id, forKey: "id")
coder.encode(failureCount, forKey: "failureCount")
2020-11-20 04:04:56 +01:00
}
// MARK: Running
2020-11-20 04:04:56 +01:00
public func execute() {
guard let pointer = TSAttachmentPointer.fetch(uniqueId: attachmentID) else {
return handleFailure(error: Error.noAttachment)
}
2020-12-02 06:25:16 +01:00
let storage = SNMessagingKitConfiguration.shared.storage
2020-12-07 06:00:21 +01:00
storage.write(with: { transaction in
2020-11-23 00:24:40 +01:00
storage.setAttachmentState(to: .downloading, for: pointer, associatedWith: self.tsIncomingMessageID, using: transaction)
}, completion: { })
2020-11-20 04:04:56 +01:00
let temporaryFilePath = URL(fileURLWithPath: OWSTemporaryDirectoryAccessibleAfterFirstAuth() + UUID().uuidString)
2020-11-23 00:24:40 +01:00
let handleFailure: (Swift.Error) -> Void = { error in // Intentionally capture self
OWSFileSystem.deleteFile(temporaryFilePath.absoluteString)
2020-11-23 05:46:53 +01:00
if let error = error as? Error, case .noAttachment = error {
2020-12-07 06:00:21 +01:00
storage.write(with: { transaction in
2020-11-23 05:46:53 +01:00
storage.setAttachmentState(to: .failed, for: pointer, associatedWith: self.tsIncomingMessageID, using: transaction)
}, completion: { })
self.handlePermanentFailure(error: error)
} else if let error = error as? DotNetAPI.Error, case .parsingFailed = error {
// No need to retry if the response is invalid. Most likely this means we (incorrectly)
// got a "Cannot GET ..." error from the file server.
2020-12-07 06:00:21 +01:00
storage.write(with: { transaction in
storage.setAttachmentState(to: .failed, for: pointer, associatedWith: self.tsIncomingMessageID, using: transaction)
}, completion: { })
self.handlePermanentFailure(error: error)
2020-11-23 05:46:53 +01:00
} else {
self.handleFailure(error: error)
}
2020-11-23 00:24:40 +01:00
}
FileServerAPI.downloadAttachment(from: pointer.downloadURL).done(on: DispatchQueue.global(qos: .userInitiated)) { data in
2020-11-20 04:04:56 +01:00
do {
try data.write(to: temporaryFilePath, options: .atomic)
} catch {
2020-11-23 00:24:40 +01:00
return handleFailure(error)
2020-11-20 04:04:56 +01:00
}
let plaintext: Data
if let key = pointer.encryptionKey, let digest = pointer.digest {
do {
plaintext = try Cryptography.decryptAttachment(data, withKey: key, digest: digest, unpaddedSize: pointer.byteCount)
} catch {
2020-11-23 00:24:40 +01:00
return handleFailure(error)
2020-11-20 04:04:56 +01:00
}
} else {
plaintext = data // Open group attachments are unencrypted
}
let stream = TSAttachmentStream(pointer: pointer)
do {
try stream.write(plaintext)
} catch {
2020-11-23 00:24:40 +01:00
return handleFailure(error)
2020-11-20 04:04:56 +01:00
}
OWSFileSystem.deleteFile(temporaryFilePath.absoluteString)
2020-12-07 06:00:21 +01:00
storage.write(with: { transaction in
2020-11-23 00:24:40 +01:00
storage.persist(stream, associatedWith: self.tsIncomingMessageID, using: transaction)
2020-11-20 04:04:56 +01:00
}, completion: { })
}.catch(on: DispatchQueue.global()) { error in
2020-11-23 00:24:40 +01:00
handleFailure(error)
2020-11-20 04:04:56 +01:00
}
}
2020-11-08 03:12:38 +01:00
private func handleSuccess() {
delegate?.handleJobSucceeded(self)
}
2020-11-23 05:46:53 +01:00
private func handlePermanentFailure(error: Swift.Error) {
delegate?.handleJobFailedPermanently(self, with: error)
}
2020-11-20 04:04:56 +01:00
private func handleFailure(error: Swift.Error) {
2020-11-08 03:12:38 +01:00
delegate?.handleJobFailed(self, with: error)
}
}