PlayerViewController+downloadFile.m 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. [fileHandle writeData:fileData];
  64. }
  65. endOfFile = [fileHandle seekToEndOfFile];
  66. // 关闭文件句柄
  67. [fileHandle closeFile];
  68. }
  69. }
  70. long didDownloadBytes = fileData.length;
  71. if(endOfFile > 0){
  72. didDownloadBytes = endOfFile;
  73. }
  74. self.curDownloadFileModel.didDownloadBytes = didDownloadBytes;
  75. // if(isDownloadDone || endOfFile == self.curDownloadFileModel.length){
  76. // [self handleDownloadDoneFun];
  77. // }
  78. // else
  79. {//获取下一片
  80. [[downloadManager shareInstance] DownloadFileChangeingOneFileFun];
  81. if([downloadManager shareInstance].isSuspendType){
  82. return;
  83. }
  84. NSString * commandStr = [RCCommandHelp applyForDownloadFileBy:self.downloadFileTaskUid withFilePath:self.curDownloadFileModel.path withPosition:didDownloadBytes];
  85. [self.commandChannelManager rc_sendData:commandStr];
  86. }
  87. }
  88. - (void)handleDownloadDoneFun
  89. {
  90. if([self.curDownloadFileModel.fileType isEqualToString:@".jpg"]){
  91. NSString *fileName = [self.curDownloadFileModel getFileNameFun];
  92. NSString*pathStr = [cachesFileManager getFilePathWithName:fileName type:DownLoadFileType];
  93. UIImage *image = [UIImage imageWithContentsOfFile:pathStr];
  94. if(image){
  95. [self loadImageFinished:image];
  96. }
  97. else{
  98. [cachesFileManager removeItemAtPath:fileName type:DownLoadFileType error:nil];
  99. [[downloadManager shareInstance] DownloadFileFailOneFileFun];
  100. }
  101. }
  102. else if([self.curDownloadFileModel.fileType isEqualToString:@"video"]){
  103. [self loadVideoFinished];
  104. }
  105. }
  106. - (void)loadImageFinished:(UIImage *)image
  107. {
  108. [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
  109. //写入图片到相册
  110. PHAssetChangeRequest *req = [PHAssetChangeRequest creationRequestForAssetFromImage:image];
  111. } completionHandler:^(BOOL success, NSError * _Nullable error) {
  112. //NSLog(@"success = %d, error = %@", success, error);
  113. if (success) {
  114. HLog(@"已将图片保存至相册");
  115. NSString *fileName = [self.curDownloadFileModel getFileNameFun];
  116. [cachesFileManager removeItemAtPath:fileName type:DownLoadFileType error:nil];
  117. } else {
  118. HLog(@"未能将图片保存至相册");
  119. }
  120. [[downloadManager shareInstance] DownloadFileDoneOneFileFun];
  121. }];
  122. }
  123. - (void)loadVideoFinished
  124. {
  125. NSString *fileName = [self.curDownloadFileModel getFileNameFun];
  126. NSString*pathStr = [cachesFileManager getFilePathWithName:fileName type:DownLoadFileType];
  127. PHPhotoLibrary *photoLibrary = [PHPhotoLibrary sharedPhotoLibrary];
  128. [photoLibrary performChanges:^{
  129. [PHAssetChangeRequest creationRequestForAssetFromVideoAtFileURL:[NSURL
  130. fileURLWithPath:pathStr]];
  131. } completionHandler:^(BOOL success, NSError * _Nullable error) {
  132. if (success) {
  133. HLog(@"已将视频保存至相册");
  134. //NSString *fileName = [self.curDownloadFileModel getFileNameFun];
  135. [cachesFileManager removeItemAtPath:fileName type:DownLoadFileType error:nil];
  136. } else {
  137. HLog(@"未能将视频保存至相册");
  138. }
  139. [[downloadManager shareInstance] DownloadFileDoneOneFileFun];
  140. }];
  141. }
  142. @end