BlobMessageLoader.m 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2015-2020 Threema GmbH
  8. //
  9. // This program is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU Affero General Public License, version 3,
  11. // as published by the Free Software Foundation.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU Affero General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Affero General Public License
  19. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. #import "BlobMessageLoader.h"
  21. #import "ProtocolDefines.h"
  22. #import "NaClCrypto.h"
  23. #import "MessageSender.h"
  24. #import "EntityManager.h"
  25. #import "ThreemaError.h"
  26. #import "PinnedHTTPSURLLoader.h"
  27. #import "Threema-Swift.h"
  28. #import "UserSettings.h"
  29. #import "MediaConverter.h"
  30. #ifdef DEBUG
  31. static const DDLogLevel ddLogLevel = DDLogLevelVerbose;
  32. #else
  33. static const DDLogLevel ddLogLevel = DDLogLevelWarning;
  34. #endif
  35. @interface BlobMessageLoader () <HTTPSURLLoaderDelegate>
  36. @end
  37. @implementation BlobMessageLoader
  38. - (void)startWithMessage:(BaseMessage<BlobData> *)message onCompletion:(void (^)(BaseMessage<BlobData> *loadedMessage))onCompletion onError:(void (^)(NSError *error))onError {
  39. _message = message;
  40. NSData *blobData = [_message blobGetData];
  41. if (blobData != nil) {
  42. onCompletion(message);
  43. return;
  44. }
  45. NSData *blobId = [_message blobGetId];
  46. if (blobId == nil) {
  47. DDLogWarn(@"Missing blob ID or encryption key!");
  48. onError([ThreemaError threemaError:[BundleUtil localizedStringForKey:@"media_file_not_found"]]);
  49. return;
  50. }
  51. NSData *encryptionKey = [_message blobGetEncryptionKey];
  52. if (encryptionKey == nil) {
  53. // handle image message backward compatibility
  54. if ([message isKindOfClass:[ImageMessage class]]) {
  55. if (((ImageMessage *)message).imageNonce == nil) {
  56. DDLogWarn(@"Missing image encryption key or nonce!");
  57. return;
  58. }
  59. } else {
  60. DDLogWarn(@"Missing encryption key!");
  61. return;
  62. }
  63. }
  64. NSNumber *progress = [_message blobGetProgress];
  65. if (progress != nil) {
  66. DDLogWarn(@"Blob download already in progress");
  67. return;
  68. }
  69. _message = message;
  70. [_message blobUpdateProgress:[NSNumber numberWithFloat:0]];
  71. PinnedHTTPSURLLoader *loader = [[PinnedHTTPSURLLoader alloc] init];
  72. loader.delegate = self;
  73. [loader startWithBlobId:blobId onCompletion:^(NSData *data) {
  74. if ([_message wasDeleted]) {
  75. return;
  76. }
  77. NSData *decryptedData = [self decryptData:data];
  78. if (decryptedData == nil) {
  79. onError([ThreemaError threemaError:@"Blob data decryption failed"]);
  80. return;
  81. }
  82. [self updateDBObjectWithData:decryptedData onCompletion:^{
  83. if (_message.conversation.groupId == nil) {
  84. [MessageSender markBlobAsDone:blobId];
  85. }
  86. DDLogInfo(@"Blob successfully downloaded (%lu bytes)", (unsigned long)data.length);
  87. onCompletion(_message);
  88. }];
  89. } onError:^(NSError *error) {
  90. [[ValidationLogger sharedValidationLogger] logString:error.description];
  91. dispatch_async(dispatch_get_main_queue(), ^{
  92. if ([_message wasDeleted] == NO) {
  93. EntityManager *entityManager = [[EntityManager alloc] init];
  94. [entityManager performSyncBlockAndSafe:^{
  95. _message.sendFailed = [NSNumber numberWithBool:YES];
  96. [_message blobUpdateProgress:nil];
  97. }];
  98. }
  99. });
  100. onError([ThreemaError threemaError:[BundleUtil localizedStringForKey:@"media_file_not_found"]]);
  101. }];
  102. }
  103. - (NSData *)decryptData:(NSData *)data {
  104. NSData *decryptedData = nil;
  105. @try {
  106. NSData *encryptionKey = [_message blobGetEncryptionKey];
  107. decryptedData = [[NaClCrypto sharedCrypto] symmetricDecryptData:data withKey:encryptionKey nonce:[NSData dataWithBytesNoCopy:kNonce_1 length:sizeof(kNonce_1) freeWhenDone:NO]];
  108. } @catch (NSException *exception) {
  109. DDLogError(@"Blob decryption failed: %@", [exception description]);
  110. }
  111. return decryptedData;
  112. }
  113. - (void)updateDBObjectWithData:(NSData *)data onCompletion:(void(^)(void))onCompletion {
  114. dispatch_async(dispatch_get_main_queue(), ^{
  115. EntityManager *entityManager = [[EntityManager alloc] init];
  116. [entityManager performSyncBlockAndSafe:^{
  117. [_message blobSetData:data];
  118. _message.sendFailed = [NSNumber numberWithBool:NO];
  119. [_message blobUpdateProgress:nil];
  120. }];
  121. /* Add to photo library */
  122. if ([UserSettings sharedUserSettings].autoSaveMedia && [_message isKindOfClass:[FileMessage class]]) {
  123. FileMessage *fileMessage = (FileMessage *)_message;
  124. NSString *filename = [FileUtility getTemporaryFileName];
  125. __block NSURL *tmpFileUrl = [fileMessage tmpURL:filename];
  126. [fileMessage exportDataToURL:tmpFileUrl];
  127. BOOL isVideo = [UTIConverter isVideoMimeType:fileMessage.mimeType] || [UTIConverter isMovieMimeType:fileMessage.mimeType];
  128. BOOL isImage = [UTIConverter isImageMimeType:fileMessage.mimeType];
  129. BOOL isSticker = [fileMessage.type isEqual: @2];
  130. if (isVideo == true || (isImage == true && isSticker == false)) {
  131. [[AlbumManager shared] saveWithUrl:tmpFileUrl isVideo:isVideo completionHandler:^(BOOL success) {
  132. if (tmpFileUrl) {
  133. [[NSFileManager defaultManager] removeItemAtURL:tmpFileUrl error:nil];
  134. tmpFileUrl = nil;
  135. }
  136. }];
  137. }
  138. }
  139. onCompletion();
  140. });
  141. }
  142. #pragma mark HTTPSURLLoaderDelegate
  143. - (BOOL)httpsLoaderShouldCancel {
  144. if ([_message wasDeleted]) {
  145. return YES;
  146. }
  147. return NO;
  148. }
  149. - (void)httpsLoaderReceivedData:(NSData *)totalData {
  150. if ([_message wasDeleted]) {
  151. return;
  152. }
  153. [_message blobUpdateProgress:[NSNumber numberWithFloat:totalData.length / [_message blobGetSize].floatValue]];
  154. }
  155. @end