webSocketManager+downloadFile.m 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. //
  2. // webSocketManager+downloadFile.m
  3. // 双子星云手机
  4. //
  5. // Created by xd h on 2024/6/20.
  6. //
  7. #import "webSocketManager+downloadFile.h"
  8. #import "cachesFileManager.h"
  9. #import "downloadManager.h"
  10. @implementation webSocketManager (downloadFile)
  11. - (void)begindownloadFileFun:(NSNotification *)notification
  12. {
  13. self.curDownloadFileModel = [notification object];
  14. if(!self.curDownloadFileModel
  15. || ![self.curDownloadFileModel isKindOfClass:[couldPhoneFileModel class]]){
  16. return;
  17. }
  18. [self ApplyDownloadFileFun];
  19. }
  20. #pragma mark 申请下载缩略图
  21. - (void)ApplyDownloadFileFun
  22. {
  23. self.downloadFileTaskUid = [iTools getTaskUidStr];
  24. if([self.downloadFileTaskUid isEqualToString:self.backupsTaskUid]
  25. ||[self.downloadFileTaskUid isEqualToString:self.taskUid]
  26. ||[self.downloadFileTaskUid isEqualToString:self.downloadThumbnailTaskUid])
  27. {//重新生成taskUid
  28. [self ApplyDownloadFileFun];
  29. return;
  30. }
  31. NSString *fileName = [self.curDownloadFileModel getFileNameFun];
  32. NSString*pathStr = [cachesFileManager getFilePathWithName:fileName type:DownLoadFileType];
  33. long didDownloadBytes = 0;
  34. NSFileManager *manager0 = [NSFileManager defaultManager];
  35. if([manager0 fileExistsAtPath:pathStr]) {
  36. NSFileHandle *fileHandle = [NSFileHandle fileHandleForReadingAtPath:pathStr]; // 创建文件句柄
  37. if (fileHandle) {
  38. didDownloadBytes = [fileHandle seekToEndOfFile];
  39. // 关闭文件句柄
  40. [fileHandle closeFile];
  41. }
  42. }
  43. NSString * commandStr = [RCCommandHelp applyForDownloadFileBy:self.downloadFileTaskUid withFilePath:self.curDownloadFileModel.path withPosition:didDownloadBytes];
  44. [self.commandChannelManager rc_sendData:commandStr];
  45. }
  46. - (void)downloadFileFunServiceResponseFun:(NSData *)fileData with:(BOOL)isDownloadDone
  47. {
  48. if(isDownloadDone){
  49. [self handleDownloadDoneFun];
  50. return;
  51. }
  52. NSString *fileName = [self.curDownloadFileModel getFileNameFun];
  53. NSString*pathStr = [cachesFileManager getFilePathWithName:fileName type:DownLoadFileType];
  54. NSFileManager *manager0 = [NSFileManager defaultManager];
  55. long long endOfFile = 0;
  56. if(![manager0 fileExistsAtPath:pathStr]) {
  57. [cachesFileManager getFileNameWithContent:fileData fileName:fileName type:DownLoadFileType];
  58. }
  59. else{
  60. NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:pathStr]; // 创建文件句柄
  61. if (fileHandle) {
  62. endOfFile = [fileHandle seekToEndOfFile];
  63. if (endOfFile >= 0) {
  64. if(endOfFile == self.curDownloadFileModel.didDownloadBytes){
  65. [fileHandle writeData:fileData];
  66. }
  67. }
  68. endOfFile = [fileHandle seekToEndOfFile];
  69. // 关闭文件句柄
  70. [fileHandle closeFile];
  71. }
  72. }
  73. long didDownloadBytes = fileData.length;
  74. if(endOfFile > 0){
  75. didDownloadBytes = endOfFile;
  76. }
  77. self.curDownloadFileModel.didDownloadBytes = didDownloadBytes;
  78. // if(isDownloadDone || endOfFile == self.curDownloadFileModel.length){
  79. // [self handleDownloadDoneFun];
  80. // }
  81. // else
  82. {//获取下一片
  83. [[downloadManager shareInstance] DownloadFileChangeingOneFileFun];
  84. if([downloadManager shareInstance].isSuspendType){
  85. return;
  86. }
  87. BOOL isCanUseCellular = [HWDataManager getBoolWithKey:stringKeyAddSn(Const_file_Transfe_canUse_Cellular_all)];
  88. if(!isCanUseCellular){//不允许流量上传
  89. //
  90. if([AFNetworkReachabilityManager sharedManager].networkReachabilityStatus == AFNetworkReachabilityStatusReachableViaWWAN){
  91. [[downloadManager shareInstance] suspendDownloadFileFun:YES withModel:nil];
  92. mainBlock(^{
  93. [[NSNotificationCenter defaultCenter] postNotificationName:downloadFileSuspendAllNotification object:nil];
  94. [[iToast makeText:NSLocalizedString(@"File_Transfer_By_Cellular_tip",nil)] show];
  95. });
  96. return;
  97. }
  98. }
  99. NSString * commandStr = [RCCommandHelp applyForDownloadFileBy:self.downloadFileTaskUid withFilePath:self.curDownloadFileModel.path withPosition:didDownloadBytes];
  100. [self.commandChannelManager rc_sendData:commandStr];
  101. }
  102. }
  103. - (void)handleDownloadDoneFun
  104. {
  105. NSString *fileName = [self.curDownloadFileModel getFileNameFun];
  106. NSString*pathStr = [cachesFileManager getFilePathWithName:fileName type:DownLoadFileType];
  107. if([self.curDownloadFileModel.fileType isEqualToString:@".jpg"]){
  108. if([iTools canSaveFileToAlbumByPhoto:YES withName:fileName]){
  109. UIImage *image = [UIImage imageWithContentsOfFile:pathStr];
  110. if(image){
  111. [self loadImageFinished:image];
  112. }
  113. else{
  114. [cachesFileManager removeItemAtPath:fileName type:DownLoadFileType error:nil];
  115. [[downloadManager shareInstance] DownloadFileFailOneFileFun];
  116. }
  117. }
  118. else{
  119. [self loadOtherDataFinished];
  120. }
  121. }
  122. else if([self.curDownloadFileModel.fileType isEqualToString:@"video"]){
  123. if([iTools canSaveFileToAlbumByPhoto:NO withName:fileName]){
  124. [self loadVideoFinished];
  125. }
  126. else{
  127. [self loadOtherDataFinished];
  128. }
  129. }
  130. else
  131. {
  132. [self loadOtherDataFinished];
  133. }
  134. }
  135. - (void)loadImageFinished:(UIImage *)image
  136. {
  137. [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
  138. //写入图片到相册
  139. PHAssetChangeRequest *req = [PHAssetChangeRequest creationRequestForAssetFromImage:image];
  140. } completionHandler:^(BOOL success, NSError * _Nullable error) {
  141. //NSLog(@"success = %d, error = %@", success, error);
  142. if (success) {
  143. HLog(@"已将图片保存至相册");
  144. //NSString *fileName = [self.curDownloadFileModel getFileNameFun];
  145. //[cachesFileManager removeItemAtPath:fileName type:DownLoadFileType error:nil];
  146. } else {
  147. HLog(@"未能将图片保存至相册");
  148. // mainBlock(^{
  149. // [[iToast makeText:NSLocalizedString(@"File_Transfer_By_Cellular_tip",nil)] show];
  150. // });
  151. }
  152. NSString *fileName = [self.curDownloadFileModel getFileNameFun];
  153. [cachesFileManager removeItemAtPath:fileName type:DownLoadFileType error:nil];
  154. [[downloadManager shareInstance] DownloadFileDoneOneFileFun];
  155. }];
  156. }
  157. - (void)loadVideoFinished
  158. {
  159. NSString *fileName = [self.curDownloadFileModel getFileNameFun];
  160. NSString*pathStr = [cachesFileManager getFilePathWithName:fileName type:DownLoadFileType];
  161. PHPhotoLibrary *photoLibrary = [PHPhotoLibrary sharedPhotoLibrary];
  162. [photoLibrary performChanges:^{
  163. [PHAssetChangeRequest creationRequestForAssetFromVideoAtFileURL:[NSURL
  164. fileURLWithPath:pathStr]];
  165. } completionHandler:^(BOOL success, NSError * _Nullable error) {
  166. if (success) {
  167. HLog(@"已将视频保存至相册");
  168. //NSString *fileName = [self.curDownloadFileModel getFileNameFun];
  169. //[cachesFileManager removeItemAtPath:fileName type:DownLoadFileType error:nil];
  170. } else {
  171. HLog(@"未能将视频保存至相册");
  172. }
  173. [cachesFileManager removeItemAtPath:fileName type:DownLoadFileType error:nil];
  174. [[downloadManager shareInstance] DownloadFileDoneOneFileFun];
  175. }];
  176. }
  177. //下载音频 文件等
  178. - (void)loadOtherDataFinished
  179. {
  180. HLog(@"\n\n\n\n\n\n\n\n没有处理 loadOtherDataFinished \n\n\n\n\n\n\n\n");
  181. // [[downloadManager shareInstance] DownloadFileDoneOneFileFun];
  182. //
  183. // NSString *fileName = [self.curDownloadFileModel getFileNameFun];
  184. // NSString*filePath = [cachesFileManager getFilePathWithName:fileName type:DownLoadFileType];
  185. //
  186. // if (![[NSFileManager defaultManager] fileExistsAtPath:filePath]){
  187. // HLog(@"没有找到文件:%@",fileName);
  188. // return;
  189. // }
  190. //
  191. // NSURL * fileURL = [NSURL fileURLWithPath:filePath];
  192. // UIDocumentPickerViewController *documentPickerVC = [[UIDocumentPickerViewController alloc] initWithURL:fileURL inMode:UIDocumentPickerModeExportToService];
  193. // // 设置代理
  194. // documentPickerVC.delegate = self;
  195. // // 设置模态弹出方式
  196. // documentPickerVC.modalPresentationStyle = UIModalPresentationFormSheet;
  197. // [self.navigationController presentViewController:documentPickerVC animated:YES completion:nil];
  198. }
  199. #pragma mark - UIDocumentPickerDelegate
  200. - (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentsAtURLs:(NSArray<NSURL *> *)urls {
  201. // 获取授权
  202. BOOL fileUrlAuthozied = [urls.firstObject startAccessingSecurityScopedResource];
  203. if (fileUrlAuthozied) {
  204. // 通过文件协调工具来得到新的文件地址,以此得到文件保护功能
  205. NSFileCoordinator *fileCoordinator = [[NSFileCoordinator alloc] init];
  206. NSError *error;
  207. [fileCoordinator coordinateReadingItemAtURL:urls.firstObject options:0 error:&error byAccessor:^(NSURL *newURL) {
  208. // 读取文件
  209. NSString *fileName = [newURL lastPathComponent];
  210. NSError *error = nil;
  211. //NSData *fileData = [NSData dataWithContentsOfURL:newURL options:NSDataReadingMappedIfSafe error:&error];
  212. if (error) {
  213. // 读取出错
  214. } else {
  215. // 上传
  216. NSLog(@"fileName : %@", fileName);
  217. [cachesFileManager removeItemAtPath:fileName type:DownLoadFileType error:nil];
  218. }
  219. }];
  220. [urls.firstObject stopAccessingSecurityScopedResource];
  221. } else {
  222. // 授权失败
  223. }
  224. }
  225. @end