PlayerViewController+downloadFile.m 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. //
  2. // PlayerViewController+downloadFile.m
  3. // 隐私保护
  4. //
  5. // Created by xd h on 2024/1/9.
  6. //
  7. #import "PlayerViewController+downloadFile.h"
  8. #import "cachesFileManager.h"
  9. #import "downloadManager.h"
  10. @implementation PlayerViewController (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. {//重新生成taskUid
  27. [self ApplyDownloadFileFun];
  28. return;
  29. }
  30. NSString *fileName = [self.curDownloadFileModel getFileNameFun];
  31. NSString*pathStr = [cachesFileManager getFilePathWithName:fileName type:DownLoadFileType];
  32. long didDownloadBytes = 0;
  33. NSFileManager *manager0 = [NSFileManager defaultManager];
  34. if([manager0 fileExistsAtPath:pathStr]) {
  35. NSFileHandle *fileHandle = [NSFileHandle fileHandleForReadingAtPath:pathStr]; // 创建文件句柄
  36. if (fileHandle) {
  37. didDownloadBytes = [fileHandle seekToEndOfFile];
  38. // 关闭文件句柄
  39. [fileHandle closeFile];
  40. }
  41. }
  42. NSString * commandStr = [RCCommandHelp applyForDownloadFileBy:self.downloadFileTaskUid withFilePath:self.curDownloadFileModel.path withPosition:didDownloadBytes];
  43. [self.commandChannelManager rc_sendData:commandStr];
  44. }
  45. - (void)downloadFileFunServiceResponseFun:(NSData *)fileData with:(BOOL)isDownloadDone
  46. {
  47. if(isDownloadDone){
  48. [self handleDownloadDoneFun];
  49. return;
  50. }
  51. NSString *fileName = [self.curDownloadFileModel getFileNameFun];
  52. NSString*pathStr = [cachesFileManager getFilePathWithName:fileName type:DownLoadFileType];
  53. NSFileManager *manager0 = [NSFileManager defaultManager];
  54. long long endOfFile = 0;
  55. if(![manager0 fileExistsAtPath:pathStr]) {
  56. [cachesFileManager getFileNameWithContent:fileData fileName:fileName type:DownLoadFileType];
  57. }
  58. else{
  59. NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:pathStr]; // 创建文件句柄
  60. if (fileHandle) {
  61. endOfFile = [fileHandle seekToEndOfFile];
  62. if (endOfFile >= 0) {
  63. if(endOfFile == self.curDownloadFileModel.didDownloadBytes){
  64. [fileHandle writeData:fileData];
  65. }
  66. }
  67. endOfFile = [fileHandle seekToEndOfFile];
  68. // 关闭文件句柄
  69. [fileHandle closeFile];
  70. }
  71. }
  72. long didDownloadBytes = fileData.length;
  73. if(endOfFile > 0){
  74. didDownloadBytes = endOfFile;
  75. }
  76. self.curDownloadFileModel.didDownloadBytes = didDownloadBytes;
  77. // if(isDownloadDone || endOfFile == self.curDownloadFileModel.length){
  78. // [self handleDownloadDoneFun];
  79. // }
  80. // else
  81. {//获取下一片
  82. [[downloadManager shareInstance] DownloadFileChangeingOneFileFun];
  83. if([downloadManager shareInstance].isSuspendType){
  84. return;
  85. }
  86. NSString * commandStr = [RCCommandHelp applyForDownloadFileBy:self.downloadFileTaskUid withFilePath:self.curDownloadFileModel.path withPosition:didDownloadBytes];
  87. [self.commandChannelManager rc_sendData:commandStr];
  88. }
  89. }
  90. - (void)handleDownloadDoneFun
  91. {
  92. if([self.curDownloadFileModel.fileType isEqualToString:@".jpg"]){
  93. NSString *fileName = [self.curDownloadFileModel getFileNameFun];
  94. NSString*pathStr = [cachesFileManager getFilePathWithName:fileName type:DownLoadFileType];
  95. UIImage *image = [UIImage imageWithContentsOfFile:pathStr];
  96. if(image){
  97. [self loadImageFinished:image];
  98. }
  99. else{
  100. [cachesFileManager removeItemAtPath:fileName type:DownLoadFileType error:nil];
  101. [[downloadManager shareInstance] DownloadFileFailOneFileFun];
  102. }
  103. }
  104. else if([self.curDownloadFileModel.fileType isEqualToString:@"video"]){
  105. [self loadVideoFinished];
  106. }
  107. }
  108. - (void)loadImageFinished:(UIImage *)image
  109. {
  110. [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
  111. //写入图片到相册
  112. PHAssetChangeRequest *req = [PHAssetChangeRequest creationRequestForAssetFromImage:image];
  113. } completionHandler:^(BOOL success, NSError * _Nullable error) {
  114. //NSLog(@"success = %d, error = %@", success, error);
  115. if (success) {
  116. HLog(@"已将图片保存至相册");
  117. NSString *fileName = [self.curDownloadFileModel getFileNameFun];
  118. [cachesFileManager removeItemAtPath:fileName type:DownLoadFileType error:nil];
  119. } else {
  120. HLog(@"未能将图片保存至相册");
  121. }
  122. [[downloadManager shareInstance] DownloadFileDoneOneFileFun];
  123. }];
  124. }
  125. - (void)loadVideoFinished
  126. {
  127. NSString *fileName = [self.curDownloadFileModel getFileNameFun];
  128. NSString*pathStr = [cachesFileManager getFilePathWithName:fileName type:DownLoadFileType];
  129. PHPhotoLibrary *photoLibrary = [PHPhotoLibrary sharedPhotoLibrary];
  130. [photoLibrary performChanges:^{
  131. [PHAssetChangeRequest creationRequestForAssetFromVideoAtFileURL:[NSURL
  132. fileURLWithPath:pathStr]];
  133. } completionHandler:^(BOOL success, NSError * _Nullable error) {
  134. if (success) {
  135. HLog(@"已将视频保存至相册");
  136. //NSString *fileName = [self.curDownloadFileModel getFileNameFun];
  137. [cachesFileManager removeItemAtPath:fileName type:DownLoadFileType error:nil];
  138. } else {
  139. HLog(@"未能将视频保存至相册");
  140. }
  141. [[downloadManager shareInstance] DownloadFileDoneOneFileFun];
  142. }];
  143. }
  144. @end