webSocketManager+downloadNasFile.m 7.6 KB

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