PlayerViewController+downloadNasFile.m 6.8 KB

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