From 37377c32df47acba683efe13dd85e6c103b3c8ec Mon Sep 17 00:00:00 2001 From: Niels Andriesse Date: Mon, 3 Feb 2020 20:55:42 +1100 Subject: [PATCH] Clean --- .../src/Loki/API/LokiDotNetAPI.swift | 39 ++++++++----------- .../src/Loki/API/LokiFileServerProxy.swift | 2 +- 2 files changed, 17 insertions(+), 24 deletions(-) diff --git a/SignalServiceKit/src/Loki/API/LokiDotNetAPI.swift b/SignalServiceKit/src/Loki/API/LokiDotNetAPI.swift index 2b3c50c33..6c2530c9d 100644 --- a/SignalServiceKit/src/Loki/API/LokiDotNetAPI.swift +++ b/SignalServiceKit/src/Loki/API/LokiDotNetAPI.swift @@ -74,20 +74,23 @@ public class LokiDotNetAPI : NSObject { throw error } // Send the request - let isProxyingRequired = (server == LokiStorageAPI.server) + func parseResponse(_ response: 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).") + return seal.reject(Error.parsingFailed) + } + // Update the attachment + attachment.serverId = serverID + attachment.isUploaded = true + attachment.downloadURL = downloadURL + attachment.save() + seal.fulfill(()) + } + let isProxyingRequired = (server == LokiStorageAPI.server) // Don't proxy open group requests for now if isProxyingRequired { let _ = LokiFileServerProxy(for: server).performLokiFileServerNSURLRequest(request as NSURLRequest).done { responseObject in - // 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() - seal.fulfill(()) + parseResponse(responseObject) }.catch { error in seal.reject(error) } @@ -110,17 +113,7 @@ public class LokiDotNetAPI : NSObject { 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(()) + parseResponse(responseObject) }) task.resume() } diff --git a/SignalServiceKit/src/Loki/API/LokiFileServerProxy.swift b/SignalServiceKit/src/Loki/API/LokiFileServerProxy.swift index dd70d537c..52a6de39a 100644 --- a/SignalServiceKit/src/Loki/API/LokiFileServerProxy.swift +++ b/SignalServiceKit/src/Loki/API/LokiFileServerProxy.swift @@ -37,7 +37,7 @@ internal class LokiFileServerProxy : LokiHTTPClient { // MARK: Proxying override internal func perform(_ request: TSRequest, withCompletionQueue queue: DispatchQueue = DispatchQueue.main) -> LokiAPI.RawResponsePromise { - let isLokiFileServer = server.contains("file.lokinet.org") || server.contains("file-dev.lokinet.org") + let isLokiFileServer = (server == LokiStorageAPI.server) guard isLokiFileServer else { return super.perform(request, withCompletionQueue: queue) } // Don't proxy open group requests for now return performLokiFileServerNSURLRequest(request, withCompletionQueue: queue) }