webSocketManager+downloadNasFile.m 7.6 KB

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