Proxy profile picture uploads

pull/105/head
Niels Andriesse 6 years ago
parent 0c18e82524
commit 758f37b886

@ -100,10 +100,10 @@ public class LokiDotNetAPI : NSObject {
return seal.reject(error) return seal.reject(error)
} }
// Send the request // Send the request
func parseResponse(_ response: Any) { func parseResponse(_ responseObject: Any) {
// Parse the server ID & download URL // 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 { 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: \(response).") print("[Loki] Couldn't parse attachment from: \(responseObject).")
return seal.reject(LokiDotNetAPIError.parsingFailed) return seal.reject(LokiDotNetAPIError.parsingFailed)
} }
// Update the attachment // Update the attachment

@ -150,24 +150,15 @@ public final class LokiFileServerAPI : LokiDotNetAPI {
print("[Loki] Couldn't upload profile picture due to error: \(error).") print("[Loki] Couldn't upload profile picture due to error: \(error).")
throw error throw error
} }
let task = AFURLSessionManager(sessionConfiguration: .default).uploadTask(withStreamedRequest: request as URLRequest, progress: nil, completionHandler: { response, responseObject, error in let _ = LokiFileServerProxy(for: server).performLokiFileServerNSURLRequest(request as NSURLRequest).done { responseObject 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)
}
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 { 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).") print("[Loki] Couldn't parse profile picture from: \(responseObject).")
return seal.reject(LokiDotNetAPIError.parsingFailed) return seal.reject(LokiDotNetAPIError.parsingFailed)
} }
return seal.fulfill(downloadURL) return seal.fulfill(downloadURL)
}) }.catch { error in
task.resume() seal.reject(error)
}
}.catch { error in }.catch { error in
print("[Loki] Couldn't upload profile picture due to error: \(error).") print("[Loki] Couldn't upload profile picture due to error: \(error).")
seal.reject(error) seal.reject(error)

@ -106,7 +106,7 @@ internal class LokiFileServerProxy : LokiHTTPClient {
print("[Loki] Received an invalid response.") print("[Loki] Received an invalid response.")
throw Error.proxyResponseParsingFailed 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)) } 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) let uncheckedJSONAsData = try DiffieHellman.decrypt(cipherText, using: symmetricKey)
if uncheckedJSONAsData.isEmpty { return () } if uncheckedJSONAsData.isEmpty { return () }

Loading…
Cancel
Save