Update AttachmentDownloadJob for the V2 file server

pull/394/head
Niels Andriesse 5 years ago
parent 3e11c505e2
commit 6b97f86c32

@ -96,39 +96,41 @@ public final class AttachmentDownloadJob : NSObject, Job, NSCoding { // NSObject
return handleFailure(Error.invalidURL) return handleFailure(Error.invalidURL)
} }
OpenGroupAPIV2.download(file, from: v2OpenGroup.room, on: v2OpenGroup.server).done(on: DispatchQueue.global(qos: .userInitiated)) { data in OpenGroupAPIV2.download(file, from: v2OpenGroup.room, on: v2OpenGroup.server).done(on: DispatchQueue.global(qos: .userInitiated)) { data in
do { self.handleDownloadedAttachment(data: data, temporaryFilePath: temporaryFilePath, pointer: pointer, failureHandler: handleFailure)
try data.write(to: temporaryFilePath, options: .atomic) }.catch(on: DispatchQueue.global()) { error in
} catch { handleFailure(error)
return handleFailure(error)
} }
let stream = TSAttachmentStream(pointer: pointer) } else if pointer.downloadURL.contains(FileServerAPIV2.server) {
do { guard let fileAsString = pointer.downloadURL.split(separator: "/").last, let file = UInt64(fileAsString) else {
try stream.write(data) return handleFailure(Error.invalidURL)
} catch {
return handleFailure(error)
} }
OWSFileSystem.deleteFile(temporaryFilePath.absoluteString) FileServerAPIV2.download(file).done(on: DispatchQueue.global(qos: .userInitiated)) { data in
storage.write(with: { transaction in self.handleDownloadedAttachment(data: data, temporaryFilePath: temporaryFilePath, pointer: pointer, failureHandler: handleFailure)
storage.persist(stream, associatedWith: self.tsMessageID, using: transaction)
}, completion: {
self.handleSuccess()
})
}.catch(on: DispatchQueue.global()) { error in }.catch(on: DispatchQueue.global()) { error in
handleFailure(error) handleFailure(error)
} }
} else { } else { // Legacy
FileServerAPI.downloadAttachment(from: pointer.downloadURL).done(on: DispatchQueue.global(qos: .userInitiated)) { data in FileServerAPI.downloadAttachment(from: pointer.downloadURL).done(on: DispatchQueue.global(qos: .userInitiated)) { data in
self.handleDownloadedAttachment(data: data, temporaryFilePath: temporaryFilePath, pointer: pointer, failureHandler: handleFailure)
}.catch(on: DispatchQueue.global()) { error in
handleFailure(error)
}
}
}
private func handleDownloadedAttachment(data: Data, temporaryFilePath: URL, pointer: TSAttachmentPointer, failureHandler: (Swift.Error) -> Void) {
let storage = SNMessagingKitConfiguration.shared.storage
do { do {
try data.write(to: temporaryFilePath, options: .atomic) try data.write(to: temporaryFilePath, options: .atomic)
} catch { } catch {
return handleFailure(error) return failureHandler(error)
} }
let plaintext: Data let plaintext: Data
if let key = pointer.encryptionKey, let digest = pointer.digest { if let key = pointer.encryptionKey, let digest = pointer.digest {
do { do {
plaintext = try Cryptography.decryptAttachment(data, withKey: key, digest: digest, unpaddedSize: pointer.byteCount) plaintext = try Cryptography.decryptAttachment(data, withKey: key, digest: digest, unpaddedSize: pointer.byteCount)
} catch { } catch {
return handleFailure(error) return failureHandler(error)
} }
} else { } else {
plaintext = data // Open group attachments are unencrypted plaintext = data // Open group attachments are unencrypted
@ -137,7 +139,7 @@ public final class AttachmentDownloadJob : NSObject, Job, NSCoding { // NSObject
do { do {
try stream.write(plaintext) try stream.write(plaintext)
} catch { } catch {
return handleFailure(error) return failureHandler(error)
} }
OWSFileSystem.deleteFile(temporaryFilePath.absoluteString) OWSFileSystem.deleteFile(temporaryFilePath.absoluteString)
storage.write(with: { transaction in storage.write(with: { transaction in
@ -145,10 +147,6 @@ public final class AttachmentDownloadJob : NSObject, Job, NSCoding { // NSObject
}, completion: { }, completion: {
self.handleSuccess() self.handleSuccess()
}) })
}.catch(on: DispatchQueue.global()) { error in
handleFailure(error)
}
}
} }
private func handleSuccess() { private func handleSuccess() {

Loading…
Cancel
Save