Minor refactoring

pull/297/head
nielsandriesse 5 years ago
parent 7eb2f0f83b
commit 0ae8da838a

@ -169,8 +169,8 @@ public class ConversationMediaView: UIView {
return
}
let view: UIView
backgroundColor = (Theme.isDarkThemeEnabled ? .ows_gray90 : .ows_gray05)
let view: UIView
if isOnionRouted { // Loki: Due to the way onion routing works we can't get upload progress for those attachments
let activityIndicatorView = UIActivityIndicatorView(style: .white)
activityIndicatorView.isHidden = false

@ -377,7 +377,7 @@ class MediaGallery: NSObject, MediaGalleryDataSource, MediaTileViewControllerDel
}
guard let initialDetailItem = galleryItem else {
owsFailDebug("unexpectedly failed to build initialDetailItem.")
// owsFailDebug("unexpectedly failed to build initialDetailItem.")
return
}

@ -1158,8 +1158,7 @@ typedef void (^ProfileManagerFailureBlock)(NSError *error);
OWSLogVerbose(@"downloading profile avatar: %@", userProfile.uniqueId);
NSString *profilePictureURL = userProfile.avatarUrlPath;
AnyPromise *promise = [LKFileServerAPI downloadProfilePicture:profilePictureURL];
[promise.then(^(NSData *data) {
[[LKFileServerAPI downloadAttachmentFrom:profilePictureURL].then(^(NSData *data) {
@synchronized(self.currentAvatarDownloads)
{
[self.currentAvatarDownloads removeObject:userProfile.recipientId];

@ -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)
}
}

@ -391,19 +391,20 @@ public final class PublicChatAPI : DotNetAPI {
var error: NSError?
let url = "\(server)/loki/v1\(profilePictureURL)"
let request = AFHTTPRequestSerializer().request(withMethod: "GET", urlString: url, parameters: nil, error: &error)
request.allHTTPHeaderFields = [ "Content-Type" : "application/json", "Authorization" : "Bearer \(token)"]
request.allHTTPHeaderFields = [ "Content-Type" : "application/json", "Authorization" : "Bearer \(token)" ]
if let error = error {
print("[Loki] Couldn't download open group avatar due to error: \(error).")
return
}
OnionRequestAPI.sendOnionRequest(request, to: server, using: serverPublicKey, isJSONRequired: false).map{ json in
guard let body = json["body"] as? JSON, let dataArray = body["data"] as? [UInt8] else {
print("[Loki] Couldn't download open group avatar.")
OnionRequestAPI.sendOnionRequest(request, to: server, using: serverPublicKey, isJSONRequired: false).map(on: DispatchQueue.global(qos: .default)) { json in
guard let body = json["body"] as? JSON, let data = body["data"] as? [UInt8] else {
print("[Loki] Couldn't parse open group profile picture from: \(json).")
return
}
let avatarData = Data(dataArray)
let attachmentStream = TSAttachmentStream(contentType: OWSMimeTypeImageJpeg, byteCount: UInt32(avatarData.count), sourceFilename: nil, caption: nil, albumMessageId: nil)
try! attachmentStream.write(avatarData)
let profilePicture = Data(data)
let attachmentStream = TSAttachmentStream(contentType: OWSMimeTypeImageJpeg, byteCount: UInt32(profilePicture.count),
sourceFilename: nil, caption: nil, albumMessageId: nil)
try! attachmentStream.write(profilePicture)
groupThread.updateAvatar(with: attachmentStream)
}
}

@ -518,8 +518,7 @@ typedef void (^AttachmentDownloadFailure)(NSError *error);
failureHandlerParam(task, error);
};
AnyPromise *promise = [LKFileServerAPI downloadAttachment:location];
[promise.then(^(NSData *data) {
[[LKFileServerAPI downloadAttachmentFrom:location].then(^(NSData *data) {
BOOL success = [data writeToFile:tempFilePath atomically:YES];
if (success) {
successHandler(tempFilePath);

Loading…
Cancel
Save