webRtcManager+downloadNasFile.m 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. //
  2. // webRtcManager+downloadNasFile.m
  3. // 双子星云手机
  4. //
  5. // Created by xd h on 2024/9/13.
  6. //
  7. #import "webRtcManager+downloadNasFile.h"
  8. @implementation webRtcManager (downloadNasFile)
  9. #pragma mark 下载完成
  10. - (void)NasDownloadTaskFinishedNoti:(customDownloadOperation *)nasDownloadOperation
  11. {
  12. if((self.nasFullPath && self.nasFullPath.length >0)
  13. || self.isShowingFileDocumentPickerType){
  14. HLog(@"上一个任务保存到问题---没保存完");
  15. return;
  16. }
  17. curDownloadmodel = nasDownloadOperation;
  18. KWeakSelf
  19. if (curDownloadmodel.downloadState == customDownloadStateCompleted) {
  20. mainBlock(^{
  21. [weakSelf handldDownloadDoneToSaveBy:self->curDownloadmodel];
  22. });
  23. }
  24. }
  25. #pragma mark 下载完后处理保持流程
  26. - (void)handldDownloadDoneToSaveBy:(customDownloadOperation*)model{
  27. //解码
  28. NSString * urlString = [model.url stringByRemovingPercentEncoding];
  29. //[model.url stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  30. NSArray *nameArr= [urlString componentsSeparatedByString:@"."];
  31. NSString * pathStr= [customDownloadCacheManager getFullPathByOldFullPath:model.fullPath];
  32. NSString * decodePathStr= [pathStr stringByRemovingPercentEncoding];
  33. HLog(@"downloadNasFile:%@\n%@\n%@",urlString,pathStr,[pathStr stringByRemovingPercentEncoding])
  34. if(![pathStr isEqualToString:decodePathStr]){
  35. // 尝试移动(即重命名)文件
  36. NSError *error = nil;
  37. NSFileManager *fileManager = [NSFileManager defaultManager];
  38. BOOL success = [fileManager moveItemAtPath:pathStr toPath:decodePathStr error:&error];
  39. if (success) {
  40. NSLog(@"文件重命名成功!");
  41. } else {
  42. NSLog(@"文件重命名失败: %@", error);
  43. }
  44. pathStr = decodePathStr;
  45. }
  46. HLog(@"%@,",pathStr);
  47. if (nameArr.count >= 2) {
  48. NSString *lastName = nameArr.lastObject;
  49. lastName = [lastName lowercaseString];
  50. if([iTools canSaveFileToAlbumByPhoto:YES withName:lastName])
  51. {//可以保持到相册
  52. UIImage *image = [UIImage imageWithContentsOfFile:pathStr];
  53. if(image){
  54. [self loadImageFinished:image with:pathStr];
  55. }
  56. }
  57. else if([iTools canSaveFileToAlbumByPhoto:NO withName:lastName]){//可以保持到相册
  58. [self loadVideoFinishedBy:pathStr];
  59. }
  60. else{//保存到文件
  61. [self loadOtherDataFinishedBy:pathStr];
  62. }
  63. }
  64. }
  65. - (void)loadImageFinished:(UIImage *)image with:(NSString*)fullPath
  66. {
  67. [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
  68. //写入图片到相册
  69. PHAssetChangeRequest *req = [PHAssetChangeRequest creationRequestForAssetFromImage:image];
  70. } completionHandler:^(BOOL success, NSError * _Nullable error) {
  71. //NSLog(@"success = %d, error = %@", success, error);
  72. if (success) {
  73. HLog(@"已将图片保存至相册");
  74. //
  75. [[NSFileManager defaultManager] removeItemAtPath:fullPath error:nil];
  76. // if(self->curYCDownloadItem){
  77. // [YCDownloadManager stopDownloadWithItem:self->curYCDownloadItem];
  78. // self->curYCDownloadItem = nil;
  79. // }
  80. [[boxDownloadFileManager shareInstance] deleteDownloadDonePlistInfoBy:fullPath];
  81. } else {
  82. HLog(@"未能将图片保存至相册");
  83. mainBlock(^{
  84. [self loadOtherDataFinishedBy:fullPath];
  85. });
  86. }
  87. }];
  88. }
  89. - (void)loadVideoFinishedBy:(NSString*)fullPath
  90. {
  91. NSString*pathStr = fullPath;
  92. PHPhotoLibrary *photoLibrary = [PHPhotoLibrary sharedPhotoLibrary];
  93. [photoLibrary performChanges:^{
  94. [PHAssetChangeRequest creationRequestForAssetFromVideoAtFileURL:[NSURL
  95. fileURLWithPath:pathStr]];
  96. } completionHandler:^(BOOL success, NSError * _Nullable error) {
  97. if (success) {
  98. HLog(@"已将视频保存至相册");
  99. //
  100. [[NSFileManager defaultManager] removeItemAtPath:pathStr error:nil];
  101. // if(self->curYCDownloadItem){
  102. // [YCDownloadManager stopDownloadWithItem:self->curYCDownloadItem];
  103. // self->curYCDownloadItem = nil;
  104. // }
  105. [[boxDownloadFileManager shareInstance] deleteDownloadDonePlistInfoBy:fullPath];
  106. } else {
  107. HLog(@"未能将视频保存至相册");
  108. mainBlock(^{
  109. [self loadOtherDataFinishedBy:fullPath];
  110. });
  111. }
  112. }];
  113. }
  114. //下载音频 文件等
  115. - (void)loadOtherDataFinishedBy:(NSString*)fullPath
  116. {
  117. NSString*filePath = fullPath;
  118. self.nasFullPath = fullPath;
  119. if (![[NSFileManager defaultManager] fileExistsAtPath:filePath]){
  120. HLog(@"没有找到文件:%@",filePath);
  121. return;
  122. }
  123. NSURL * fileURL = [NSURL fileURLWithPath:filePath];
  124. UIDocumentPickerViewController *documentPickerVC = [[UIDocumentPickerViewController alloc] initWithURL:fileURL inMode:UIDocumentPickerModeExportToService];
  125. // 设置代理
  126. documentPickerVC.delegate = self;
  127. // 设置模态弹出方式
  128. documentPickerVC.modalPresentationStyle = UIModalPresentationFormSheet;
  129. //[self.navigationController presentViewController:documentPickerVC animated:YES completion:nil];
  130. [ksharedAppDelegate.window.rootViewController presentViewController:documentPickerVC animated:YES completion:nil];
  131. self.isShowingFileDocumentPickerType = YES;
  132. }
  133. //同文件 有了代理 走了 downloadFile 里面的 UIDocumentPickerDelegate
  134. #pragma mark - UIDocumentPickerDelegate
  135. - (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentsAtURLs:(NSArray<NSURL *> *)urls {
  136. // 获取授权
  137. BOOL fileUrlAuthozied = [urls.firstObject startAccessingSecurityScopedResource];
  138. if (fileUrlAuthozied) {
  139. // 通过文件协调工具来得到新的文件地址,以此得到文件保护功能
  140. NSFileCoordinator *fileCoordinator = [[NSFileCoordinator alloc] init];
  141. NSError *error;
  142. [fileCoordinator coordinateReadingItemAtURL:urls.firstObject options:0 error:&error byAccessor:^(NSURL *newURL) {
  143. // 读取文件
  144. NSString *fileName = [newURL lastPathComponent];
  145. NSError *error = nil;
  146. //NSData *fileData = [NSData dataWithContentsOfURL:newURL options:NSDataReadingMappedIfSafe error:&error];
  147. if (error) {
  148. // 读取出错
  149. if(self.nasFullPath){
  150. self.nasFullPath = nil;
  151. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(20 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  152. [self checkSaveNasFileAgainFun];
  153. });
  154. }
  155. else{
  156. //self.wsDownloadSaveFilePath = nil;
  157. //self.wsDownloadDonwPlishPath = nil;
  158. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(20 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  159. [self checkSaveWebsocketFileAgainFun];
  160. });
  161. }
  162. } else {
  163. NSLog(@"222 fileName : %@", fileName);
  164. if(!self.nasFullPath){
  165. // websock下载流程
  166. //[[NSFileManager defaultManager] removeItemAtPath:self.wsDownloadSaveFilePath error:nil];
  167. //[cachesFileManager removeItemAtPath:fileName type:DownLoadFileType error:nil];
  168. //[[downloadManager shareInstance] deleteDownloadDonePlistInfoBy:self.wsDownloadDonwPlishPath];
  169. //self.wsDownloadSaveFilePath = nil;
  170. //self.wsDownloadDonwPlishPath = nil;
  171. [self checkSaveWebsocketFileAgainFun];
  172. }
  173. else{
  174. //nas下载流程
  175. [[NSFileManager defaultManager] removeItemAtPath:self.nasFullPath error:nil];
  176. [[boxDownloadFileManager shareInstance] deleteDownloadDonePlistInfoBy:self.nasFullPath];
  177. [[nasDownloadFileManager shareInstance] deleteDownloadDonePlistInfoBy:self.nasFullPath];
  178. self.nasFullPath = nil;
  179. [self checkSaveNasFileAgainFun];
  180. }
  181. }
  182. self.isShowingFileDocumentPickerType = NO;
  183. }];
  184. [urls.firstObject stopAccessingSecurityScopedResource];
  185. } else {
  186. // 授权失败
  187. if(self.nasFullPath){
  188. self.nasFullPath = nil;
  189. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(20 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  190. [self checkSaveNasFileAgainFun];
  191. });
  192. }
  193. else{
  194. //self.wsDownloadSaveFilePath = nil;
  195. //self.wsDownloadDonwPlishPath = nil;
  196. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(20 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  197. [self checkSaveWebsocketFileAgainFun];
  198. });
  199. }
  200. self.isShowingFileDocumentPickerType = NO;
  201. }
  202. }
  203. #pragma mark 再次读取下载完成数据做保存操作
  204. - (void)checkSaveNasFileAgainFun
  205. {
  206. [[boxDownloadFileManager shareInstance] checkDownloadDonePlistInfoFun];
  207. [[nasDownloadFileManager shareInstance] checkDownloadDonePlistInfoFun];
  208. }
  209. #pragma mark 再次读取ws下载完成数据做保存操作
  210. - (void)checkSaveWebsocketFileAgainFun
  211. {
  212. [[downloadManager shareInstance] checkDownloadDonePlistInfoFun];
  213. }
  214. @end