FileMessageEncoder.m 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 "FileMessageEncoder.h"
  21. #import "FileMessageKeys.h"
  22. #import "NSString+Hex.h"
  23. #import "JsonUtil.h"
  24. #ifdef DEBUG
  25. static const DDLogLevel ddLogLevel = DDLogLevelVerbose;
  26. #else
  27. static const DDLogLevel ddLogLevel = DDLogLevelWarning;
  28. #endif
  29. @implementation FileMessageEncoder
  30. + (BoxFileMessage *)encodeFileMessage:(FileMessage *)message {
  31. NSData *jsonData = [self jsonDataForMessage:message];
  32. BoxFileMessage *boxMessage = [[BoxFileMessage alloc] init];
  33. boxMessage.messageId = [AbstractMessage randomMessageId];
  34. boxMessage.date = [NSDate date];
  35. boxMessage.jsonData = jsonData;
  36. return boxMessage;
  37. }
  38. + (GroupFileMessage *)encodeGroupFileMessage:(FileMessage *)message {
  39. NSData *jsonData = [self jsonDataForMessage:message];
  40. GroupFileMessage *boxMessage = [[GroupFileMessage alloc] init];
  41. boxMessage.messageId = [AbstractMessage randomMessageId];
  42. boxMessage.date = [NSDate date];
  43. boxMessage.jsonData = jsonData;
  44. return boxMessage;
  45. }
  46. + (NSString *)jsonStringForMessage:(FileMessage *)message {
  47. NSData *jsonData = [self jsonDataForMessage:message];
  48. return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
  49. }
  50. + (NSData *)jsonDataForMessage:(FileMessage *)message {
  51. NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
  52. if (message.fileName) {
  53. [dictionary setObject:message.fileName forKey:JSON_FILE_KEY_FILENAME];
  54. }
  55. [dictionary setObject:message.fileSize forKey:JSON_FILE_KEY_FILESIZE];
  56. [dictionary setObject:message.mimeType forKey:JSON_FILE_KEY_MIMETYPE];
  57. [dictionary setObject:message.type forKey:JSON_FILE_KEY_TYPE];
  58. [dictionary setObject:[NSNumber numberWithInt:0] forKey:JSON_FILE_KEY_TYPE_DEPRECATED];
  59. if (message.correlationId) {
  60. [dictionary setObject:message.correlationId forKey:JSON_FILE_KEY_CORRELATION];
  61. }
  62. NSString *encryptionKeyHex = [NSString stringWithHexData:message.encryptionKey];
  63. [dictionary setObject:encryptionKeyHex forKey:JSON_FILE_KEY_ENCRYPTION_KEY];
  64. if (message.blobId) {
  65. NSString *blobIdHex = [NSString stringWithHexData:message.blobId];
  66. [dictionary setObject:blobIdHex forKey:JSON_FILE_KEY_FILE_BLOB];
  67. }
  68. if (message.blobThumbnailId) {
  69. NSString *thumbnailIdHex = [NSString stringWithHexData:message.blobThumbnailId];
  70. [dictionary setObject:thumbnailIdHex forKey:JSON_FILE_KEY_THUMBNAIL_BLOB];
  71. }
  72. if (message.caption) {
  73. [dictionary setObject:message.caption forKey:JSON_FILE_KEY_DESCRIPTION];
  74. }
  75. NSMutableDictionary *metaDict = [NSMutableDictionary dictionary];
  76. if (message.duration > 0) {
  77. NSNumber *duration = [[NSNumber alloc] initWithFloat:message.duration.floatValue];
  78. [metaDict setObject:duration forKey:JSON_FILE_KEY_METADATA_DURATION];
  79. }
  80. if (message.height > 0) {
  81. [metaDict setObject:message.height forKey:JSON_FILE_KEY_METADATA_HEIGHT];
  82. }
  83. if (message.width > 0) {
  84. [metaDict setObject:message.width forKey:JSON_FILE_KEY_METADATA_WIDTH];
  85. }
  86. if ([metaDict count] > 0) {
  87. [dictionary setObject:metaDict forKey:JSON_FILE_KEY_METADATA];
  88. }
  89. NSError *error;
  90. NSData *jsonData = [JsonUtil serializeJsonFrom:dictionary error:error];
  91. if (jsonData == nil) {
  92. DDLogError(@"Error encoding json data %@, %@", error, [error userInfo]);
  93. }
  94. return jsonData;
  95. }
  96. @end