webSocketManager+downloadNasFile.m 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. HLog(@"前面一个保存流程没走完")
  14. return;
  15. }
  16. curDownloadmodel = nasDownloadOperation;
  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. [ksharedAppDelegate.window.rootViewController presentViewController:documentPickerVC animated:YES completion:nil];
  129. }
  130. //同文件 有了代理 走了 downloadFile 里面的 UIDocumentPickerDelegate
  131. #pragma mark - UIDocumentPickerDelegate
  132. //- (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentsAtURLs:(NSArray<NSURL *> *)urls {
  133. // // 获取授权
  134. // BOOL fileUrlAuthozied = [urls.firstObject startAccessingSecurityScopedResource];
  135. // if (fileUrlAuthozied) {
  136. // // 通过文件协调工具来得到新的文件地址,以此得到文件保护功能
  137. // NSFileCoordinator *fileCoordinator = [[NSFileCoordinator alloc] init];
  138. // NSError *error;
  139. //
  140. // NSURL * firstUrl = urls.firstObject;
  141. // [fileCoordinator coordinateReadingItemAtURL:urls.firstObject options:0 error:&error byAccessor:^(NSURL *newURL) {
  142. // // 读取文件
  143. // NSString *fileName = [firstUrl lastPathComponent];
  144. // NSString *pathStr = [firstUrl absoluteString];
  145. // NSError *error = nil;
  146. // //NSData *fileData = [NSData dataWithContentsOfURL:newURL options:NSDataReadingMappedIfSafe error:&error];
  147. // if (error) {
  148. // // 读取出错
  149. // } else {
  150. // // 上传
  151. // NSLog(@"111fileName : %@ -- %@ ---%@", fileName,pathStr,self.nasFullPath);
  152. //
  153. // if(!pathStr || [pathStr isEqualToString:self.nasFullPath]){
  154. // pathStr = self.nasFullPath;
  155. // }
  156. //
  157. // //
  158. // [[NSFileManager defaultManager] removeItemAtPath:pathStr error:nil];
  159. //// if(self->curYCDownloadItem){
  160. //// [YCDownloadManager stopDownloadWithItem:self->curYCDownloadItem];
  161. //// self->curYCDownloadItem = nil;
  162. //// }
  163. //
  164. // [[boxDownloadFileManager shareInstance] deleteDownloadDonePlistInfoBy:pathStr];
  165. //
  166. // }
  167. // }];
  168. // [urls.firstObject stopAccessingSecurityScopedResource];
  169. // } else {
  170. // // 授权失败
  171. // }
  172. //}
  173. @end