PlayerViewController+downloadNasFile.m 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. //
  2. // PlayerViewController+downloadNasFile.m
  3. // 双子星云手机
  4. //
  5. // Created by xd h on 2024/5/31.
  6. //
  7. #import "PlayerViewController+downloadNasFile.h"
  8. @implementation PlayerViewController (downloadNasFile)
  9. #pragma mark 下载完成
  10. - (void)downloadTaskFinishedNoti:(NSNotification *)notification
  11. {
  12. if(self.nasFullPath && self.nasFullPath.length >0){
  13. HLog(@"前面一个保存流程没走完")
  14. return;
  15. }
  16. curDownloadmodel = notification.object;
  17. KWeakSelf
  18. if (curDownloadmodel.downloadState == customDownloadStateCompleted) {
  19. mainBlock(^{
  20. [weakSelf handldDownloadDoneToSaveBy:self->curDownloadmodel];
  21. });
  22. }
  23. }
  24. #pragma mark 下载完后处理保持流程
  25. - (void)handldDownloadDoneToSaveBy:(customDownloadOperation*)model{
  26. //解码
  27. NSString * urlString = [model.url stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  28. NSArray *nameArr= [urlString componentsSeparatedByString:@"."];
  29. NSString * pathStr= model.fullPath;
  30. NSString * decodePathStr= [pathStr stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  31. HLog(@"downloadNasFile:%@\n%@\n%@",urlString,pathStr,[pathStr stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding])
  32. if(![pathStr isEqualToString:decodePathStr]){
  33. // 尝试移动(即重命名)文件
  34. NSError *error = nil;
  35. NSFileManager *fileManager = [NSFileManager defaultManager];
  36. BOOL success = [fileManager moveItemAtPath:pathStr toPath:decodePathStr error:&error];
  37. if (success) {
  38. NSLog(@"文件重命名成功!");
  39. } else {
  40. NSLog(@"文件重命名失败: %@", error);
  41. }
  42. pathStr = decodePathStr;
  43. }
  44. HLog(@"%@,",pathStr);
  45. if (nameArr.count >= 2) {
  46. NSString *lastName = nameArr.lastObject;
  47. lastName = [lastName lowercaseString];
  48. if([iTools canSaveFileToAlbumByPhoto:YES withName:lastName])
  49. {//可以保持到相册
  50. UIImage *image = [UIImage imageWithContentsOfFile:pathStr];
  51. if(image){
  52. [self loadImageFinished:image with:pathStr];
  53. }
  54. }
  55. else if([iTools canSaveFileToAlbumByPhoto:NO withName:lastName]){//可以保持到相册
  56. [self loadVideoFinishedBy:pathStr];
  57. }
  58. else{//保存到文件
  59. [self loadOtherDataFinishedBy:pathStr];
  60. }
  61. }
  62. }
  63. - (void)loadImageFinished:(UIImage *)image with:(NSString*)fullPath
  64. {
  65. [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
  66. //写入图片到相册
  67. PHAssetChangeRequest *req = [PHAssetChangeRequest creationRequestForAssetFromImage:image];
  68. } completionHandler:^(BOOL success, NSError * _Nullable error) {
  69. //NSLog(@"success = %d, error = %@", success, error);
  70. if (success) {
  71. HLog(@"已将图片保存至相册");
  72. //
  73. [[NSFileManager defaultManager] removeItemAtPath:fullPath error:nil];
  74. // if(self->curYCDownloadItem){
  75. // [YCDownloadManager stopDownloadWithItem:self->curYCDownloadItem];
  76. // self->curYCDownloadItem = nil;
  77. // }
  78. [[boxDownloadFileManager shareInstance] deleteDownloadDonePlistInfoBy:fullPath];
  79. } else {
  80. HLog(@"未能将图片保存至相册");
  81. mainBlock(^{
  82. [self loadOtherDataFinishedBy:fullPath];
  83. });
  84. }
  85. }];
  86. }
  87. - (void)loadVideoFinishedBy:(NSString*)fullPath
  88. {
  89. NSString*pathStr = fullPath;
  90. PHPhotoLibrary *photoLibrary = [PHPhotoLibrary sharedPhotoLibrary];
  91. [photoLibrary performChanges:^{
  92. [PHAssetChangeRequest creationRequestForAssetFromVideoAtFileURL:[NSURL
  93. fileURLWithPath:pathStr]];
  94. } completionHandler:^(BOOL success, NSError * _Nullable error) {
  95. if (success) {
  96. HLog(@"已将视频保存至相册");
  97. //
  98. [[NSFileManager defaultManager] removeItemAtPath:pathStr error:nil];
  99. // if(self->curYCDownloadItem){
  100. // [YCDownloadManager stopDownloadWithItem:self->curYCDownloadItem];
  101. // self->curYCDownloadItem = nil;
  102. // }
  103. [[boxDownloadFileManager shareInstance] deleteDownloadDonePlistInfoBy:fullPath];
  104. } else {
  105. HLog(@"未能将视频保存至相册");
  106. mainBlock(^{
  107. [self loadOtherDataFinishedBy:fullPath];
  108. });
  109. }
  110. }];
  111. }
  112. //下载音频 文件等
  113. - (void)loadOtherDataFinishedBy:(NSString*)fullPath
  114. {
  115. NSString*filePath = fullPath;
  116. self.nasFullPath = fullPath;
  117. if (![[NSFileManager defaultManager] fileExistsAtPath:filePath]){
  118. HLog(@"没有找到文件:%@",filePath);
  119. return;
  120. }
  121. NSURL * fileURL = [NSURL fileURLWithPath:filePath];
  122. UIDocumentPickerViewController *documentPickerVC = [[UIDocumentPickerViewController alloc] initWithURL:fileURL inMode:UIDocumentPickerModeExportToService];
  123. // 设置代理
  124. documentPickerVC.delegate = self;
  125. // 设置模态弹出方式
  126. documentPickerVC.modalPresentationStyle = UIModalPresentationFormSheet;
  127. [self.navigationController presentViewController:documentPickerVC animated:YES completion:nil];
  128. }
  129. //同文件 有了代理 走了 downloadFile 里面的 UIDocumentPickerDelegate
  130. #pragma mark - UIDocumentPickerDelegate
  131. //- (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentsAtURLs:(NSArray<NSURL *> *)urls {
  132. // // 获取授权
  133. // BOOL fileUrlAuthozied = [urls.firstObject startAccessingSecurityScopedResource];
  134. // if (fileUrlAuthozied) {
  135. // // 通过文件协调工具来得到新的文件地址,以此得到文件保护功能
  136. // NSFileCoordinator *fileCoordinator = [[NSFileCoordinator alloc] init];
  137. // NSError *error;
  138. //
  139. // NSURL * firstUrl = urls.firstObject;
  140. // [fileCoordinator coordinateReadingItemAtURL:urls.firstObject options:0 error:&error byAccessor:^(NSURL *newURL) {
  141. // // 读取文件
  142. // NSString *fileName = [firstUrl lastPathComponent];
  143. // NSString *pathStr = [firstUrl absoluteString];
  144. // NSError *error = nil;
  145. // //NSData *fileData = [NSData dataWithContentsOfURL:newURL options:NSDataReadingMappedIfSafe error:&error];
  146. // if (error) {
  147. // // 读取出错
  148. // } else {
  149. // // 上传
  150. // NSLog(@"111fileName : %@ -- %@ ---%@", fileName,pathStr,self.nasFullPath);
  151. //
  152. // if(!pathStr || [pathStr isEqualToString:self.nasFullPath]){
  153. // pathStr = self.nasFullPath;
  154. // }
  155. //
  156. // //
  157. // [[NSFileManager defaultManager] removeItemAtPath:pathStr error:nil];
  158. //// if(self->curYCDownloadItem){
  159. //// [YCDownloadManager stopDownloadWithItem:self->curYCDownloadItem];
  160. //// self->curYCDownloadItem = nil;
  161. //// }
  162. //
  163. // [[boxDownloadFileManager shareInstance] deleteDownloadDonePlistInfoBy:pathStr];
  164. //
  165. // }
  166. // }];
  167. // [urls.firstObject stopAccessingSecurityScopedResource];
  168. // } else {
  169. // // 授权失败
  170. // }
  171. //}
  172. @end