|
|
|
@ -74,37 +74,56 @@ public class LokiDotNetAPI : NSObject {
|
|
|
|
|
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)
|
|
|
|
|
let isLokiFileServer = server.contains("file.lokinet.org") || server.contains("file-dev.lokinet.org")
|
|
|
|
|
if isLokiFileServer {
|
|
|
|
|
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(())
|
|
|
|
|
}.catch { error in
|
|
|
|
|
seal.reject(error)
|
|
|
|
|
}
|
|
|
|
|
}, 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()
|
|
|
|
|
} else {
|
|
|
|
|
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(on: DispatchQueue.global()) { error in
|
|
|
|
|
print("[Loki] Couldn't upload attachment.")
|
|
|
|
|
seal.reject(error)
|
|
|
|
|