From f7613e09bfb2dacb4dd9ef38e6404909d3a1de89 Mon Sep 17 00:00:00 2001 From: Niels Andriesse Date: Mon, 26 Apr 2021 16:20:07 +1000 Subject: [PATCH] Convert remaining file server usages --- .../File Server/FileServerAPIV2.swift | 14 +++++++++-- .../Jobs/AttachmentUploadJob.swift | 16 ++++++------- .../MessageSender+Convenience.swift | 23 +++++++++++++------ SignalUtilitiesKit/To Do/OWSProfileManager.m | 21 ++++++++++++++--- 4 files changed, 54 insertions(+), 20 deletions(-) diff --git a/SessionMessagingKit/File Server/FileServerAPIV2.swift b/SessionMessagingKit/File Server/FileServerAPIV2.swift index 8fcb24663..a9d54f546 100644 --- a/SessionMessagingKit/File Server/FileServerAPIV2.swift +++ b/SessionMessagingKit/File Server/FileServerAPIV2.swift @@ -4,9 +4,9 @@ import SessionSnodeKit @objc(SNFileServerAPIV2) public final class FileServerAPIV2 : NSObject { - public static let server = "http://88.99.175.227" + @objc public static let server = "http://88.99.175.227" public static let serverPublicKey = "7cb31905b55cd5580c686911debf672577b3fb0bff81df4ce2d5c4cb3a7aaa69" - public static let useV2FileServer = true + @objc public static let useV2FileServer = false private override init() { } @@ -71,6 +71,11 @@ public final class FileServerAPIV2 : NSObject { } // MARK: File Storage + @objc(upload:) + public static func objc_upload(file: Data) -> AnyPromise { + return AnyPromise.from(upload(file)) + } + public static func upload(_ file: Data) -> Promise { let base64EncodedFile = file.base64EncodedString() let parameters = [ "file" : base64EncodedFile ] @@ -81,6 +86,11 @@ public final class FileServerAPIV2 : NSObject { } } + @objc(download:) + public static func objc_download(file: UInt64) -> AnyPromise { + return AnyPromise.from(download(file)) + } + public static func download(_ file: UInt64) -> Promise { let request = Request(verb: .get, endpoint: "files/\(file)") return send(request).map(on: DispatchQueue.global(qos: .userInitiated)) { json in diff --git a/SessionMessagingKit/Jobs/AttachmentUploadJob.swift b/SessionMessagingKit/Jobs/AttachmentUploadJob.swift index 918bb11f6..546ae8742 100644 --- a/SessionMessagingKit/Jobs/AttachmentUploadJob.swift +++ b/SessionMessagingKit/Jobs/AttachmentUploadJob.swift @@ -66,9 +66,9 @@ public final class AttachmentUploadJob : NSObject, Job, NSCoding { // NSObject/N guard !stream.isUploaded else { return handleSuccess() } // Should never occur let storage = SNMessagingKitConfiguration.shared.storage if let v2OpenGroup = storage.getV2OpenGroup(for: threadID) { - upload(stream, using: { data in return OpenGroupAPIV2.upload(data, to: v2OpenGroup.room, on: v2OpenGroup.server) }, encrypt: false) + AttachmentUploadJob.upload(stream, using: { data in return OpenGroupAPIV2.upload(data, to: v2OpenGroup.room, on: v2OpenGroup.server) }, encrypt: false, onSuccess: handleSuccess, onFailure: handleFailure) } else if FileServerAPIV2.useV2FileServer && storage.getOpenGroup(for: threadID) == nil { - upload(stream, using: FileServerAPIV2.upload, encrypt: true) + AttachmentUploadJob.upload(stream, using: FileServerAPIV2.upload, encrypt: true, onSuccess: handleSuccess, onFailure: handleFailure) } else { // Legacy let openGroup = storage.getOpenGroup(for: threadID) let server = openGroup?.server ?? FileServerAPI.server @@ -87,11 +87,11 @@ public final class AttachmentUploadJob : NSObject, Job, NSCoding { // NSObject/N } } - private func upload(_ stream: TSAttachmentStream, using upload: (Data) -> Promise, encrypt: Bool) { + public static func upload(_ stream: TSAttachmentStream, using upload: (Data) -> Promise, encrypt: Bool, onSuccess: (() -> Void)?, onFailure: ((Swift.Error) -> Void)?) { // Get the attachment guard var data = try? stream.readDataFromFile() else { SNLog("Couldn't read attachment from disk.") - return handleFailure(error: Error.noAttachment) + onFailure?(Error.noAttachment); return } // Encrypt the attachment if needed if encrypt { @@ -99,7 +99,7 @@ public final class AttachmentUploadJob : NSObject, Job, NSCoding { // NSObject/N var digest = NSData() guard let ciphertext = Cryptography.encryptAttachmentData(data, shouldPad: false, outKey: &encryptionKey, outDigest: &digest) else { SNLog("Couldn't encrypt attachment.") - return handleFailure(error: Error.encryptionFailed) + onFailure?(Error.encryptionFailed); return } stream.encryptionKey = encryptionKey as Data stream.digest = digest as Data @@ -108,7 +108,7 @@ public final class AttachmentUploadJob : NSObject, Job, NSCoding { // NSObject/N // Check the file size SNLog("File size: \(data.count) bytes.") if Double(data.count) > Double(FileServerAPI.maxFileSize) / FileServerAPI.fileSizeORMultiplier { - return handleFailure(error: FileServerAPI.Error.maxFileSizeExceeded) + onFailure?(FileServerAPI.Error.maxFileSizeExceeded); return } // Send the request stream.isUploaded = false @@ -119,9 +119,9 @@ public final class AttachmentUploadJob : NSObject, Job, NSCoding { // NSObject/N stream.isUploaded = true stream.downloadURL = downloadURL stream.save() - self.handleSuccess() + onSuccess?() }.catch { error in - self.handleFailure(error: error) + onFailure?(error) } } diff --git a/SignalUtilitiesKit/Messaging/Sending & Receiving/MessageSender+Convenience.swift b/SignalUtilitiesKit/Messaging/Sending & Receiving/MessageSender+Convenience.swift index fa0d6baa7..e6d140743 100644 --- a/SignalUtilitiesKit/Messaging/Sending & Receiving/MessageSender+Convenience.swift +++ b/SignalUtilitiesKit/Messaging/Sending & Receiving/MessageSender+Convenience.swift @@ -42,13 +42,22 @@ extension MessageSender { let attachments = attachmentIDs.compactMap { TSAttachment.fetch(uniqueId: $0, transaction: transaction) as? TSAttachmentStream } let attachmentsToUpload = attachments.filter { !$0.isUploaded } let attachmentUploadPromises: [Promise] = attachmentsToUpload.map { stream in - let openGroup = SNMessagingKitConfiguration.shared.storage.getOpenGroup(for: thread.uniqueId!) - let openGroupV2 = SNMessagingKitConfiguration.shared.storage.getV2OpenGroup(for: thread.uniqueId!) - let server = openGroupV2?.server ?? openGroup?.server ?? FileServerAPI.server - // FIXME: This is largely a duplication of the code in AttachmentUploadJob - let maxRetryCount: UInt = (openGroup != nil) ? 24 : 8 - return attempt(maxRetryCount: maxRetryCount, recoveringOn: DispatchQueue.global(qos: .userInitiated)) { - FileServerAPI.uploadAttachment(stream, with: stream.uniqueId!, to: server) + let storage = SNMessagingKitConfiguration.shared.storage + if let v2OpenGroup = storage.getV2OpenGroup(for: thread.uniqueId!) { + let (promise, seal) = Promise.pending() + AttachmentUploadJob.upload(stream, using: { data in return OpenGroupAPIV2.upload(data, to: v2OpenGroup.room, on: v2OpenGroup.server) }, encrypt: false, onSuccess: { seal.fulfill(()) }, onFailure: { seal.reject($0) }) + return promise + } else if FileServerAPIV2.useV2FileServer && storage.getOpenGroup(for: thread.uniqueId!) == nil { + let (promise, seal) = Promise.pending() + AttachmentUploadJob.upload(stream, using: FileServerAPIV2.upload, encrypt: true, onSuccess: { seal.fulfill(()) }, onFailure: { seal.reject($0) }) + return promise + } else { // Legacy + let openGroup = storage.getOpenGroup(for: thread.uniqueId!) + let server = openGroup?.server ?? FileServerAPI.server + let maxRetryCount: UInt = (openGroup != nil) ? 24 : 8 + return attempt(maxRetryCount: maxRetryCount, recoveringOn: DispatchQueue.global(qos: .userInitiated)) { + FileServerAPI.uploadAttachment(stream, with: stream.uniqueId!, to: server) + } } } return when(resolved: attachmentUploadPromises).then(on: DispatchQueue.global(qos: .userInitiated)) { results -> Promise in diff --git a/SignalUtilitiesKit/To Do/OWSProfileManager.m b/SignalUtilitiesKit/To Do/OWSProfileManager.m index 4e9c23e91..b1f4aaf01 100644 --- a/SignalUtilitiesKit/To Do/OWSProfileManager.m +++ b/SignalUtilitiesKit/To Do/OWSProfileManager.m @@ -362,8 +362,14 @@ typedef void (^ProfileManagerFailureBlock)(NSError *error); NSData *encryptedAvatarData = [self encryptProfileData:avatarData profileKey:newProfileKey]; OWSAssertDebug(encryptedAvatarData.length > 0); - [[SNFileServerAPI uploadProfilePicture:encryptedAvatarData] - .thenOn(dispatch_get_main_queue(), ^(NSString *downloadURL) { + AnyPromise *promise; + if (SNFileServerAPIV2.useV2FileServer) { + promise = [SNFileServerAPIV2 upload:encryptedAvatarData]; + } else { + promise = [SNFileServerAPI uploadProfilePicture:encryptedAvatarData]; + } + + [promise.thenOn(dispatch_get_main_queue(), ^(NSString *downloadURL) { [self.localUserProfile updateWithProfileKey:newProfileKey dbConnection:self.dbConnection completion:^{ successBlock(downloadURL); }]; @@ -801,7 +807,16 @@ typedef void (^ProfileManagerFailureBlock)(NSError *error); OWSLogVerbose(@"downloading profile avatar: %@", userProfile.uniqueId); NSString *profilePictureURL = userProfile.avatarUrlPath; - [[SNFileServerAPI downloadAttachmentFrom:profilePictureURL].then(^(NSData *data) { + + AnyPromise *promise; + if ([profilePictureURL containsString:SNFileServerAPIV2.server]) { + uint64_t *file = (uint64_t)[[profilePictureURL lastPathComponent] intValue]; + promise = [SNFileServerAPIV2 download:file]; + } else { + promise = [SNFileServerAPI downloadAttachmentFrom:profilePictureURL]; + } + + [promise.then(^(NSData *data) { @synchronized(self.currentAvatarDownloads) { [self.currentAvatarDownloads removeObject:userProfile.recipientId];