123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 |
- //
- // PlayerViewController+downloadNasFile.m
- // 双子星云手机
- //
- // Created by xd h on 2024/5/31.
- //
- #import "PlayerViewController+downloadNasFile.h"
- @implementation PlayerViewController (downloadNasFile)
- #pragma mark 下载完成
- - (void)downloadTaskFinishedNoti:(NSNotification *)notification
- {
- curYCDownloadItem = notification.object;
- KWeakSelf
- if (curYCDownloadItem.downloadStatus == YCDownloadStatusFinished) {
- mainBlock(^{
- [weakSelf handldDownloadDoneToSaveBy:self->curYCDownloadItem];
- });
- }
- }
- #pragma mark 下载完后处理保持流程
- - (void)handldDownloadDoneToSaveBy:(YCDownloadItem*)item{
- //解码
- NSString * urlString = [item.downloadURL stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
- NSArray *nameArr= [urlString componentsSeparatedByString:@"."];
- NSString * pathStr= item.savePath;
-
- NSString *addFileName = @"";
- if (![item.downloadURL containsString:@".quicktime"]
- && [pathStr containsString:@".quicktime"])
- {
- addFileName = @".quicktime";
- }
- else if (![item.downloadURL containsString:@".octet-stream"]
- && [pathStr containsString:@".octet-stream"])
- {
- addFileName = @".octet-stream";
- }
-
- if(addFileName.length >0)
- {
- //文件名被添加了 .quicktime
-
- NSError *error = nil;
- NSFileManager *fileManager = [NSFileManager defaultManager];
- // 新文件名路径
- NSString *newFilePath = [pathStr stringByReplacingOccurrencesOfString:addFileName withString:@""];
- // 尝试移动(即重命名)文件
- BOOL success = [fileManager moveItemAtPath:pathStr toPath:newFilePath error:&error];
-
- if (success) {
- pathStr = newFilePath;
- NSLog(@"文件重命名成功!");
- } else {
- NSLog(@"文件重命名失败: %@", error);
- }
- }
-
- 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<NSURL *> *)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
|