From 758f37b886e3d770b4fda548c41bd24e7d521436 Mon Sep 17 00:00:00 2001 From: Niels Andriesse <andriesseniels@gmail.com> Date: Mon, 17 Feb 2020 10:29:43 +1100 Subject: [PATCH] Proxy profile picture uploads --- .../src/Loki/API/LokiDotNetAPI.swift | 6 +++--- .../src/Loki/API/LokiFileServerAPI.swift | 17 ++++------------- .../src/Loki/API/LokiFileServerProxy.swift | 2 +- 3 files changed, 8 insertions(+), 17 deletions(-) diff --git a/SignalServiceKit/src/Loki/API/LokiDotNetAPI.swift b/SignalServiceKit/src/Loki/API/LokiDotNetAPI.swift index e56c58471..4cb21c49e 100644 --- a/SignalServiceKit/src/Loki/API/LokiDotNetAPI.swift +++ b/SignalServiceKit/src/Loki/API/LokiDotNetAPI.swift @@ -100,10 +100,10 @@ public class LokiDotNetAPI : NSObject { return seal.reject(error) } // Send the request - func parseResponse(_ response: Any) { + func parseResponse(_ responseObject: Any) { // Parse the server ID & download URL - guard let json = response 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: \(response).") + 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(LokiDotNetAPIError.parsingFailed) } // Update the attachment diff --git a/SignalServiceKit/src/Loki/API/LokiFileServerAPI.swift b/SignalServiceKit/src/Loki/API/LokiFileServerAPI.swift index cc70bb566..f9b60faca 100644 --- a/SignalServiceKit/src/Loki/API/LokiFileServerAPI.swift +++ b/SignalServiceKit/src/Loki/API/LokiFileServerAPI.swift @@ -150,24 +150,15 @@ public final class LokiFileServerAPI : LokiDotNetAPI { print("[Loki] Couldn't upload profile picture due to error: \(error).") throw error } - let task = AFURLSessionManager(sessionConfiguration: .default).uploadTask(withStreamedRequest: request as URLRequest, progress: nil, completionHandler: { response, responseObject, error in - if let error = error { - print("[Loki] Couldn't upload profile picture 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 profile picture.") - return seal.reject(LokiDotNetAPIError.generic) - } + let _ = LokiFileServerProxy(for: server).performLokiFileServerNSURLRequest(request as NSURLRequest).done { responseObject in guard let json = responseObject as? JSON, let data = json["data"] as? JSON, let profilePicture = data["avatar_image"] as? JSON, let downloadURL = profilePicture["url"] as? String else { print("[Loki] Couldn't parse profile picture from: \(responseObject).") return seal.reject(LokiDotNetAPIError.parsingFailed) } return seal.fulfill(downloadURL) - }) - task.resume() + }.catch { error in + seal.reject(error) + } }.catch { error in print("[Loki] Couldn't upload profile picture due to error: \(error).") seal.reject(error) diff --git a/SignalServiceKit/src/Loki/API/LokiFileServerProxy.swift b/SignalServiceKit/src/Loki/API/LokiFileServerProxy.swift index a4ce4c848..d3a007f3b 100644 --- a/SignalServiceKit/src/Loki/API/LokiFileServerProxy.swift +++ b/SignalServiceKit/src/Loki/API/LokiFileServerProxy.swift @@ -106,7 +106,7 @@ internal class LokiFileServerProxy : LokiHTTPClient { print("[Loki] Received an invalid response.") throw Error.proxyResponseParsingFailed } - let isSuccess = (200..<300).contains(statusCode) + let isSuccess = (200...299) ~= statusCode guard isSuccess else { throw HTTPError.networkError(code: statusCode, response: nil, underlyingError: Error.fileServerHTTPError(code: statusCode, message: nil)) } let uncheckedJSONAsData = try DiffieHellman.decrypt(cipherText, using: symmetricKey) if uncheckedJSONAsData.isEmpty { return () }