PlayerViewController+downloadFile.m 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. //
  2. // PlayerViewController+downloadFile.m
  3. // 隐私保护
  4. //
  5. // Created by xd h on 2024/1/9.
  6. //
  7. #import "PlayerViewController+downloadFile.h"
  8. #import "cachesFileManager.h"
  9. #import "downloadManager.h"
  10. @implementation PlayerViewController (downloadFile)
  11. - (void)begindownloadFileFun:(NSNotification *)notification
  12. {
  13. self.curDownloadFileModel = [notification object];
  14. if(!self.curDownloadFileModel
  15. || ![self.curDownloadFileModel isKindOfClass:[couldPhoneFileModel class]]){
  16. return;
  17. }
  18. [self ApplyDownloadFileFun];
  19. }
  20. #pragma mark 申请下载缩略图
  21. - (void)ApplyDownloadFileFun
  22. {
  23. self.downloadFileTaskUid = [iTools getTaskUidStr];
  24. if([self.downloadFileTaskUid isEqualToString:self.backupsTaskUid]
  25. ||[self.downloadFileTaskUid isEqualToString:self.taskUid]
  26. ||[self.downloadFileTaskUid isEqualToString:self.downloadThumbnailTaskUid])
  27. {//重新生成taskUid
  28. [self ApplyDownloadFileFun];
  29. return;
  30. }
  31. NSString *fileName = [self.curDownloadFileModel getFileNameFun];
  32. NSString*pathStr = [cachesFileManager getFilePathWithName:fileName type:DownLoadFileType];
  33. long didDownloadBytes = 0;
  34. NSFileManager *manager0 = [NSFileManager defaultManager];
  35. if([manager0 fileExistsAtPath:pathStr]) {
  36. NSFileHandle *fileHandle = [NSFileHandle fileHandleForReadingAtPath:pathStr]; // 创建文件句柄
  37. if (fileHandle) {
  38. didDownloadBytes = [fileHandle seekToEndOfFile];
  39. // 关闭文件句柄
  40. [fileHandle closeFile];
  41. }
  42. }
  43. NSString * commandStr = [RCCommandHelp applyForDownloadFileBy:self.downloadFileTaskUid withFilePath:self.curDownloadFileModel.path withPosition:didDownloadBytes];
  44. [self.commandChannelManager rc_sendData:commandStr];
  45. }
  46. - (void)downloadFileFunServiceResponseFun:(NSData *)fileData with:(BOOL)isDownloadDone
  47. {
  48. if(isDownloadDone){
  49. [self handleDownloadDoneFun];
  50. return;
  51. }
  52. NSString *fileName = [self.curDownloadFileModel getFileNameFun];
  53. NSString*pathStr = [cachesFileManager getFilePathWithName:fileName type:DownLoadFileType];
  54. NSFileManager *manager0 = [NSFileManager defaultManager];
  55. long long endOfFile = 0;
  56. if(![manager0 fileExistsAtPath:pathStr]) {
  57. [cachesFileManager getFileNameWithContent:fileData fileName:fileName type:DownLoadFileType];
  58. }
  59. else{
  60. NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:pathStr]; // 创建文件句柄
  61. if (fileHandle) {
  62. endOfFile = [fileHandle seekToEndOfFile];
  63. if (endOfFile >= 0) {
  64. if(endOfFile == self.curDownloadFileModel.didDownloadBytes){
  65. [fileHandle writeData:fileData];
  66. }
  67. }
  68. endOfFile = [fileHandle seekToEndOfFile];
  69. // 关闭文件句柄
  70. [fileHandle closeFile];
  71. }
  72. }
  73. long didDownloadBytes = fileData.length;
  74. if(endOfFile > 0){
  75. didDownloadBytes = endOfFile;
  76. }
  77. self.curDownloadFileModel.didDownloadBytes = didDownloadBytes;
  78. // if(isDownloadDone || endOfFile == self.curDownloadFileModel.length){
  79. // [self handleDownloadDoneFun];
  80. // }
  81. // else
  82. {//获取下一片
  83. [[downloadManager shareInstance] DownloadFileChangeingOneFileFun];
  84. if([downloadManager shareInstance].isSuspendType){
  85. return;
  86. }
  87. BOOL isCanUseCellular = [HWDataManager getBoolWithKey:stringKeyAddSn(Const_file_Transfe_canUse_Cellular_all)];
  88. if(!isCanUseCellular){//不允许流量上传
  89. //
  90. if([AFNetworkReachabilityManager sharedManager].networkReachabilityStatus == AFNetworkReachabilityStatusReachableViaWWAN){
  91. [[downloadManager shareInstance] suspendDownloadFileFun:YES];
  92. mainBlock(^{
  93. [[NSNotificationCenter defaultCenter] postNotificationName:downloadFileSuspendAllNotification object:nil];
  94. [[iToast makeText:NSLocalizedString(@"File_Transfer_By_Cellular_tip",nil)] show];
  95. });
  96. return;
  97. }
  98. }
  99. NSString * commandStr = [RCCommandHelp applyForDownloadFileBy:self.downloadFileTaskUid withFilePath:self.curDownloadFileModel.path withPosition:didDownloadBytes];
  100. [self.commandChannelManager rc_sendData:commandStr];
  101. }
  102. }
  103. - (void)handleDownloadDoneFun
  104. {
  105. if([self.curDownloadFileModel.fileType isEqualToString:@".jpg"]){
  106. NSString *fileName = [self.curDownloadFileModel getFileNameFun];
  107. NSString*pathStr = [cachesFileManager getFilePathWithName:fileName type:DownLoadFileType];
  108. UIImage *image = [UIImage imageWithContentsOfFile:pathStr];
  109. if(image){
  110. [self loadImageFinished:image];
  111. }
  112. else{
  113. [cachesFileManager removeItemAtPath:fileName type:DownLoadFileType error:nil];
  114. [[downloadManager shareInstance] DownloadFileFailOneFileFun];
  115. }
  116. }
  117. else if([self.curDownloadFileModel.fileType isEqualToString:@"video"]){
  118. [self loadVideoFinished];
  119. }
  120. }
  121. - (void)loadImageFinished:(UIImage *)image
  122. {
  123. [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
  124. //写入图片到相册
  125. PHAssetChangeRequest *req = [PHAssetChangeRequest creationRequestForAssetFromImage:image];
  126. } completionHandler:^(BOOL success, NSError * _Nullable error) {
  127. //NSLog(@"success = %d, error = %@", success, error);
  128. if (success) {
  129. HLog(@"已将图片保存至相册");
  130. //NSString *fileName = [self.curDownloadFileModel getFileNameFun];
  131. //[cachesFileManager removeItemAtPath:fileName type:DownLoadFileType error:nil];
  132. } else {
  133. HLog(@"未能将图片保存至相册");
  134. // mainBlock(^{
  135. // [[iToast makeText:NSLocalizedString(@"File_Transfer_By_Cellular_tip",nil)] show];
  136. // });
  137. }
  138. NSString *fileName = [self.curDownloadFileModel getFileNameFun];
  139. [cachesFileManager removeItemAtPath:fileName type:DownLoadFileType error:nil];
  140. [[downloadManager shareInstance] DownloadFileDoneOneFileFun];
  141. }];
  142. }
  143. - (void)loadVideoFinished
  144. {
  145. NSString *fileName = [self.curDownloadFileModel getFileNameFun];
  146. NSString*pathStr = [cachesFileManager getFilePathWithName:fileName type:DownLoadFileType];
  147. PHPhotoLibrary *photoLibrary = [PHPhotoLibrary sharedPhotoLibrary];
  148. [photoLibrary performChanges:^{
  149. [PHAssetChangeRequest creationRequestForAssetFromVideoAtFileURL:[NSURL
  150. fileURLWithPath:pathStr]];
  151. } completionHandler:^(BOOL success, NSError * _Nullable error) {
  152. if (success) {
  153. HLog(@"已将视频保存至相册");
  154. //NSString *fileName = [self.curDownloadFileModel getFileNameFun];
  155. //[cachesFileManager removeItemAtPath:fileName type:DownLoadFileType error:nil];
  156. } else {
  157. HLog(@"未能将视频保存至相册");
  158. }
  159. [cachesFileManager removeItemAtPath:fileName type:DownLoadFileType error:nil];
  160. [[downloadManager shareInstance] DownloadFileDoneOneFileFun];
  161. }];
  162. }
  163. @end