PlayerViewController+downloadNasFile.m 6.5 KB

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