ImageMessage.m 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 "ImageMessage.h"
  21. #import "NSString+Hex.h"
  22. #import "UTIConverter.h"
  23. #import "ThreemaFramework/ThreemaFramework-swift.h"
  24. @implementation ImageMessage
  25. @dynamic image;
  26. @dynamic thumbnail;
  27. @dynamic imageBlobId;
  28. @dynamic imageNonce;
  29. @dynamic imageSize;
  30. @dynamic progress;
  31. @dynamic encryptionKey;
  32. - (NSString*)logText {
  33. if ([self.image getCaption] != nil) {
  34. return [NSString stringWithFormat:@"%@ (%@) %@ %@", NSLocalizedString(@"image", nil), [self blobGetFilename], NSLocalizedString(@"caption", nil), [self.image getCaption]];
  35. }
  36. return [NSString stringWithFormat:@"%@ (%@)", NSLocalizedString(@"image", nil), [self blobGetFilename]];
  37. }
  38. - (NSString*)previewText {
  39. return NSLocalizedString(@"image", nil);
  40. }
  41. - (NSData *)blobGetData {
  42. if (self.image) {
  43. return self.image.data;
  44. }
  45. return nil;
  46. }
  47. - (NSData *)blobGetId {
  48. return self.imageBlobId;
  49. }
  50. - (NSData *)blobGetEncryptionKey {
  51. return self.encryptionKey;
  52. }
  53. - (NSNumber *)blobGetSize {
  54. return self.imageSize;
  55. }
  56. - (void)blobSetData:(NSData *)data {
  57. ImageData *dbData = [NSEntityDescription
  58. insertNewObjectForEntityForName:@"ImageData"
  59. inManagedObjectContext:self.managedObjectContext];
  60. dbData.data = data;
  61. self.image = dbData;
  62. }
  63. - (NSData *)blobGetThumbnail {
  64. if (self.thumbnail) {
  65. return self.thumbnail.data;
  66. }
  67. return nil;
  68. }
  69. - (NSString *)blobGetUTI {
  70. return UTTYPE_IMAGE;
  71. }
  72. - (NSString *)blobGetFilename {
  73. return [NSString stringWithFormat: @"%@.%@", [NSString stringWithHexData:self.id], MEDIA_EXTENSION_IMAGE];
  74. }
  75. - (NSString *)blobGetWebFilename {
  76. return [NSString stringWithFormat: @"threema-%@-image.%@", [DateFormatter getDateForWeb:self.date], MEDIA_EXTENSION_IMAGE];
  77. }
  78. - (void)blobUpdateProgress:(NSNumber *)progress {
  79. self.progress = progress;
  80. }
  81. - (NSNumber *)blobGetProgress {
  82. return self.progress;
  83. }
  84. #pragma mark ExternalStorageInfo
  85. - (NSString *)getFilename {
  86. return self.image != nil ? [self getFilename:self.image.data] : nil;
  87. }
  88. - (NSString *)getThumbnailname {
  89. return self.thumbnail != nil ? [self getFilename:self.thumbnail.data] : nil;
  90. }
  91. - (NSString *)getFilename:(NSData *)ofData {
  92. if (ofData != nil && [ofData respondsToSelector:NSSelectorFromString(@"filename")]) {
  93. return [ofData performSelector:NSSelectorFromString(@"filename")];
  94. }
  95. return nil;
  96. }
  97. - (NSString *)quotePreviewText {
  98. NSString *caption = nil;
  99. if (self.image != nil) {
  100. caption = [self.image getCaption];
  101. if (caption != nil) {
  102. NSString *quotePreviewString = [[caption componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]] componentsJoinedByString:@" "];
  103. caption = [quotePreviewString substringWithRange:NSMakeRange(0, MIN(caption.length, 200))];
  104. }
  105. }
  106. if (caption == nil || caption.length == 0) {
  107. caption = self.previewText;
  108. }
  109. return caption;
  110. }
  111. #ifdef DEBUG
  112. #else
  113. - (NSString *)debugDescription
  114. {
  115. return [NSString stringWithFormat:@"<%@: %p> %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@", [self class], self, @"image =", self.image.description, @"thumbnail =", self.thumbnail.description, @"imageBlobId =", @"***", @"imageNonce =", @"***", @"imageSize =", self.imageSize.description, @"progress =", self.progress.description, @"encryptionKey =", self.encryptionKey.description];
  116. }
  117. #endif
  118. @end