ImageMessageLoader.m 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2012-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 "ImageMessageLoader.h"
  21. #import "ImageMessage.h"
  22. #import "ImageData.h"
  23. #import "MyIdentityStore.h"
  24. #import "ProtocolDefines.h"
  25. #import "NaClCrypto.h"
  26. #import "MessageSender.h"
  27. #import "EntityManager.h"
  28. #import "UserSettings.h"
  29. #import "MediaConverter.h"
  30. #import "Threema-Swift.h"
  31. #ifdef DEBUG
  32. static const DDLogLevel ddLogLevel = DDLogLevelVerbose;
  33. #else
  34. static const DDLogLevel ddLogLevel = DDLogLevelWarning;
  35. #endif
  36. @interface ImageMessageLoader ()
  37. @property UIImage *image;
  38. @end
  39. @implementation ImageMessageLoader
  40. - (NSData *)decryptData:(NSData *)data {
  41. NSData *decryptedData = nil;
  42. @try {
  43. NSData *encryptionKey = [self.message blobGetEncryptionKey];
  44. if (encryptionKey != nil) {
  45. decryptedData = [[NaClCrypto sharedCrypto] symmetricDecryptData:data withKey:encryptionKey nonce:[NSData dataWithBytesNoCopy:kNonce_1 length:sizeof(kNonce_1) freeWhenDone:NO]];
  46. } else {
  47. NSData *imageNonce = ((ImageMessage *)self.message).imageNonce;
  48. NSData *publicKey = self.message.conversation.contact.publicKey;
  49. decryptedData = [[MyIdentityStore sharedMyIdentityStore] decryptData:data withNonce:imageNonce publicKey:publicKey];
  50. }
  51. if (decryptedData) {
  52. _image = [UIImage imageWithData:decryptedData];
  53. if (_image == nil) {
  54. DDLogError(@"Image decoding failed");
  55. decryptedData = nil;
  56. }
  57. }
  58. } @catch (NSException *exception) {
  59. DDLogError(@"Image decryption failed: %@", [exception description]);
  60. }
  61. return decryptedData;
  62. }
  63. - (void)updateDBObjectWithData:(NSData *)data onCompletion:(void(^)(void))onCompletion {
  64. dispatch_async(dispatch_get_main_queue(), ^{
  65. UIImage *thumbnail = [MediaConverter getThumbnailForImage:_image];
  66. NSData *thumbnailData = UIImageJPEGRepresentation(thumbnail, kJPEGCompressionQuality);
  67. EntityManager *entityManager = [[EntityManager alloc] init];
  68. [entityManager performSyncBlockAndSafe:^{
  69. ImageMessage *imageMessage = (ImageMessage *)self.message;
  70. [imageMessage blobSetData:data];
  71. imageMessage.sendFailed = [NSNumber numberWithBool:NO];
  72. [imageMessage blobUpdateProgress:nil];
  73. ImageData *dbThumbnail = [entityManager.entityCreator imageData];
  74. dbThumbnail.data = thumbnailData;
  75. dbThumbnail.width = [NSNumber numberWithInt:thumbnail.size.width];
  76. dbThumbnail.height = [NSNumber numberWithInt:thumbnail.size.height];
  77. imageMessage.thumbnail = dbThumbnail;
  78. }];
  79. /* Add to photo library */
  80. if ([UserSettings sharedUserSettings].autoSaveMedia) {
  81. [[AlbumManager shared] saveWithImage:_image];
  82. }
  83. onCompletion();
  84. });
  85. }
  86. @end