CloudUploadSession.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. //
  2. // CloudUploadSession.m
  3. // zhuxun
  4. //
  5. // Created by winsoft on 17/6/27.
  6. //
  7. //
  8. #import "CloudUploadSession.h"
  9. #import <MJExtension/MJExtension.h>
  10. #import "HttpRequest.h"
  11. #import "CloudStorageDB.h"
  12. @interface CloudUploadSession()
  13. @property (nonatomic , strong) WSUploadSession *uploadSession;
  14. @property (nonatomic , strong) NSMutableDictionary *successBlocksDict;
  15. @property (nonatomic , strong) NSMutableDictionary *failureBlocksDict;
  16. @property (nonatomic , strong) NSMutableDictionary *progressBlocksDict;
  17. @property (nonatomic , assign) NSOperationQueue *operationQueue;
  18. @property (nonatomic , copy) NSDate *lastRecordTime;
  19. @end
  20. @implementation CloudUploadSession
  21. - (NSMutableDictionary *)successBlocksDict
  22. {
  23. if (!_successBlocksDict) {
  24. _successBlocksDict = [NSMutableDictionary dictionary];
  25. }
  26. return _successBlocksDict;
  27. }
  28. - (NSMutableDictionary *)failureBlocksDict
  29. {
  30. if (!_failureBlocksDict) {
  31. _failureBlocksDict = [NSMutableDictionary dictionary];
  32. }
  33. return _failureBlocksDict;
  34. }
  35. - (NSMutableDictionary *)progressBlocksDict
  36. {
  37. if (!_progressBlocksDict) {
  38. _progressBlocksDict = [NSMutableDictionary dictionary];
  39. }
  40. return _progressBlocksDict;
  41. }
  42. + (CloudUploadSession *)startUploadFileWithFileDownUpModel:(CSFileDownUpModel *)fileDownUpModel operationQueue:(NSOperationQueue *)operationQueue
  43. {
  44. CloudUploadSession *cloudUploadSession = [[CloudUploadSession alloc]initCloudUploadSessionWithFileDownUpModel:fileDownUpModel operationQueue:operationQueue];
  45. return cloudUploadSession;
  46. }
  47. - (instancetype)initCloudUploadSessionWithFileDownUpModel:(CSFileDownUpModel *)fileDownUpModel operationQueue:(NSOperationQueue *)operationQueue
  48. {
  49. if (self = [super init]) {
  50. _operationQueue = operationQueue;
  51. _fileDownUpModel = fileDownUpModel;
  52. NSString *localFullPath = nil;
  53. if (fileDownUpModel.fileSouce == FileSourceFileLocal) {
  54. localFullPath = [NSString stringWithFormat:@"%@/%@",fileDownUpModel.fileType == FileTypeOther?FileFolderFullRouter:PhotosMessageDir,fileDownUpModel.localRootPath];
  55. }else localFullPath = [NSString stringWithFormat:@"%@/%@",CloudStorageFullRouter,fileDownUpModel.localRootPath];
  56. [self startUploadFileWithfileDownUpModel:fileDownUpModel localFullPath:localFullPath operationQueue:operationQueue];
  57. }
  58. return self;
  59. }
  60. - (void)startUploadFileWithfileDownUpModel:(CSFileDownUpModel *)fileDownUpModel localFullPath:(NSString *)localFullPath operationQueue:(NSOperationQueue *)operationQueue
  61. {
  62. // __weak typeof(self)weakSelf = self;
  63. [[HttpRequest sharedManager]cloudStorageGetFileUploadURLWithMD5:fileDownUpModel.md5 complete:^(BOOL success, id data, NSError *error) {
  64. NSString *host = [data[@"host"]componentsSeparatedByString:@":"][0];
  65. NSString *port = [data[@"host"]componentsSeparatedByString:@":"][1];
  66. if (success && host && port) {
  67. self.uploadSession = [WSUploadSession uploadFileWithServerHost:host port:port fileMD5:fileDownUpModel.md5 uuid:fileDownUpModel.downUpUUID fileSize:fileDownUpModel.size startSize:fileDownUpModel.downUpSize localFileFullPath:localFullPath processBlock:nil successBlock:nil failBlock:nil];
  68. // [operationQueue addOperation:self.uploadSession];
  69. __weak typeof(self)weakself = self;
  70. self.uploadSession.uploadSuccessBlock = ^(NSString* url,long long fileSize,NSString *msgUUID){
  71. weakself.fileDownUpModel.downUpSize = fileSize;
  72. [[HttpRequest sharedManager]cloudStorageCreateNewFileWithFileName:fileDownUpModel.name md5:fileDownUpModel.md5 pid:fileDownUpModel.pid delflag:nil ext:fileDownUpModel.ext size:[NSNumber numberWithInteger:fileDownUpModel.size] complete:^(BOOL success, id data, NSError *error) {
  73. /*
  74. data = {
  75. "_id" = 59546bfd8145020d6ce090bc;
  76. delflag = 0;
  77. ext = PNG;
  78. isdir = 0;
  79. md5 = e19af74a289b0973c1e402a9c131dd68;
  80. name = "e19af74a289b0973c1e402a9c131dd68.dat?786D74B9-E5B9-47FB-B3D9-F1F7AAA8BBCE.PNG";
  81. pid = 594b894f66be760240cac18b;
  82. "server_ctime" = 1498704893428;
  83. size = 774598;
  84. "update_time" = 1498704893428;
  85. "user_id" = 7616;
  86. };
  87. message = "";
  88. success = 1;
  89. */
  90. NSLog(@"%@",error);
  91. if (success && data) {
  92. weakself.fileDownUpModel.downUpStatus = DownUpStatusSuccess;
  93. #warning 2017-07-25 缺少serverfullpath?
  94. CSFileModel *csFileModel = [CSFileModel mj_objectWithKeyValues:data[@"data"]];
  95. csFileModel.serverFullPath = weakself.fileDownUpModel.serverFullPath;
  96. weakself.fileDownUpModel.sid = csFileModel.sid;
  97. // add file to cache
  98. [kCloudStorageDB addcsFiletoDB:csFileModel];
  99. //update upfilemodel
  100. [kCloudStorageDB updateCSDownUpFileIDWithCSFileDownUpModel:weakself.fileDownUpModel sid:csFileModel.sid];
  101. [kCloudStorageDB updateCSDownUpFileStatusWithCSFileDownUpModel:weakself.fileDownUpModel];
  102. // if (weakself.uploadSuccessBlock) {
  103. // weakself.uploadSuccessBlock(url,fileSize,weakself.fileDownUpModel,weakself.fileDownUpModel.downUpUUID);
  104. // }
  105. int i = 0;
  106. for (CSUploadSuccessUIBlock succBlock in weakself.successBlocksDict.allValues) {
  107. NSString *key = weakself.successBlocksDict.allKeys[i];
  108. if (succBlock) {
  109. //一方面,所有的model都注册了,另一方面,有时候多个model公共一个task.
  110. succBlock(url,fileSize,weakself.fileDownUpModel,key);
  111. }
  112. i++;
  113. }
  114. if (weakself.uploadSuccessDBBlock) {
  115. weakself.uploadSuccessDBBlock(url,fileSize,weakself.fileDownUpModel);
  116. }
  117. }else{
  118. weakself.fileDownUpModel.downUpStatus = DownUpStatusFailure;
  119. [kCloudStorageDB updateCSDownUpFileStatusWithCSFileDownUpModel:weakself.fileDownUpModel];
  120. int i = 0;
  121. for (CSUploadFailedUIBlock faiureBlock in weakself.failureBlocksDict.allValues) {
  122. NSString *key = weakself.failureBlocksDict.allKeys[i];
  123. if (faiureBlock) {
  124. //一方面,所有的model都注册了,另一方面,有时候多个model公共一个task.
  125. faiureBlock(error.description,weakself.fileDownUpModel,key);
  126. }
  127. i++;
  128. }
  129. if (weakself.uploadFailedDBBlock) {
  130. weakself.uploadFailedDBBlock (error.description,weakself.fileDownUpModel);
  131. }
  132. }
  133. }];
  134. };
  135. self.uploadSession.uploadProgressBlock = ^(double progress,NSInteger sendedSize, NSString *msgUUID){
  136. weakself.fileDownUpModel.downUpSize = sendedSize;
  137. NSDate *now = [NSDate date];
  138. if (!weakself.lastRecordTime || [now timeIntervalSinceDate:weakself.lastRecordTime] >= 1) {
  139. //存储
  140. [kCloudStorageDB updateCSDownUpFileStatusWithCSFileDownUpModel:weakself.fileDownUpModel];
  141. weakself.lastRecordTime = now;
  142. }
  143. int i = 0;
  144. for (CSUploadProgressUIBlock proBlock in weakself.progressBlocksDict.allValues) {
  145. NSString *key = weakself.progressBlocksDict.allKeys[i];
  146. if (proBlock) {
  147. //一方面,所有的model都注册了,另一方面,有时候多个model公共一个task.
  148. proBlock(progress,sendedSize,weakself.fileDownUpModel,key);
  149. }
  150. i++;
  151. }
  152. if (weakself.uploadProgressDBBlock) {
  153. weakself.uploadProgressDBBlock(progress,sendedSize,weakself.fileDownUpModel);
  154. }
  155. };
  156. self.uploadSession.uploadFailedBlock = ^(NSString* errMessage,NSString *msgUUID){
  157. weakself.fileDownUpModel.downUpStatus = DownUpStatusFailure;
  158. [kCloudStorageDB updateCSDownUpFileStatusWithCSFileDownUpModel:weakself.fileDownUpModel];
  159. int i = 0;
  160. for (CSUploadFailedUIBlock faiureBlock in weakself.failureBlocksDict.allValues) {
  161. NSString *key = weakself.failureBlocksDict.allKeys[i];
  162. if (faiureBlock) {
  163. //一方面,所有的model都注册了,另一方面,有时候多个model公共一个task.
  164. faiureBlock(errMessage,weakself.fileDownUpModel,key);
  165. }
  166. i++;
  167. }
  168. if (weakself.uploadFailedDBBlock) {
  169. weakself.uploadFailedDBBlock (errMessage,weakself.fileDownUpModel);
  170. }
  171. };
  172. }else{
  173. //失败
  174. self.fileDownUpModel.downUpStatus = DownUpStatusFailure;
  175. [kCloudStorageDB updateCSDownUpFileStatusWithCSFileDownUpModel:self.fileDownUpModel];
  176. int i = 0;
  177. for (CSUploadFailedUIBlock faiureBlock in self.failureBlocksDict.allValues) {
  178. NSString *key = self.failureBlocksDict.allKeys[i];
  179. if (faiureBlock) {
  180. //一方面,所有的model都注册了,另一方面,有时候多个model公共一个task.
  181. faiureBlock(nil,self.fileDownUpModel,key);
  182. }
  183. i++;
  184. }
  185. if (self.uploadFailedDBBlock) {
  186. self.uploadFailedDBBlock (nil,self.fileDownUpModel);
  187. }
  188. }
  189. }];
  190. }
  191. - (void)addBlockToUIWithUUID:(NSString *)uuid withProcessBlock:(CSUploadProgressUIBlock)processBlock successBlock:(CSUploadSuccessUIBlock)successBlock failBlock:(CSUploadFailedUIBlock)failBlock
  192. {
  193. [self.successBlocksDict setObject:successBlock forKey:uuid];
  194. [self.progressBlocksDict setObject:processBlock forKey:uuid];
  195. [self.failureBlocksDict setObject:failBlock forKey:uuid];
  196. }
  197. - (void)cancerUpload
  198. {
  199. [self.uploadSession cancerUpload];
  200. }
  201. @end