// // webRtcManager+downloadNasFile.m // Private-X // // Created by xd h on 2024/9/13. // #import "webRtcManager+downloadNasFile.h" @implementation webRtcManager (downloadNasFile) #pragma mark 下载完成 - (void)NasDownloadTaskFinishedNoti:(customDownloadOperation *)nasDownloadOperation { // if((self.nasFullPath && self.nasFullPath.length >0) // || self.isShowingFileDocumentPickerType){ // HLog(@"上一个任务保存到问题---没保存完"); // return; // } curDownloadmodel = nasDownloadOperation; KWeakSelf if (curDownloadmodel.downloadState == customDownloadStateCompleted) { mainBlock(^{ [weakSelf handldDownloadDoneToSaveBy:self->curDownloadmodel]; }); } } #pragma mark 下载完后处理保持流程 - (void)handldDownloadDoneToSaveBy:(customDownloadOperation*)model{ //解码 NSString * urlString = [model.url stringByRemovingPercentEncoding]; //[model.url stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; NSArray *nameArr= [urlString componentsSeparatedByString:@"."]; NSString * pathStr= [customDownloadCacheManager getFullPathByOldFullPath:model.fullPath]; NSString * decodePathStr= [pathStr stringByRemovingPercentEncoding]; HLog(@"downloadNasFile:%@\n%@\n%@",urlString,pathStr,[pathStr stringByRemovingPercentEncoding]) if(![pathStr isEqualToString:decodePathStr]){ // 尝试移动(即重命名)文件 NSError *error = nil; NSFileManager *fileManager = [NSFileManager defaultManager]; BOOL success = [fileManager moveItemAtPath:pathStr toPath:decodePathStr error:&error]; if (success) { NSLog(@"文件重命名成功!"); } else { NSLog(@"文件重命名失败: %@", error); } pathStr = decodePathStr; } HLog(@"%@,",pathStr); if (nameArr.count >= 2) { NSString *lastName = nameArr.lastObject; lastName = [lastName lowercaseString]; if([iTools canSaveFileToAlbumByPhoto:YES withName:lastName]) {//可以保持到相册 UIImage *image = [UIImage imageWithContentsOfFile:pathStr]; if(image){ [self loadImageFinished:image with:pathStr]; } } else if([iTools canSaveFileToAlbumByPhoto:NO withName:lastName]){//可以保持到相册 [self loadVideoFinishedBy:pathStr]; } else{//保存到文件 if((self.nasFullPath && self.nasFullPath.length >0) || self.isShowingFileDocumentPickerType){ HLog(@"上一个任务保存到问题---没保存完"); return; } [self loadOtherDataFinishedBy:pathStr]; } } } - (void)loadImageFinished:(UIImage *)image with:(NSString*)fullPath { [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{ //写入图片到相册 PHAssetChangeRequest *req = [PHAssetChangeRequest creationRequestForAssetFromImage:image]; } completionHandler:^(BOOL success, NSError * _Nullable error) { //NSLog(@"success = %d, error = %@", success, error); if (success) { HLog(@"已将图片保存至相册"); // [[NSFileManager defaultManager] removeItemAtPath:fullPath error:nil]; // if(self->curYCDownloadItem){ // [YCDownloadManager stopDownloadWithItem:self->curYCDownloadItem]; // self->curYCDownloadItem = nil; // } [[boxDownloadFileManager shareInstance] deleteDownloadDonePlistInfoBy:fullPath]; } else { HLog(@"未能将图片保存至相册"); mainBlock(^{ [self loadOtherDataFinishedBy:fullPath]; }); } }]; } - (void)loadVideoFinishedBy:(NSString*)fullPath { NSString*pathStr = fullPath; PHPhotoLibrary *photoLibrary = [PHPhotoLibrary sharedPhotoLibrary]; [photoLibrary performChanges:^{ [PHAssetChangeRequest creationRequestForAssetFromVideoAtFileURL:[NSURL fileURLWithPath:pathStr]]; } completionHandler:^(BOOL success, NSError * _Nullable error) { if (success) { HLog(@"已将视频保存至相册"); // [[NSFileManager defaultManager] removeItemAtPath:pathStr error:nil]; // if(self->curYCDownloadItem){ // [YCDownloadManager stopDownloadWithItem:self->curYCDownloadItem]; // self->curYCDownloadItem = nil; // } [[boxDownloadFileManager shareInstance] deleteDownloadDonePlistInfoBy:fullPath]; } else { HLog(@"未能将视频保存至相册"); mainBlock(^{ [self loadOtherDataFinishedBy:fullPath]; }); } }]; } //下载音频 文件等 - (void)loadOtherDataFinishedBy:(NSString*)fullPath { NSString*filePath = fullPath; self.nasFullPath = fullPath; if (![[NSFileManager defaultManager] fileExistsAtPath:filePath]){ HLog(@"没有找到文件:%@",filePath); return; } NSURL * fileURL = [NSURL fileURLWithPath:filePath]; UIDocumentPickerViewController *documentPickerVC = [[UIDocumentPickerViewController alloc] initWithURL:fileURL inMode:UIDocumentPickerModeExportToService]; // 设置代理 documentPickerVC.delegate = self; // 设置模态弹出方式 documentPickerVC.modalPresentationStyle = UIModalPresentationFormSheet; //[self.navigationController presentViewController:documentPickerVC animated:YES completion:nil]; [ksharedAppDelegate.window.rootViewController presentViewController:documentPickerVC animated:YES completion:nil]; self.isShowingFileDocumentPickerType = YES; } //同文件 有了代理 走了 downloadFile 里面的 UIDocumentPickerDelegate #pragma mark - UIDocumentPickerDelegate - (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentsAtURLs:(NSArray *)urls { // 获取授权 BOOL fileUrlAuthozied = [urls.firstObject startAccessingSecurityScopedResource]; if (fileUrlAuthozied) { // 通过文件协调工具来得到新的文件地址,以此得到文件保护功能 NSFileCoordinator *fileCoordinator = [[NSFileCoordinator alloc] init]; NSError *error; [fileCoordinator coordinateReadingItemAtURL:urls.firstObject options:0 error:&error byAccessor:^(NSURL *newURL) { // 读取文件 NSString *fileName = [newURL lastPathComponent]; NSError *error = nil; //NSData *fileData = [NSData dataWithContentsOfURL:newURL options:NSDataReadingMappedIfSafe error:&error]; if (error) { // 读取出错 if(self.nasFullPath){ self.nasFullPath = nil; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(20 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [self checkSaveNasFileAgainFun]; }); } else{ //self.wsDownloadSaveFilePath = nil; //self.wsDownloadDonwPlishPath = nil; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(20 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [self checkSaveWebsocketFileAgainFun]; }); } } else { NSLog(@"222 fileName : %@", fileName); if(!self.nasFullPath){ // websock下载流程 //[[NSFileManager defaultManager] removeItemAtPath:self.wsDownloadSaveFilePath error:nil]; //[cachesFileManager removeItemAtPath:fileName type:DownLoadFileType error:nil]; //[[downloadManager shareInstance] deleteDownloadDonePlistInfoBy:self.wsDownloadDonwPlishPath]; //self.wsDownloadSaveFilePath = nil; //self.wsDownloadDonwPlishPath = nil; [self checkSaveWebsocketFileAgainFun]; } else{ //nas下载流程 [[NSFileManager defaultManager] removeItemAtPath:self.nasFullPath error:nil]; [[boxDownloadFileManager shareInstance] deleteDownloadDonePlistInfoBy:self.nasFullPath]; [[nasDownloadFileManager shareInstance] deleteDownloadDonePlistInfoBy:self.nasFullPath]; self.nasFullPath = nil; [self checkSaveNasFileAgainFun]; } } self.isShowingFileDocumentPickerType = NO; }]; [urls.firstObject stopAccessingSecurityScopedResource]; } else { // 授权失败 if(self.nasFullPath){ self.nasFullPath = nil; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(20 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [self checkSaveNasFileAgainFun]; }); } else{ //self.wsDownloadSaveFilePath = nil; //self.wsDownloadDonwPlishPath = nil; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(20 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [self checkSaveWebsocketFileAgainFun]; }); } self.isShowingFileDocumentPickerType = NO; } } #pragma mark 再次读取下载完成数据做保存操作 - (void)checkSaveNasFileAgainFun { [[boxDownloadFileManager shareInstance] checkDownloadDonePlistInfoFun]; [[nasDownloadFileManager shareInstance] checkDownloadDonePlistInfoFun]; } #pragma mark 再次读取ws下载完成数据做保存操作 - (void)checkSaveWebsocketFileAgainFun { [[downloadManager shareInstance] checkDownloadDonePlistInfoFun]; } @end