webRtcManager+downloadNasFile.m 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. if((self.nasFullPath && self.nasFullPath.length >0)
  62. || self.isShowingFileDocumentPickerType){
  63. HLog(@"上一个任务保存到问题---没保存完");
  64. return;
  65. }
  66. [self loadOtherDataFinishedBy:pathStr];
  67. }
  68. }
  69. }
  70. - (void)loadImageFinished:(UIImage *)image with:(NSString*)fullPath
  71. {
  72. [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
  73. //写入图片到相册
  74. PHAssetChangeRequest *req = [PHAssetChangeRequest creationRequestForAssetFromImage:image];
  75. } completionHandler:^(BOOL success, NSError * _Nullable error) {
  76. //NSLog(@"success = %d, error = %@", success, error);
  77. if (success) {
  78. HLog(@"已将图片保存至相册");
  79. //
  80. [[NSFileManager defaultManager] removeItemAtPath:fullPath error:nil];
  81. // if(self->curYCDownloadItem){
  82. // [YCDownloadManager stopDownloadWithItem:self->curYCDownloadItem];
  83. // self->curYCDownloadItem = nil;
  84. // }
  85. [[boxDownloadFileManager shareInstance] deleteDownloadDonePlistInfoBy:fullPath];
  86. } else {
  87. HLog(@"未能将图片保存至相册");
  88. mainBlock(^{
  89. [self loadOtherDataFinishedBy:fullPath];
  90. });
  91. }
  92. }];
  93. }
  94. - (void)loadVideoFinishedBy:(NSString*)fullPath
  95. {
  96. NSString*pathStr = fullPath;
  97. PHPhotoLibrary *photoLibrary = [PHPhotoLibrary sharedPhotoLibrary];
  98. [photoLibrary performChanges:^{
  99. [PHAssetChangeRequest creationRequestForAssetFromVideoAtFileURL:[NSURL
  100. fileURLWithPath:pathStr]];
  101. } completionHandler:^(BOOL success, NSError * _Nullable error) {
  102. if (success) {
  103. HLog(@"已将视频保存至相册");
  104. //
  105. [[NSFileManager defaultManager] removeItemAtPath:pathStr error:nil];
  106. // if(self->curYCDownloadItem){
  107. // [YCDownloadManager stopDownloadWithItem:self->curYCDownloadItem];
  108. // self->curYCDownloadItem = nil;
  109. // }
  110. [[boxDownloadFileManager shareInstance] deleteDownloadDonePlistInfoBy:fullPath];
  111. } else {
  112. HLog(@"未能将视频保存至相册");
  113. mainBlock(^{
  114. [self loadOtherDataFinishedBy:fullPath];
  115. });
  116. }
  117. }];
  118. }
  119. //下载音频 文件等
  120. - (void)loadOtherDataFinishedBy:(NSString*)fullPath
  121. {
  122. NSString*filePath = fullPath;
  123. self.nasFullPath = fullPath;
  124. if (![[NSFileManager defaultManager] fileExistsAtPath:filePath]){
  125. HLog(@"没有找到文件:%@",filePath);
  126. return;
  127. }
  128. NSURL * fileURL = [NSURL fileURLWithPath:filePath];
  129. UIDocumentPickerViewController *documentPickerVC = [[UIDocumentPickerViewController alloc] initWithURL:fileURL inMode:UIDocumentPickerModeExportToService];
  130. // 设置代理
  131. documentPickerVC.delegate = self;
  132. // 设置模态弹出方式
  133. documentPickerVC.modalPresentationStyle = UIModalPresentationFormSheet;
  134. //[self.navigationController presentViewController:documentPickerVC animated:YES completion:nil];
  135. [ksharedAppDelegate.window.rootViewController presentViewController:documentPickerVC animated:YES completion:nil];
  136. self.isShowingFileDocumentPickerType = YES;
  137. }
  138. //同文件 有了代理 走了 downloadFile 里面的 UIDocumentPickerDelegate
  139. #pragma mark - UIDocumentPickerDelegate
  140. - (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentsAtURLs:(NSArray<NSURL *> *)urls {
  141. // 获取授权
  142. BOOL fileUrlAuthozied = [urls.firstObject startAccessingSecurityScopedResource];
  143. if (fileUrlAuthozied) {
  144. // 通过文件协调工具来得到新的文件地址,以此得到文件保护功能
  145. NSFileCoordinator *fileCoordinator = [[NSFileCoordinator alloc] init];
  146. NSError *error;
  147. [fileCoordinator coordinateReadingItemAtURL:urls.firstObject options:0 error:&error byAccessor:^(NSURL *newURL) {
  148. // 读取文件
  149. NSString *fileName = [newURL lastPathComponent];
  150. NSError *error = nil;
  151. //NSData *fileData = [NSData dataWithContentsOfURL:newURL options:NSDataReadingMappedIfSafe error:&error];
  152. if (error) {
  153. // 读取出错
  154. if(self.nasFullPath){
  155. self.nasFullPath = nil;
  156. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(20 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  157. [self checkSaveNasFileAgainFun];
  158. });
  159. }
  160. else{
  161. //self.wsDownloadSaveFilePath = nil;
  162. //self.wsDownloadDonwPlishPath = nil;
  163. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(20 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  164. [self checkSaveWebsocketFileAgainFun];
  165. });
  166. }
  167. } else {
  168. NSLog(@"222 fileName : %@", fileName);
  169. if(!self.nasFullPath){
  170. // websock下载流程
  171. //[[NSFileManager defaultManager] removeItemAtPath:self.wsDownloadSaveFilePath error:nil];
  172. //[cachesFileManager removeItemAtPath:fileName type:DownLoadFileType error:nil];
  173. //[[downloadManager shareInstance] deleteDownloadDonePlistInfoBy:self.wsDownloadDonwPlishPath];
  174. //self.wsDownloadSaveFilePath = nil;
  175. //self.wsDownloadDonwPlishPath = nil;
  176. [self checkSaveWebsocketFileAgainFun];
  177. }
  178. else{
  179. //nas下载流程
  180. [[NSFileManager defaultManager] removeItemAtPath:self.nasFullPath error:nil];
  181. [[boxDownloadFileManager shareInstance] deleteDownloadDonePlistInfoBy:self.nasFullPath];
  182. [[nasDownloadFileManager shareInstance] deleteDownloadDonePlistInfoBy:self.nasFullPath];
  183. self.nasFullPath = nil;
  184. [self checkSaveNasFileAgainFun];
  185. }
  186. }
  187. self.isShowingFileDocumentPickerType = NO;
  188. }];
  189. [urls.firstObject stopAccessingSecurityScopedResource];
  190. } else {
  191. // 授权失败
  192. if(self.nasFullPath){
  193. self.nasFullPath = nil;
  194. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(20 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  195. [self checkSaveNasFileAgainFun];
  196. });
  197. }
  198. else{
  199. //self.wsDownloadSaveFilePath = nil;
  200. //self.wsDownloadDonwPlishPath = nil;
  201. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(20 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  202. [self checkSaveWebsocketFileAgainFun];
  203. });
  204. }
  205. self.isShowingFileDocumentPickerType = NO;
  206. }
  207. }
  208. #pragma mark 再次读取下载完成数据做保存操作
  209. - (void)checkSaveNasFileAgainFun
  210. {
  211. [[boxDownloadFileManager shareInstance] checkDownloadDonePlistInfoFun];
  212. [[nasDownloadFileManager shareInstance] checkDownloadDonePlistInfoFun];
  213. }
  214. #pragma mark 再次读取ws下载完成数据做保存操作
  215. - (void)checkSaveWebsocketFileAgainFun
  216. {
  217. [[downloadManager shareInstance] checkDownloadDonePlistInfoFun];
  218. }
  219. @end