You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
session-ios/SessionMessagingKit/File Server/FileServerAPI.swift

102 lines
4.1 KiB
Swift

// Copyright © 2022 Rangeproof Pty Ltd. All rights reserved.
import Foundation
import Combine
import SessionSnodeKit
import SessionUtilitiesKit
public enum FileServerAPI {
// MARK: - Settings
public static let oldServer = "http://88.99.175.227"
public static let oldServerPublicKey = "7cb31905b55cd5580c686911debf672577b3fb0bff81df4ce2d5c4cb3a7aaa69"
public static let server = "http://filev2.getsession.org"
public static let serverPublicKey = "da21e1d886c6fbaea313f75298bd64aab03a97ce985b46bb2dad9f2089c8ee59"
public static let maxFileSize = (10 * 1024 * 1024) // 10 MB
/// The file server has a file size limit of `maxFileSize`, which the Service Nodes try to enforce as well. However, the limit
/// applied by the Service Nodes is on the **HTTP request** and not the actual file size. Because the file server expects the
/// file data to be base 64 encoded, the size of the HTTP request for a given file will be at least `ceil(n / 3) * 4` bytes,
/// where n is the file size in bytes. This is the minimum size because there might also be other parameters in the request. On
/// average the multiplier appears to be about 1.5, so when checking whether the file will exceed the file size limit when uploading
/// a file we just divide the size of the file by this number. The alternative would be to actually check the size of the HTTP request
/// but that's only possible after proof of work has been calculated and the onion request encryption has happened, which takes
/// several seconds.
public static let fileSizeORMultiplier: Double = 2
// MARK: - File Storage
public static func upload(_ file: Data) -> AnyPublisher<FileUploadResponse, Error> {
let request = Request(
method: .post,
server: server,
endpoint: Endpoint.file,
headers: [
.contentDisposition: "attachment",
.contentType: "application/octet-stream"
],
body: Array(file)
)
return send(request, serverPublicKey: serverPublicKey)
.decoded(as: FileUploadResponse.self)
}
public static func download(_ fileId: String, useOldServer: Bool) -> AnyPublisher<Data, Error> {
let serverPublicKey: String = (useOldServer ? oldServerPublicKey : serverPublicKey)
let request = Request<NoBody, Endpoint>(
server: (useOldServer ? oldServer : server),
endpoint: .fileIndividual(fileId: fileId)
)
return send(request, serverPublicKey: serverPublicKey)
}
public static func getVersion(_ platform: String) -> AnyPublisher<String, Error> {
let request = Request<NoBody, Endpoint>(
server: server,
endpoint: .sessionVersion,
queryParameters: [
.platform: platform
]
)
return send(request, serverPublicKey: serverPublicKey)
.decoded(as: VersionResponse.self)
.map { response in response.version }
.eraseToAnyPublisher()
}
// MARK: - Convenience
private static func send<T: Encodable>(
_ request: Request<T, Endpoint>,
serverPublicKey: String
) -> AnyPublisher<Data, Error> {
let urlRequest: URLRequest
do {
urlRequest = try request.generateUrlRequest()
}
catch {
return Fail(error: error)
.eraseToAnyPublisher()
}
return OnionRequestAPI.sendOnionRequest(urlRequest, to: request.server, with: serverPublicKey)
.subscribe(on: DispatchQueue.global(qos: .userInitiated))
.flatMap { _, response -> AnyPublisher<Data, Error> in
guard let response: Data = response else {
return Fail(error: HTTPError.parsingFailed)
.eraseToAnyPublisher()
}
return Just(response)
.setFailureType(to: Error.self)
.eraseToAnyPublisher()
}
.eraseToAnyPublisher()
}
}