Upload attachments to public chat server as needed

pull/60/head
Niels Andriesse 6 years ago
parent b2ba6f75bf
commit 4adf209b13

@ -127,7 +127,7 @@
<key>NSContactsUsageDescription</key> <key>NSContactsUsageDescription</key>
<string>Signal uses your contacts to find users you know. We do not store your contacts on the server.</string> <string>Signal uses your contacts to find users you know. We do not store your contacts on the server.</string>
<key>NSFaceIDUsageDescription</key> <key>NSFaceIDUsageDescription</key>
<string>Loki Messenger&apos;s Screen Lock feature uses Face ID.</string> <string>Loki Messenger's Screen Lock feature uses Face ID.</string>
<key>NSMicrophoneUsageDescription</key> <key>NSMicrophoneUsageDescription</key>
<string>Loki Messenger needs access to your microphone to record videos.</string> <string>Loki Messenger needs access to your microphone to record videos.</string>
<key>NSPhotoLibraryAddUsageDescription</key> <key>NSPhotoLibraryAddUsageDescription</key>

@ -7,6 +7,9 @@ public class LokiDotNetAPI : NSObject {
internal static let userKeyPair = OWSIdentityManager.shared().identityKeyPair()! internal static let userKeyPair = OWSIdentityManager.shared().identityKeyPair()!
internal static let userHexEncodedPublicKey = userKeyPair.hexEncodedPublicKey internal static let userHexEncodedPublicKey = userKeyPair.hexEncodedPublicKey
// MARK: Settings
private static let attachmentType = "network.loki"
// MARK: Error // MARK: Error
public enum Error : Swift.Error { public enum Error : Swift.Error {
case generic, parsingFailed, encryptionFailed, decryptionFailed, signingFailed case generic, parsingFailed, encryptionFailed, decryptionFailed, signingFailed
@ -33,6 +36,74 @@ public class LokiDotNetAPI : NSObject {
// MARK: Lifecycle // MARK: Lifecycle
override private init() { } override private init() { }
// MARK: Attachments (Public API)
public static func uploadAttachment(_ attachment: TSAttachmentStream, with attachmentID: String, to server: String) -> Promise<Void> {
return Promise<Void>() { seal in
getAuthToken(for: server).done { token in
// Encrypt the attachment
guard let unencryptedAttachmentData = try? attachment.readDataFromFile() else {
print("[Loki] Couldn't read attachment data from disk.")
return seal.reject(Error.generic)
}
var encryptionKey = NSData()
var digest = NSData()
guard let encryptedAttachmentData = Cryptography.encryptAttachmentData(unencryptedAttachmentData, outKey: &encryptionKey, outDigest: &digest) else {
print("[Loki] Couldn't encrypt attachment.")
return seal.reject(Error.encryptionFailed)
}
attachment.encryptionKey = encryptionKey as Data
attachment.digest = digest as Data
// Create the request
let url = "\(server)/files"
let parameters: JSON = [ "type" : attachmentType, "Content-Type" : "application/binary" ]
var error: NSError?
var request = AFHTTPRequestSerializer().multipartFormRequest(withMethod: "POST", urlString: url, parameters: parameters, constructingBodyWith: { formData in
formData.appendPart(withFileData: encryptedAttachmentData, name: "content", fileName: UUID().uuidString, mimeType: "application/binary")
}, error: &error)
request.addValue("Bearer \(token)", forHTTPHeaderField: "Authorization")
if let error = error {
print("[Loki] Couldn't upload attachment due to error: \(error).")
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)
}
}, 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 { error in
print("[Loki] Couldn't upload attachment.")
seal.reject(error)
}
}
}
// MARK: Internal API // MARK: Internal API
internal static func getAuthToken(for server: String) -> Promise<String> { internal static func getAuthToken(for server: String) -> Promise<String> {
if let token = getAuthTokenFromDatabase(for: server) { if let token = getAuthTokenFromDatabase(for: server) {
@ -77,4 +148,10 @@ public class LokiDotNetAPI : NSObject {
let request = TSRequest(url: url, method: "POST", parameters: parameters) let request = TSRequest(url: url, method: "POST", parameters: parameters)
return TSNetworkManager.shared().makePromise(request: request).map { _ in token } return TSNetworkManager.shared().makePromise(request: request).map { _ in token }
} }
// MARK: Attachments (Public Obj-C API)
@objc(uploadAttachment:withID:toServer:)
public static func objc_uploadAttachment(_ attachment: TSAttachmentStream, with attachmentID: String, to server: String) -> AnyPromise {
return AnyPromise.from(uploadAttachment(attachment, with: attachmentID, to: server))
}
} }

@ -7,10 +7,9 @@ public final class LokiStorageAPI : LokiDotNetAPI {
// #if DEBUG // #if DEBUG
// private static let server = "http://file-dev.lokinet.org" // private static let server = "http://file-dev.lokinet.org"
// #else // #else
private static let server = "https://file.lokinet.org" @objc public static let server = "https://file.lokinet.org"
// #endif // #endif
private static let deviceLinkType = "network.loki.messenger.devicemapping" private static let deviceLinkType = "network.loki.messenger.devicemapping"
private static let attachmentType = "network.loki"
// MARK: Database // MARK: Database
override internal class var authTokenCollection: String { return "LokiStorageAuthTokenCollection" } override internal class var authTokenCollection: String { return "LokiStorageAuthTokenCollection" }
@ -124,78 +123,4 @@ public final class LokiStorageAPI : LokiDotNetAPI {
public static func objc_getDeviceLinks(associatedWith hexEncodedPublicKey: String) -> AnyPromise { public static func objc_getDeviceLinks(associatedWith hexEncodedPublicKey: String) -> AnyPromise {
return AnyPromise.from(getDeviceLinks(associatedWith: hexEncodedPublicKey)) return AnyPromise.from(getDeviceLinks(associatedWith: hexEncodedPublicKey))
} }
// MARK: Attachments (Public API)
public static func uploadAttachment(_ attachment: TSAttachmentStream, attachmentID: String) -> Promise<Void> {
return Promise<Void>() { seal in
getAuthToken(for: server).done { token in
// Encrypt the attachment
guard let unencryptedAttachmentData = try? attachment.readDataFromFile() else {
print("[Loki] Couldn't read attachment data from disk.")
return seal.reject(Error.generic)
}
var encryptionKey = NSData()
var digest = NSData()
guard let encryptedAttachmentData = Cryptography.encryptAttachmentData(unencryptedAttachmentData, outKey: &encryptionKey, outDigest: &digest) else {
print("[Loki] Couldn't encrypt attachment.")
return seal.reject(Error.encryptionFailed)
}
attachment.encryptionKey = encryptionKey as Data
attachment.digest = digest as Data
// Create the request
let url = "\(server)/files"
let parameters: JSON = [ "type" : attachmentType, "Content-Type" : "application/binary" ]
var error: NSError?
var request = AFHTTPRequestSerializer().multipartFormRequest(withMethod: "POST", urlString: url, parameters: parameters, constructingBodyWith: { formData in
formData.appendPart(withFileData: encryptedAttachmentData, name: "content", fileName: UUID().uuidString, mimeType: "application/binary")
}, error: &error)
request.addValue("Bearer \(token)", forHTTPHeaderField: "Authorization")
if let error = error {
print("[Loki] Couldn't upload attachment due to error: \(error).")
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)
}
}, 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 { error in
print("[Loki] Couldn't upload attachment.")
seal.reject(error)
}
}
}
// MARK: Attachments (Public Obj-C API)
@objc(uploadAttachment:withID:)
public static func objc_uploadAttachment(_ attachment: TSAttachmentStream, attachmentID: String) -> AnyPromise {
return AnyPromise.from(uploadAttachment(attachment, attachmentID: attachmentID))
}
} }

@ -389,8 +389,7 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException";
failure:failureHandler]; failure:failureHandler];
for (NSString *attachmentId in allAttachmentIds) { for (NSString *attachmentId in allAttachmentIds) {
OWSUploadOperation *uploadAttachmentOperation = OWSUploadOperation *uploadAttachmentOperation = [[OWSUploadOperation alloc] initWithAttachmentId:attachmentId threadID:message.thread.uniqueId dbConnection:self.dbConnection];
[[OWSUploadOperation alloc] initWithAttachmentId:attachmentId dbConnection:self.dbConnection];
// TODO: put attachment uploads on a (low priority) concurrent queue // TODO: put attachment uploads on a (low priority) concurrent queue
[sendMessageOperation addDependency:uploadAttachmentOperation]; [sendMessageOperation addDependency:uploadAttachmentOperation];
[sendingQueue addOperation:uploadAttachmentOperation]; [sendingQueue addOperation:uploadAttachmentOperation];

@ -19,6 +19,7 @@ extern NSString *const kAttachmentUploadAttachmentIDKey;
- (instancetype)init NS_UNAVAILABLE; - (instancetype)init NS_UNAVAILABLE;
- (instancetype)initWithAttachmentId:(NSString *)attachmentId - (instancetype)initWithAttachmentId:(NSString *)attachmentId
threadID:(NSString *)threadID
dbConnection:(YapDatabaseConnection *)dbConnection NS_DESIGNATED_INITIALIZER; dbConnection:(YapDatabaseConnection *)dbConnection NS_DESIGNATED_INITIALIZER;
@end @end

@ -30,6 +30,7 @@ static const CGFloat kAttachmentUploadProgressTheta = 0.001f;
@interface OWSUploadOperation () @interface OWSUploadOperation ()
@property (readonly, nonatomic) NSString *attachmentId; @property (readonly, nonatomic) NSString *attachmentId;
@property (readonly, nonatomic) NSString *threadID;
@property (readonly, nonatomic) YapDatabaseConnection *dbConnection; @property (readonly, nonatomic) YapDatabaseConnection *dbConnection;
@end @end
@ -39,6 +40,7 @@ static const CGFloat kAttachmentUploadProgressTheta = 0.001f;
@implementation OWSUploadOperation @implementation OWSUploadOperation
- (instancetype)initWithAttachmentId:(NSString *)attachmentId - (instancetype)initWithAttachmentId:(NSString *)attachmentId
threadID:(NSString *)threadID
dbConnection:(YapDatabaseConnection *)dbConnection dbConnection:(YapDatabaseConnection *)dbConnection
{ {
self = [super init]; self = [super init];
@ -49,6 +51,7 @@ static const CGFloat kAttachmentUploadProgressTheta = 0.001f;
self.remainingRetries = 4; self.remainingRetries = 4;
_attachmentId = attachmentId; _attachmentId = attachmentId;
_threadID = threadID;
_dbConnection = dbConnection; _dbConnection = dbConnection;
return self; return self;
@ -82,8 +85,14 @@ static const CGFloat kAttachmentUploadProgressTheta = 0.001f;
} }
[self fireNotificationWithProgress:0]; [self fireNotificationWithProgress:0];
[[LKStorageAPI uploadAttachment:attachmentStream withID:self.attachmentId] __block LKPublicChat *publicChat;
[self.dbConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) {
publicChat = [LKDatabaseUtilities getPublicChatForThreadID:self.threadID transaction:transaction];
}];
NSString *server = (publicChat != nil) ? publicChat.server : LKStorageAPI.server;
[[LKStorageAPI uploadAttachment:attachmentStream withID:self.attachmentId toServer:server]
.thenOn(dispatch_get_main_queue(), ^() { .thenOn(dispatch_get_main_queue(), ^() {
[self reportSuccess]; [self reportSuccess];
}) })

Loading…
Cancel
Save