// // PlayerViewController+downloadNasFile.m // 双子星云手机 // // Created by xd h on 2024/5/31. // #import "PlayerViewController+downloadNasFile.h" @implementation PlayerViewController (downloadNasFile) #pragma mark 下载完成 - (void)downloadTaskFinishedNoti:(NSNotification *)notification { curDownloadmodel = notification.object; KWeakSelf if (curDownloadmodel.downloadState == DownloadStateCompleted) { mainBlock(^{ [weakSelf handldDownloadDoneToSaveBy:self->curDownloadmodel]; }); } } #pragma mark 下载完后处理保持流程 - (void)handldDownloadDoneToSaveBy:(mixDownloadOperation*)model{ //解码 NSString * urlString = [model.url stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; NSArray *nameArr= [urlString componentsSeparatedByString:@"."]; NSString * pathStr= model.fullPath; NSString * decodePathStr= [pathStr stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; HLog(@"%@\n%@\n%@",urlString,pathStr,[pathStr stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]) 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; // } } 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; // } } else { HLog(@"未能将视频保存至相册"); mainBlock(^{ [self loadOtherDataFinishedBy:fullPath]; }); } }]; } //下载音频 文件等 - (void)loadOtherDataFinishedBy:(NSString*)fullPath { NSString*filePath = 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]; } #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]; NSString *pathStr = [newURL absoluteString]; NSError *error = nil; //NSData *fileData = [NSData dataWithContentsOfURL:newURL options:NSDataReadingMappedIfSafe error:&error]; if (error) { // 读取出错 } else { // 上传 NSLog(@"fileName : %@-- %@", fileName,pathStr); // [[NSFileManager defaultManager] removeItemAtPath:pathStr error:nil]; // if(self->curYCDownloadItem){ // [YCDownloadManager stopDownloadWithItem:self->curYCDownloadItem]; // self->curYCDownloadItem = nil; // } } }]; [urls.firstObject stopAccessingSecurityScopedResource]; } else { // 授权失败 } } @end