TUIFileMessageCellData.m 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. //
  2. // TUIFileMessageCellData.m
  3. // TXIMSDK_TUIKit_iOS
  4. //
  5. // Created by annidyfeng on 2019/5/21.
  6. // Copyright © 2023 Tencent. All rights reserved.
  7. //
  8. #import "TUIFileMessageCellData.h"
  9. #import <TIMCommon/TIMDefine.h>
  10. #import <TUICore/NSString+TUIUtil.h>
  11. #import "TUIMessageProgressManager.h"
  12. @interface TUIFileMessageCellData ()
  13. @property(nonatomic, strong) NSMutableArray *progressBlocks;
  14. @property(nonatomic, strong) NSMutableArray *responseBlocks;
  15. @end
  16. @implementation TUIFileMessageCellData
  17. + (TUIMessageCellData *)getCellData:(V2TIMMessage *)message {
  18. V2TIMFileElem *elem = message.fileElem;
  19. TUIFileMessageCellData *fileData = [[TUIFileMessageCellData alloc] initWithDirection:(message.isSelf ? MsgDirectionOutgoing : MsgDirectionIncoming)];
  20. fileData.path = [elem.path safePathString];
  21. fileData.fileName = elem.filename;
  22. fileData.length = elem.fileSize;
  23. fileData.uuid = elem.uuid;
  24. fileData.reuseId = TFileMessageCell_ReuseId;
  25. return fileData;
  26. }
  27. + (NSString *)getDisplayString:(V2TIMMessage *)message {
  28. return TIMCommonLocalizableString(TUIkitMessageTypeFile); // @"[File]";
  29. }
  30. - (Class)getReplyQuoteViewDataClass {
  31. return NSClassFromString(@"TUIFileReplyQuoteViewData");
  32. }
  33. - (Class)getReplyQuoteViewClass {
  34. return NSClassFromString(@"TUIFileReplyQuoteView");
  35. }
  36. - (int)length {
  37. if (self.innerMessage) {
  38. _length = self.innerMessage.fileElem.fileSize;
  39. }
  40. return _length;
  41. }
  42. - (instancetype)initWithDirection:(TMsgDirection)direction {
  43. self = [super initWithDirection:direction];
  44. if (self) {
  45. _uploadProgress = 100;
  46. _downladProgress = 100;
  47. _isDownloading = NO;
  48. _progressBlocks = [NSMutableArray array];
  49. _responseBlocks = [NSMutableArray array];
  50. }
  51. return self;
  52. }
  53. - (void)downloadFile {
  54. BOOL isExist = NO;
  55. NSString *path = [self getFilePath:&isExist];
  56. if (isExist) {
  57. return;
  58. }
  59. NSInteger progress = [TUIMessageProgressManager.shareManager downloadProgressForMessage:self.msgID];
  60. if (progress != 0) {
  61. return;
  62. }
  63. if (self.isDownloading) return;
  64. self.isDownloading = YES;
  65. @weakify(self);
  66. if (self.innerMessage.elemType == V2TIM_ELEM_TYPE_FILE) {
  67. NSString *msgID = self.msgID;
  68. [self.innerMessage.fileElem downloadFile:path
  69. progress:^(NSInteger curSize, NSInteger totalSize) {
  70. @strongify(self);
  71. NSInteger progress = curSize * 100 / totalSize;
  72. [self updateDownalodProgress:MIN(progress, 99)];
  73. [TUIMessageProgressManager.shareManager appendDownloadProgress:msgID progress:MIN(progress, 99)];
  74. }
  75. succ:^{
  76. @strongify(self);
  77. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  78. self.isDownloading = NO;
  79. [self updateDownalodProgress:100];
  80. [TUIMessageProgressManager.shareManager appendDownloadProgress:msgID progress:100];
  81. dispatch_async(dispatch_get_main_queue(), ^{
  82. self.path = path;
  83. });
  84. });
  85. }
  86. fail:^(int code, NSString *msg) {
  87. @strongify(self);
  88. self.isDownloading = NO;
  89. }];
  90. }
  91. }
  92. - (void)updateDownalodProgress:(NSUInteger)progress {
  93. dispatch_async(dispatch_get_main_queue(), ^{
  94. self.downladProgress = progress;
  95. });
  96. }
  97. - (BOOL)isLocalExist {
  98. BOOL isExist;
  99. [self getFilePath:&isExist];
  100. return isExist;
  101. }
  102. - (NSString *)getFilePath:(BOOL *)isExist {
  103. NSString *path = nil;
  104. BOOL isDir = NO;
  105. *isExist = NO;
  106. if (self.direction == MsgDirectionOutgoing) {
  107. // The origin file path is valid when uploading
  108. path = [NSString stringWithFormat:@"%@%@", TUIKit_File_Path, _path.lastPathComponent];
  109. if ([[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:&isDir]) {
  110. if (!isDir) {
  111. *isExist = YES;
  112. }
  113. }
  114. }
  115. if (!*isExist) {
  116. path = [NSString stringWithFormat:@"%@%@%@", TUIKit_File_Path,self.uuid, _fileName];
  117. if ([[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:&isDir]) {
  118. if (!isDir) {
  119. *isExist = YES;
  120. }
  121. }
  122. }
  123. if (*isExist) {
  124. _path = path;
  125. }
  126. // TODO: uuid
  127. return path;
  128. }
  129. @end