From 3b252056de7b57765085a0e6f2d0b7a55e1e831d Mon Sep 17 00:00:00 2001 From: Niels Andriesse Date: Mon, 23 Nov 2020 15:37:25 +1100 Subject: [PATCH] Implement attachment uploading --- .../Jobs/AttachmentUploadJob.swift | 45 ++++++++++++++++--- SessionMessagingKit/Storage.swift | 4 ++ 2 files changed, 43 insertions(+), 6 deletions(-) diff --git a/SessionMessagingKit/Jobs/AttachmentUploadJob.swift b/SessionMessagingKit/Jobs/AttachmentUploadJob.swift index 838e4a63e..4e9fe1be5 100644 --- a/SessionMessagingKit/Jobs/AttachmentUploadJob.swift +++ b/SessionMessagingKit/Jobs/AttachmentUploadJob.swift @@ -1,26 +1,59 @@ import SessionUtilitiesKit -// TODO: Implementation - public final class AttachmentUploadJob : NSObject, Job, NSCoding { // NSObject/NSCoding conformance is needed for YapDatabase compatibility public var delegate: JobDelegate? + private let attachmentID: String + private let threadID: String public var id: String? public var failureCount: UInt = 0 + 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" } public static let maxFailureCount: UInt = 20 // MARK: Initialization - public override init() { } + public init(attachmentID: String, threadID: String) { + self.attachmentID = attachmentID + self.threadID = threadID + } // MARK: Coding - public init?(coder: NSCoder) { } + public init?(coder: NSCoder) { + guard let attachmentID = coder.decodeObject(forKey: "attachmentID") as! String?, + let threadID = coder.decodeObject(forKey: "threadID") as! String? else { return nil } + self.attachmentID = attachmentID + self.threadID = threadID + } - public func encode(with coder: NSCoder) { } + public func encode(with coder: NSCoder) { + coder.encode(attachmentID, forKey: "attachmentID") + coder.encode(threadID, forKey: "threadID") + } // MARK: Running - public func execute() { } + 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 + 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 + self.handleFailure(error: error) + } + } private func handleSuccess() { delegate?.handleJobSucceeded(self) diff --git a/SessionMessagingKit/Storage.swift b/SessionMessagingKit/Storage.swift index 92476e886..0d55606e5 100644 --- a/SessionMessagingKit/Storage.swift +++ b/SessionMessagingKit/Storage.swift @@ -37,6 +37,10 @@ public protocol SessionMessagingKitStorageProtocol { func setAuthToken(for server: String, to newValue: String, using transaction: Any) func removeAuthToken(for server: String, using transaction: Any) + // MARK: - Open Groups + + func getOpenGroup(for threadID: String) -> OpenGroup? + // MARK: - Open Group Public Keys func getOpenGroupPublicKey(for server: String) -> String?