PlayerViewController+upLoadFile.m 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  1. //
  2. // PlayerViewController+upLoadFile.m
  3. // 隐私保护
  4. //
  5. // Created by xd h on 2024/1/3.
  6. //
  7. #import "PlayerViewController+upLoadFile.h"
  8. @implementation PlayerViewController (upLoadFile)
  9. #pragma mark 监听文件开始的的通知
  10. - (void)beginGotoUploadFileFun:(NSNotification *)notification
  11. {
  12. self.isSuspendUploadType = NO;
  13. uploadFileDataModel *uploadFileDataMod = [notification object];
  14. //HLog(@"%@",uploadFileDataMod);
  15. // if(self.curUploadFileDataMod && uploadFileDataMod.bg_id.integerValue == self.curUploadFileDataMod.bg_id.integerValue){
  16. // return;
  17. // }
  18. self.curUploadFileDataMod = uploadFileDataMod;
  19. if(![uploadFileDataMod isKindOfClass:[uploadFileDataMod class]]){
  20. HLog(@"上传的文件类型不对");
  21. return;
  22. }
  23. [self ApplyUploadFileFun];
  24. }
  25. - (void)applyUploadFileServiceResponseFun:(NSDictionary *)dataDict
  26. {
  27. if([dataDict isKindOfClass:[NSDictionary class]]){
  28. if ([[dataDict allKeys] containsObject:@"data"]) {
  29. NSDictionary *data = dataDict[@"data"];
  30. if ([[data allKeys] containsObject:@"status"]) {
  31. NSString *status = [data objectForKey:@"status"];
  32. if(![status isKindOfClass:[NSString class]]){
  33. status = [[NSString alloc] initWithFormat:@"%@",status];
  34. }
  35. //if(1)
  36. if([status isEqualToString:@"1"])
  37. {
  38. //[self tryAgainApplyUploadFun];
  39. [self clearUploadFileDataFun];
  40. [[uploadFileManager shareInstance] uploadFileFailFun];
  41. }else if ([status isEqualToString:@"0"]){
  42. if ([[data allKeys] containsObject:@"taskUid"]) {
  43. NSString *taskUid = data[@"taskUid"];
  44. if(![status isKindOfClass:[NSString class]]){
  45. taskUid = [[NSString alloc] initWithFormat:@"%@",taskUid];
  46. }
  47. if(taskUid && [taskUid isEqualToString:self.taskUid]){
  48. NSNumber * fileLengthNum = @0;
  49. if ([[data allKeys] containsObject:@"fileLength"])
  50. {
  51. fileLengthNum = data[@"fileLength"];
  52. }
  53. // [[uploadFileManager shareInstance] changeUploadFileState:uploadStateUploading withDidUploadBytes:[fileLengthNum integerValue] withModel:self.curUploadFileDataMod complete:^(BOOL isSuccess) {
  54. //
  55. // }];
  56. if([fileLengthNum integerValue] == self.curUploadFileDataMod.totalBytes && self.curUploadFileDataMod.totalBytes != 0){
  57. self.taskUid = nil;
  58. self.curUploadFileDataMod.curUploadStateType = uploadStateDone;
  59. NSMutableArray *delArr = [NSMutableArray new];
  60. [delArr addObject:self.curUploadFileDataMod];
  61. [[uploadFileManager shareInstance] deleteUploadFileRecordBy:delArr withDelCache:NO complete:^(BOOL isSuccess) {
  62. if (isSuccess) {
  63. }
  64. }];
  65. mainBlock(^{
  66. [[iToast makeText:NSLocalizedString(@"File_upload_file_already_exists",nil)] show];
  67. });
  68. HLog(@"hxd already 上传文件已存在")
  69. return;
  70. }
  71. self.curUploadPostion = [fileLengthNum longValue];
  72. if(self.curUploadFileDataMod.curUploadFileType == uploadFileTypeImage){
  73. [self beginGotoUploadImageFun:fileLengthNum];
  74. }
  75. else{
  76. [self beginGotoUploadVidelFun:fileLengthNum];
  77. }
  78. }
  79. }
  80. }
  81. }
  82. }
  83. else{
  84. //异常
  85. [self tryAgainApplyUploadFun];
  86. }
  87. }
  88. else{
  89. //异常
  90. [self tryAgainApplyUploadFun];
  91. }
  92. }
  93. #pragma mark 图片上传
  94. -(void)beginGotoUploadImageFun:(NSNumber*)fileLengthNum{
  95. self.cutFileDataArr = [self fileCutPartsBy:self.AllFileData withDidUpLoadSize:[fileLengthNum integerValue]];
  96. if(!self.cutFileDataArr || self.cutFileDataArr.count == 0){
  97. [self clearUploadFileDataFun];
  98. [[uploadFileManager shareInstance] uploadFileFailFun];
  99. HLog(@"hxd fail 上传任务异常终止")
  100. return;
  101. }
  102. //答复的taskUid 跟申请的一致 开发发送内容
  103. [self upLoadFileFun];
  104. }
  105. #pragma mark 视频上传
  106. //每次从文件重新切一片 传一片
  107. -(void)beginGotoUploadVidelFun:(NSNumber*)fileLengthNum{
  108. NSData *data = [self cutVideoFileFunAtIndex:fileLengthNum.longLongValue];
  109. HLog(@"data leng:%ld",[data length]);
  110. if([data length] == 0){
  111. [self clearUploadFileDataFun];
  112. [[uploadFileManager shareInstance] uploadFileFailFun];
  113. return;
  114. }
  115. self.AllFileData = data;
  116. self.cutFileDataArr = [self fileCutPartsBy:self.AllFileData withDidUpLoadSize:0];
  117. self.indexOfUploadFlie = 0;
  118. //return;
  119. // NSString*videoPathStr = [cachesFileManager getFilePathWithName:self.curUploadFileDataMod.filename type:uploadFileTypeVideo];
  120. // self.AllFileData = [NSData dataWithContentsOfFile:videoPathStr];
  121. // self.cutFileDataArr = [self fileCutPartsBy:self.AllFileData withDidUpLoadSize:[fileLengthNum integerValue]];
  122. if(!self.cutFileDataArr ||self.cutFileDataArr.count ==0){
  123. [self clearUploadFileDataFun];
  124. [[uploadFileManager shareInstance] uploadFileFailFun];
  125. HLog(@"hxd fail 上传任务异常终止")
  126. return;
  127. }
  128. //答复的taskUid 跟申请的一致 开发发送内容
  129. [self upLoadFileFun];
  130. }
  131. #pragma mark 分段读视频文件
  132. -(NSData*)cutVideoFileFunAtIndex:(NSUInteger)dataIndex{
  133. NSString *filePath = [cachesFileManager getFilePathWithName:self.curUploadFileDataMod.filename type:uploadFileTypeVideo]; // 文件路径
  134. NSFileManager *manager0 = [NSFileManager defaultManager];
  135. if(![manager0 fileExistsAtPath:filePath]) {
  136. return [NSData new];
  137. }
  138. NSFileHandle *fileHandle = [NSFileHandle fileHandleForReadingAtPath:filePath]; // 创建文件句柄
  139. // 设置分段读取的大小,这里以每次读取1KB为例
  140. //const NSUInteger chunkSize = 50 * 1024 *1024;
  141. const NSUInteger chunkSize = 5 * 1024 *1024;
  142. NSMutableData *data = [NSMutableData data];
  143. if (fileHandle) {
  144. long long endOfFile = [fileHandle seekToEndOfFile];
  145. if(self.curUploadFileDataMod.totalBytes == 0){//异常处理
  146. self.curUploadFileDataMod.totalBytes = endOfFile;
  147. }
  148. if (endOfFile >= chunkSize) {
  149. // 读取文件的分段数据到某个位置
  150. [fileHandle seekToFileOffset:dataIndex];
  151. // 读取文件的分段数据
  152. NSData* chunk = [fileHandle readDataOfLength:chunkSize];
  153. if (chunk) {
  154. [data appendData:chunk];
  155. }
  156. }
  157. else{
  158. // 读取文件的分段数据到某个位置
  159. [fileHandle seekToFileOffset:dataIndex];
  160. [data appendData:[fileHandle readDataToEndOfFile]];
  161. }
  162. // 在这里可以对文件内容进行处理
  163. // ...
  164. // 关闭文件句柄
  165. [fileHandle closeFile];
  166. }
  167. return data;
  168. }
  169. - (void)upLoadFileFunServiceResponseFun:(NSDictionary *)dataDict
  170. {
  171. //{"type":"uploadFileRandomRet","data":{"progress":"9\/10","filePath":"/sdcard/Download/IMG_0036.JPG","status":"0","msg":"success"}}
  172. if([dataDict isKindOfClass:[NSDictionary class]]){
  173. if ([[dataDict allKeys] containsObject:@"data"]) {
  174. NSDictionary *data = dataDict[@"data"];
  175. if ([[data allKeys] containsObject:@"status"]) {
  176. NSString *status = [data objectForKey:@"status"];
  177. if(![status isKindOfClass:[NSString class]]){
  178. status = [[NSString alloc] initWithFormat:@"%@",status];
  179. }
  180. if ([status isEqualToString:@"0"]){
  181. NSNumber * fileLengthNum = @0;
  182. if ([[data allKeys] containsObject:@"fileLenth"])
  183. {
  184. fileLengthNum = data[@"fileLenth"];
  185. }
  186. self.curUploadPostion = [fileLengthNum longValue];
  187. [self handleUploadingFun:fileLengthNum];
  188. }
  189. else if ([status isEqualToString:@"2"]) {
  190. [self clearUploadFileDataFun];
  191. [[uploadFileManager shareInstance] suspendUploadFileFun:YES];
  192. NSString *tipStr = NSLocalizedString(@"disk_not_find_tip",nil);
  193. mainBlock(^{
  194. [[iToast makeText:tipStr] show];
  195. });
  196. }
  197. else if ([status isEqualToString:@"5"]) {
  198. [self clearUploadFileDataFun];
  199. [[uploadFileManager shareInstance] suspendUploadFileFun:YES];
  200. NSString *tipStr = NSLocalizedString(@"disk_space_not_tip",nil);
  201. mainBlock(^{
  202. [[iToast makeText:tipStr] show];
  203. });
  204. }
  205. else //if ([status isEqualToString:@"1"])
  206. {
  207. //[self tryAgainApplyUploadFun];
  208. [self clearUploadFileDataFun];
  209. [[uploadFileManager shareInstance] uploadFileFailFun];
  210. }
  211. }
  212. }
  213. else{
  214. //异常
  215. [self tryAgainApplyUploadFun];
  216. }
  217. }
  218. else{
  219. //异常
  220. [self tryAgainApplyUploadFun];
  221. }
  222. }
  223. #pragma mark 申请上传文件
  224. - (void)ApplyUploadFileFun
  225. {
  226. //if(!self.taskUid){
  227. self.taskUid = [iTools getTaskUidStr];
  228. if([self.taskUid isEqualToString:self.downloadFileTaskUid]
  229. ||[self.taskUid isEqualToString:self.backupsTaskUid])
  230. {//重新生成taskUid
  231. [self ApplyUploadFileFun];
  232. return;
  233. }
  234. self.fileName = self.curUploadFileDataMod.filename;
  235. if(self.curUploadFileDataMod.curUploadFileType == uploadFileTypeVideo){
  236. //self.AllFileData = self.curUploadFileDataMod.videoData;
  237. }
  238. else{
  239. self.AllFileData = self.curUploadFileDataMod.imageData;
  240. }
  241. self.indexOfUploadFlie = 0;
  242. //HLog(@"%@ %@",self.taskUid,self.fileName);
  243. //self.cutFileDataArr = [self fileCutPartsBy:self.AllFileData];
  244. //}
  245. NSString * commandStr = [RCCommandHelp applyForUploadFileBy:self.taskUid filePath:self.fileName];
  246. [self.commandChannelManager rc_sendData:commandStr];
  247. }
  248. #pragma mark 遇到异常时 重新上传
  249. - (void)tryAgainApplyUploadFun
  250. {
  251. self.taskUid = nil;
  252. [self ApplyUploadFileFun];
  253. }
  254. #pragma mark 处理上传过程
  255. - (void)handleUploadingFun:(NSNumber*)fileLengthNum
  256. {
  257. //异常处理
  258. if(fileLengthNum.longValue < 0)
  259. {
  260. return;
  261. }
  262. BOOL isCanUseCellular = [HWDataManager getBoolWithKey:stringKeyAddSn(Const_file_Transfe_canUse_Cellular)];
  263. if(!isCanUseCellular){//不允许流量备份
  264. //
  265. if([AFNetworkReachabilityManager sharedManager].networkReachabilityStatus == AFNetworkReachabilityStatusReachableViaWWAN){
  266. self.isSuspendUploadType = YES;
  267. }
  268. }
  269. if(self.isSuspendUploadType){
  270. //暂停
  271. self.taskUid = nil;
  272. //NSInteger didUploadSize = (self.indexOfUploadFlie+1) * (self.cutFileEachPieceSzie);
  273. NSInteger didUploadSize = fileLengthNum.longLongValue;
  274. [[uploadFileManager shareInstance] changeUploadFileState:uploadStateSuspend withDidUploadBytes:didUploadSize withModel:self.curUploadFileDataMod complete:^(BOOL isSuccess) {
  275. }];
  276. return;
  277. }
  278. if(self.indexOfUploadFlie < self.cutFileDataArr.count -1) {
  279. //NSInteger didUploadSize = (self.indexOfUploadFlie+1) * (self.cutFileEachPieceSzie);
  280. NSInteger didUploadSize = fileLengthNum.longLongValue;
  281. [[uploadFileManager shareInstance] changeUploadFileState:uploadStateUploading withDidUploadBytes:didUploadSize withModel:self.curUploadFileDataMod complete:^(BOOL isSuccess) {
  282. }];
  283. self.indexOfUploadFlie ++;
  284. [self upLoadFileFun];
  285. }
  286. else if(self.curUploadFileDataMod.curUploadFileType == uploadFileTypeVideo && fileLengthNum && fileLengthNum.longLongValue < self.curUploadFileDataMod.totalBytes){
  287. //视频做了二次切割 不一定上传完了
  288. HLog(@"视频上传传完一片 再来一片");
  289. [self beginGotoUploadVidelFun:fileLengthNum];
  290. }
  291. else{
  292. HLog(@"上传完成");
  293. [self clearUploadFileDataFun];
  294. [[uploadFileManager shareInstance] uploadFileDoneFun];
  295. }
  296. }
  297. -(void)clearUploadFileDataFun{
  298. //上传数据清空
  299. self.taskUid = nil;
  300. self.fileName = nil;
  301. self.AllFileData = nil;
  302. self.indexOfUploadFlie = 0;
  303. self.cutFileDataArr = nil;
  304. self.curUploadFileDataMod = nil;
  305. self.isSuspendUploadType = NO;
  306. }
  307. - (Byte)getBCC:(NSData *)data {
  308. Byte bcc = 0;
  309. unsigned char *bytes = (unsigned char *)[data bytes];
  310. for (int i = 0; i < [data length]; i++) {
  311. bcc = (Byte)(bcc ^ bytes[i]);
  312. }
  313. return bcc;
  314. }
  315. #pragma mark 文件切片
  316. -(NSMutableArray *)fileCutPartsBy:(NSData*)curData withDidUpLoadSize:(NSInteger)didLoadSize{
  317. NSData * data = curData;
  318. NSUInteger allLength = data.length;
  319. NSUInteger subs = EachPieceSzie;//1*1024*1024;//4096;//要切片的大小,我这里设置的是4096字节
  320. //NSUInteger subs = 31*1024;//4096;//要切片的大小,内存没有暴增
  321. self.cutFileEachPieceSzie = subs;
  322. NSInteger index = 0;//起始位置
  323. if(didLoadSize != 0){
  324. if (didLoadSize % subs != 0){
  325. //出错了
  326. return nil;
  327. }
  328. else{
  329. // index = didLoadSize/subs;
  330. // allLength = allLength - didLoadSize;
  331. self.indexOfUploadFlie = didLoadSize/subs;
  332. }
  333. }
  334. NSMutableArray *dataArray =[NSMutableArray new];
  335. do {
  336. //@autoreleasepool {
  337. if (allLength>subs) {
  338. NSRange range =NSMakeRange(index*subs, subs);
  339. index++;
  340. //NSLog(@"%@",NSStringFromRange(range));
  341. [dataArray addObject:[data subdataWithRange:range]];
  342. allLength = allLength - subs;
  343. }else{
  344. NSRange range = NSMakeRange(index*subs, allLength);
  345. //NSLog(@"%@",NSStringFromRange(range));
  346. NSData *subdata = [data subdataWithRange:range];
  347. if(subdata){
  348. [dataArray addObject:subdata];
  349. }
  350. allLength = 0;
  351. }
  352. //}
  353. } while (allLength>0);
  354. HLog(@"dataArray leng:%ld",dataArray.count);//最后得到切片的结果,数组里面是NSData对象
  355. return dataArray;
  356. }
  357. // int转NSData
  358. - (NSData *)int2Data:(int)i{
  359. Byte b0 = i & 0xff;
  360. Byte b1 = i >> 8 & 0xff;
  361. Byte b2 = i >> 16 & 0xff;
  362. Byte b3 = i >> 24 & 0xff;
  363. // 有大小端模式问题?
  364. //Byte result[] = {b0, b1, b2, b3};
  365. Byte result[] = {b3, b2, b1, b0};
  366. return [NSData dataWithBytes:result length:sizeof(result)];
  367. }
  368. // long转NSData
  369. - (NSData *)long2Data:(long)i{
  370. Byte b0 = i & 0xff;
  371. Byte b1 = i >> 8 & 0xff;
  372. Byte b2 = i >> 16 & 0xff;
  373. Byte b3 = i >> 24 & 0xff;
  374. Byte b4 = i >> 32 & 0xff;
  375. Byte b5 = i >> 40 & 0xff;
  376. Byte b6 = i >> 48 & 0xff;
  377. Byte b7 = i >> 56 & 0xff;
  378. // 有大小端模式问题?
  379. //Byte result[] = {b0, b1, b2, b3};
  380. //Byte result[] = {b3, b2, b1, b0};
  381. Byte result[] = {b7, b6, b5,b4,b3, b2, b1, b0};
  382. return [NSData dataWithBytes:result length:sizeof(result)];
  383. }
  384. // short转NSData
  385. - (NSData *)short2Data:(short)i{
  386. Byte b0 = i & 0xff;
  387. Byte b1 = i >> 8 & 0xff;
  388. // 有大小端模式问题?
  389. //Byte result[] = {b0, b1, b2, b3};
  390. Byte result[] = { b1, b0};
  391. return [NSData dataWithBytes:result length:sizeof(result)];
  392. }
  393. #pragma mark 可以发送文件了
  394. /*
  395. 文件数据字节流byte[]
  396. 格式1:下载日志文件压缩包
  397. |head|msgType|taskId| datalen | curNum | totalNum| data |bcc校验|end|
  398. 长度: 1 1 6 (curNum+totalNum+data) 4 4 datalen- 8 1 1
  399. 0x68 0x0e 0x16
  400. bcc校验从第1位 也就是从msgType起 到 校验位前(不包含校验位)数据作bcc校验
  401. */
  402. - (void)upLoadFileFun
  403. {
  404. //HLog(@"上传文件%ld/%ld",self.indexOfUploadFlie +1,self.cutFileDataArr.count);
  405. if(self.cutFileDataArr.count <= self.indexOfUploadFlie)
  406. {
  407. return;
  408. }
  409. //data
  410. NSData *curData = self.cutFileDataArr[self.indexOfUploadFlie];
  411. //HLog(@"headData:%@",curData);
  412. //head
  413. unsigned char headNum = 104;
  414. NSData *headData = [NSMutableData dataWithBytes:&headNum length:sizeof(headNum)];
  415. //HLog(@"headData:%@",headData);
  416. //msgType
  417. //unsigned char msgType = 12;
  418. //unsigned char msgType = 15;
  419. unsigned char msgType = 20;
  420. NSData *msgTypeData = [NSMutableData dataWithBytes:&msgType length:sizeof(msgType)];
  421. //HLog(@"msgTypeData:%@",msgTypeData);
  422. //taskId
  423. NSData *taskIdData = [self.taskUid dataUsingEncoding:NSUTF8StringEncoding];
  424. //HLog(@"taskIdData:%@",taskIdData);
  425. //datalen
  426. //int datalenNum = int([curData length] +(4+4));
  427. int datalenNum = [curData length] + (4+4);
  428. //NSData *datalenData = [NSData dataWithBytes:&datalenNum length:sizeof(datalenNum)];
  429. NSData *datalenData = [self int2Data:datalenNum];
  430. //HLog(@"datalenData %@",datalenData);
  431. //curNum
  432. int curNum = (int)self.indexOfUploadFlie ;//+ 1;
  433. //NSData *curNumData = [NSData dataWithBytes:&curNum length:sizeof(curNum)];
  434. NSData * curNumData = [self int2Data:curNum];
  435. //HLog(@"curNumData:%@",curNumData);
  436. //totalNum
  437. int totalNum = (int)self.cutFileDataArr.count;
  438. //NSData *totalNumData = [NSData dataWithBytes:&totalNum length:sizeof(totalNum)];
  439. NSData * totalNumData = [self int2Data:totalNum];
  440. //HLog(@"totalNumData:%@",totalNumData);
  441. long Postion = self.curUploadPostion;
  442. //long Postion = 100000000000;
  443. NSData * PostionData = [self long2Data:Postion];
  444. HLog(@"PostionData:%@",PostionData);
  445. NSMutableData *checkBCCData = [NSMutableData new];
  446. [checkBCCData appendData:msgTypeData];
  447. [checkBCCData appendData:taskIdData];
  448. [checkBCCData appendData:datalenData];
  449. [checkBCCData appendData:curNumData];
  450. [checkBCCData appendData:totalNumData];
  451. [checkBCCData appendData:PostionData];
  452. [checkBCCData appendData:curData];
  453. long length = [curData length];
  454. //HLog(@"curData length %d",length);
  455. Byte bccCheck = [self getBCC:checkBCCData];
  456. //HLog(@"bcc 000 %d",bccCheck);
  457. //bcc校验
  458. char bccNum = (char)bccCheck;
  459. NSData *bccData = [NSMutableData dataWithBytes:&bccNum length:sizeof(bccNum)];
  460. //HLog(@"%@",bccData);
  461. //end
  462. unsigned char endNum = 22;
  463. NSData *endData = [NSMutableData dataWithBytes:&endNum length:sizeof(endNum)];
  464. //HLog(@"%@",endData);
  465. NSMutableData *sendData = [NSMutableData new];
  466. [sendData appendData:headData];
  467. [sendData appendData:checkBCCData];
  468. [sendData appendData:bccData];
  469. [sendData appendData:endData];
  470. //HLog(@"sendData length %ld",[sendData length]);
  471. [self.commandChannelManager rc_sendData:sendData];
  472. //HLog(@"文件上传sendData %@",sendData);
  473. }
  474. - (void)suspendUploadFileFun:(NSNotification *)notification
  475. {
  476. self.isSuspendUploadType = YES;
  477. }
  478. @end