|
|
|
|
@ -18,7 +18,7 @@ public final class FileServerAPI : DotNetAPI {
|
|
|
|
|
public static let fileSizeORMultiplier: Double = 6
|
|
|
|
|
|
|
|
|
|
@objc public static let server = "https://file.getsession.org"
|
|
|
|
|
@objc public static let fileStaticServer = "https://file-static.lokinet.org"
|
|
|
|
|
@objc public static let fileStorageBucketURL = "https://file-static.lokinet.org"
|
|
|
|
|
|
|
|
|
|
// MARK: Storage
|
|
|
|
|
override internal class var authTokenCollection: String { return "LokiStorageAuthTokenCollection" }
|
|
|
|
|
@ -53,34 +53,26 @@ public final class FileServerAPI : DotNetAPI {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@objc(downloadProfilePicture:)
|
|
|
|
|
public static func objc_downloadProfilePicture(_ downloadURL: String) -> AnyPromise {
|
|
|
|
|
return AnyPromise.from(downloadAttachment(downloadURL))
|
|
|
|
|
// MARK: Attachments
|
|
|
|
|
@objc(downloadAttachmentFrom:)
|
|
|
|
|
public static func objc_downloadAttachment(from url: String) -> AnyPromise {
|
|
|
|
|
return AnyPromise.from(downloadAttachment(from: url))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MARK: Attachment Download
|
|
|
|
|
@objc(downloadAttachment:)
|
|
|
|
|
public static func objc_downloadAttachment(_ downloadURL: String) -> AnyPromise {
|
|
|
|
|
return AnyPromise.from(downloadAttachment(downloadURL))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static func downloadAttachment(_ downloadURL: String) -> Promise<Data> {
|
|
|
|
|
public static func downloadAttachment(from url: String) -> Promise<Data> {
|
|
|
|
|
var error: NSError?
|
|
|
|
|
var url = downloadURL
|
|
|
|
|
if downloadURL.contains(fileStaticServer) {
|
|
|
|
|
url = downloadURL.replacingOccurrences(of: fileStaticServer, with: "\(server)/loki/v1")
|
|
|
|
|
}
|
|
|
|
|
let url = url.replacingOccurrences(of: fileStorageBucketURL, with: "\(server)/loki/v1")
|
|
|
|
|
let request = AFHTTPRequestSerializer().request(withMethod: "GET", urlString: url, parameters: nil, error: &error)
|
|
|
|
|
if let error = error {
|
|
|
|
|
print("[Loki] Couldn't download attachment due to error: \(error).")
|
|
|
|
|
return Promise(error: error)
|
|
|
|
|
}
|
|
|
|
|
return OnionRequestAPI.sendOnionRequest(request, to: server, using: fileServerPublicKey, isJSONRequired: false).map2 { json in
|
|
|
|
|
guard let body = json["body"] as? JSON, let dataArray = body["data"] as? [UInt8] else {
|
|
|
|
|
print("[Loki] Couldn't download attachment.")
|
|
|
|
|
return Data()
|
|
|
|
|
guard let body = json["body"] as? JSON, let data = body["data"] as? [UInt8] else {
|
|
|
|
|
print("[Loki] Couldn't parse attachment from: \(json).")
|
|
|
|
|
throw DotNetAPIError.parsingFailed
|
|
|
|
|
}
|
|
|
|
|
return Data(dataArray)
|
|
|
|
|
return Data(data)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|