diff --git a/SignalServiceKit/src/Loki/API/LokiStorageAPI.swift b/SignalServiceKit/src/Loki/API/LokiStorageAPI.swift index a2c3e258b..80304e9aa 100644 --- a/SignalServiceKit/src/Loki/API/LokiStorageAPI.swift +++ b/SignalServiceKit/src/Loki/API/LokiStorageAPI.swift @@ -27,7 +27,7 @@ public final class LokiStorageAPI : LokiDotNetAPI { public static func getDeviceLinks(associatedWith hexEncodedPublicKeys: Set) -> Promise> { let hexEncodedPublicKeysDescription = "[ \(hexEncodedPublicKeys.joined(separator: ", ")) ]" print("[Loki] Getting device links for: \(hexEncodedPublicKeysDescription).") - return getAuthToken(for: server).then(on: DispatchQueue.global()) { token -> Promise> in + return getAuthToken(for: server).then(on: DispatchQueue.main) { token -> Promise> in let queryParameters = "ids=\(hexEncodedPublicKeys.map { "@\($0)" }.joined(separator: ","))&include_user_annotations=1" let url = URL(string: "\(server)/users?\(queryParameters)")! let request = TSRequest(url: url) @@ -134,80 +134,6 @@ public final class LokiStorageAPI : LokiDotNetAPI { return AnyPromise.from(getDeviceLinks(associatedWith: hexEncodedPublicKey)) } - // MARK: Attachments (Public API) - public static func uploadAttachment(_ attachment: TSAttachmentStream, attachmentID: String) -> Promise { - return Promise() { seal in - getAuthToken(for: server).done { token in - // Encrypt the attachment - guard let unencryptedAttachmentData = try? attachment.readDataFromFile() else { - print("[Loki] Couldn't read attachment data from disk.") - return seal.reject(Error.generic) - } - var encryptionKey = NSData() - var digest = NSData() - guard let encryptedAttachmentData = Cryptography.encryptAttachmentData(unencryptedAttachmentData, outKey: &encryptionKey, outDigest: &digest) else { - print("[Loki] Couldn't encrypt attachment.") - return seal.reject(Error.encryptionFailed) - } - attachment.encryptionKey = encryptionKey as Data - attachment.digest = digest as Data - // Create the request - let url = "\(server)/files" - let parameters: JSON = [ "type" : attachmentType, "Content-Type" : "application/binary" ] - var error: NSError? - var request = AFHTTPRequestSerializer().multipartFormRequest(withMethod: "POST", urlString: url, parameters: parameters, constructingBodyWith: { formData in - formData.appendPart(withFileData: encryptedAttachmentData, name: "content", fileName: UUID().uuidString, mimeType: "application/binary") - }, error: &error) - request.addValue("Bearer \(token)", forHTTPHeaderField: "Authorization") - if let error = error { - print("[Loki] Couldn't upload attachment due to error: \(error).") - throw error - } - // Send the request - let task = AFURLSessionManager(sessionConfiguration: .default).uploadTask(withStreamedRequest: request as URLRequest, progress: { rawProgress in - // Broadcast progress updates - let progress = max(0.1, rawProgress.fractionCompleted) - let userInfo: [String:Any] = [ kAttachmentUploadProgressKey : progress, kAttachmentUploadAttachmentIDKey : attachmentID ] - DispatchQueue.main.async { - NotificationCenter.default.post(name: .attachmentUploadProgress, object: nil, userInfo: userInfo) - } - }, completionHandler: { response, responseObject, error in - if let error = error { - print("[Loki] Couldn't upload attachment due to error: \(error).") - return seal.reject(error) - } - let statusCode = (response as! HTTPURLResponse).statusCode - let isSuccessful = (200...299) ~= statusCode - guard isSuccessful else { - print("[Loki] Couldn't upload attachment.") - return seal.reject(Error.generic) - } - // Parse the server ID & download URL - guard let json = responseObject as? JSON, let data = json["data"] as? JSON, let serverID = data["id"] as? UInt64, let downloadURL = data["url"] as? String else { - print("[Loki] Couldn't parse attachment from: \(responseObject).") - return seal.reject(Error.parsingFailed) - } - // Update the attachment - attachment.serverId = serverID - attachment.isUploaded = true - attachment.downloadURL = downloadURL - attachment.save() - return seal.fulfill(()) - }) - task.resume() - }.catch { error in - print("[Loki] Couldn't upload attachment.") - seal.reject(error) - } - } - } - - // MARK: Attachments (Public Obj-C API) - @objc(uploadAttachment:withID:) - public static func objc_uploadAttachment(_ attachment: TSAttachmentStream, attachmentID: String) -> AnyPromise { - return AnyPromise.from(uploadAttachment(attachment, attachmentID: attachmentID)) - } - // MARK: Profile Pictures (Public API) public static func setProfilePicture(_ profilePicture: Data) -> Promise { return Promise() { seal in