// // webSocketManager+downloadNasFile.m // 双子星云手机 // // Created by xd h on 2024/7/10. // #import "webSocketManager+downloadNasFile.h" @implementation webSocketManager (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{//保存到文件 [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; // // NSURL * firstUrl = urls.firstObject; // [fileCoordinator coordinateReadingItemAtURL:urls.firstObject options:0 error:&error byAccessor:^(NSURL *newURL) { // // 读取文件 // NSString *fileName = [firstUrl lastPathComponent]; // NSString *pathStr = [firstUrl absoluteString]; // NSError *error = nil; // //NSData *fileData = [NSData dataWithContentsOfURL:newURL options:NSDataReadingMappedIfSafe error:&error]; // if (error) { // // 读取出错 // } else { // // 上传 // NSLog(@"111fileName : %@ -- %@ ---%@", fileName,pathStr,self.nasFullPath); // // if(!pathStr || [pathStr isEqualToString:self.nasFullPath]){ // pathStr = self.nasFullPath; // } // // // // [[NSFileManager defaultManager] removeItemAtPath:pathStr error:nil]; //// if(self->curYCDownloadItem){ //// [YCDownloadManager stopDownloadWithItem:self->curYCDownloadItem]; //// self->curYCDownloadItem = nil; //// } // // [[boxDownloadFileManager shareInstance] deleteDownloadDonePlistInfoBy:pathStr]; // // } // }]; // [urls.firstObject stopAccessingSecurityScopedResource]; // } else { // // 授权失败 // } //} @end