PlayerViewController+downloadFile.m 6.7 KB

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