PlayerViewController+downloadFile.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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 withModel:nil];
  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. NSString *fileName = [self.curDownloadFileModel getFileNameFun];
  106. NSString*pathStr = [cachesFileManager getFilePathWithName:fileName type:DownLoadFileType];
  107. if([self.curDownloadFileModel.fileType isEqualToString:@".jpg"]){
  108. if([iTools canSaveFileToAlbumByPhoto:YES withName:fileName]){
  109. UIImage *image = [UIImage imageWithContentsOfFile:pathStr];
  110. if(image){
  111. [self loadImageFinished:image];
  112. }
  113. else{
  114. [cachesFileManager removeItemAtPath:fileName type:DownLoadFileType error:nil];
  115. [[downloadManager shareInstance] DownloadFileFailOneFileFun];
  116. }
  117. }
  118. else{
  119. [self loadOtherDataFinished];
  120. }
  121. }
  122. else if([self.curDownloadFileModel.fileType isEqualToString:@"video"]){
  123. if([iTools canSaveFileToAlbumByPhoto:NO withName:fileName]){
  124. [self loadVideoFinished];
  125. }
  126. else{
  127. [self loadOtherDataFinished];
  128. }
  129. }
  130. else
  131. {
  132. [self loadOtherDataFinished];
  133. }
  134. }
  135. - (void)loadImageFinished:(UIImage *)image
  136. {
  137. [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
  138. //写入图片到相册
  139. PHAssetChangeRequest *req = [PHAssetChangeRequest creationRequestForAssetFromImage:image];
  140. } completionHandler:^(BOOL success, NSError * _Nullable error) {
  141. //NSLog(@"success = %d, error = %@", success, error);
  142. if (success) {
  143. HLog(@"已将图片保存至相册");
  144. //NSString *fileName = [self.curDownloadFileModel getFileNameFun];
  145. //[cachesFileManager removeItemAtPath:fileName type:DownLoadFileType error:nil];
  146. } else {
  147. HLog(@"未能将图片保存至相册");
  148. // mainBlock(^{
  149. // [[iToast makeText:NSLocalizedString(@"File_Transfer_By_Cellular_tip",nil)] show];
  150. // });
  151. }
  152. NSString *fileName = [self.curDownloadFileModel getFileNameFun];
  153. [cachesFileManager removeItemAtPath:fileName type:DownLoadFileType error:nil];
  154. [[downloadManager shareInstance] DownloadFileDoneOneFileFun];
  155. }];
  156. }
  157. - (void)loadVideoFinished
  158. {
  159. NSString *fileName = [self.curDownloadFileModel getFileNameFun];
  160. NSString*pathStr = [cachesFileManager getFilePathWithName:fileName type:DownLoadFileType];
  161. PHPhotoLibrary *photoLibrary = [PHPhotoLibrary sharedPhotoLibrary];
  162. [photoLibrary performChanges:^{
  163. [PHAssetChangeRequest creationRequestForAssetFromVideoAtFileURL:[NSURL
  164. fileURLWithPath:pathStr]];
  165. } completionHandler:^(BOOL success, NSError * _Nullable error) {
  166. if (success) {
  167. HLog(@"已将视频保存至相册");
  168. //NSString *fileName = [self.curDownloadFileModel getFileNameFun];
  169. //[cachesFileManager removeItemAtPath:fileName type:DownLoadFileType error:nil];
  170. } else {
  171. HLog(@"未能将视频保存至相册");
  172. }
  173. [cachesFileManager removeItemAtPath:fileName type:DownLoadFileType error:nil];
  174. [[downloadManager shareInstance] DownloadFileDoneOneFileFun];
  175. }];
  176. }
  177. //下载音频 文件等
  178. - (void)loadOtherDataFinished
  179. {
  180. [[downloadManager shareInstance] DownloadFileDoneOneFileFun];
  181. NSString *fileName = [self.curDownloadFileModel getFileNameFun];
  182. NSString*filePath = [cachesFileManager getFilePathWithName:fileName type:DownLoadFileType];
  183. if (![[NSFileManager defaultManager] fileExistsAtPath:filePath]){
  184. HLog(@"没有找到文件:%@",fileName);
  185. return;
  186. }
  187. NSURL * fileURL = [NSURL fileURLWithPath:filePath];
  188. UIDocumentPickerViewController *documentPickerVC = [[UIDocumentPickerViewController alloc] initWithURL:fileURL inMode:UIDocumentPickerModeExportToService];
  189. // 设置代理
  190. documentPickerVC.delegate = self;
  191. // 设置模态弹出方式
  192. documentPickerVC.modalPresentationStyle = UIModalPresentationFormSheet;
  193. [self.navigationController presentViewController:documentPickerVC animated:YES completion:nil];
  194. }
  195. #pragma mark - UIDocumentPickerDelegate
  196. - (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentsAtURLs:(NSArray<NSURL *> *)urls {
  197. // 获取授权
  198. BOOL fileUrlAuthozied = [urls.firstObject startAccessingSecurityScopedResource];
  199. if (fileUrlAuthozied) {
  200. // 通过文件协调工具来得到新的文件地址,以此得到文件保护功能
  201. NSFileCoordinator *fileCoordinator = [[NSFileCoordinator alloc] init];
  202. NSError *error;
  203. [fileCoordinator coordinateReadingItemAtURL:urls.firstObject options:0 error:&error byAccessor:^(NSURL *newURL) {
  204. // 读取文件
  205. NSString *fileName = [newURL lastPathComponent];
  206. NSError *error = nil;
  207. //NSData *fileData = [NSData dataWithContentsOfURL:newURL options:NSDataReadingMappedIfSafe error:&error];
  208. if (error) {
  209. // 读取出错
  210. if(self.nasFullPath){
  211. self.nasFullPath = nil;
  212. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(20 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  213. [self checkSaveNasFileAgainFun];
  214. });
  215. }
  216. } else {
  217. NSLog(@"222 fileName : %@", fileName);
  218. if(!self.nasFullPath){
  219. // websock下载流程
  220. [cachesFileManager removeItemAtPath:fileName type:DownLoadFileType error:nil];
  221. }
  222. else{
  223. //nas下载流程
  224. [[NSFileManager defaultManager] removeItemAtPath:self.nasFullPath error:nil];
  225. [[boxDownloadFileManager shareInstance] deleteDownloadDonePlistInfoBy:self.nasFullPath];
  226. self.nasFullPath = nil;
  227. [self checkSaveNasFileAgainFun];
  228. }
  229. }
  230. }];
  231. [urls.firstObject stopAccessingSecurityScopedResource];
  232. } else {
  233. // 授权失败
  234. if(self.nasFullPath){
  235. self.nasFullPath = nil;
  236. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(20 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  237. [self checkSaveNasFileAgainFun];
  238. });
  239. }
  240. }
  241. }
  242. #pragma mark 再次读取下载完成数据做保存操作
  243. - (void)checkSaveNasFileAgainFun
  244. {
  245. [[boxDownloadFileManager shareInstance] checkDownloadDonePlistInfoFun];
  246. }
  247. @end