PlayerViewController+downloadFile.m 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. NSString *fileName = [self.curDownloadFileModel getFileNameFun];
  48. NSString*pathStr = [cachesFileManager getFilePathWithName:fileName type:DownLoadFileType];
  49. NSFileManager *manager0 = [NSFileManager defaultManager];
  50. long long endOfFile = 0;
  51. if(![manager0 fileExistsAtPath:pathStr]) {
  52. [cachesFileManager getFileNameWithContent:fileData fileName:fileName type:DownLoadFileType];
  53. }
  54. else{
  55. NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:pathStr]; // 创建文件句柄
  56. if (fileHandle) {
  57. endOfFile = [fileHandle seekToEndOfFile];
  58. if (endOfFile >= 0) {
  59. [fileHandle writeData:fileData];
  60. }
  61. endOfFile = [fileHandle seekToEndOfFile];
  62. // 关闭文件句柄
  63. [fileHandle closeFile];
  64. }
  65. }
  66. long didDownloadBytes = fileData.length;
  67. if(endOfFile > 0){
  68. didDownloadBytes = endOfFile;
  69. }
  70. self.curDownloadFileModel.didDownloadBytes = didDownloadBytes;
  71. // if(isDownloadDone){
  72. // NSInteger i;
  73. // }
  74. //
  75. // if(endOfFile == self.curDownloadFileModel.length){
  76. // NSInteger i;
  77. // }
  78. if(isDownloadDone || endOfFile == self.curDownloadFileModel.length){
  79. [self handleDownloadDoneFun];
  80. }
  81. else{//获取下一片
  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. }
  100. else if([self.curDownloadFileModel.fileType isEqualToString:@"video"]){
  101. [self loadVideoFinished];
  102. }
  103. }
  104. - (void)loadImageFinished:(UIImage *)image
  105. {
  106. [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
  107. //写入图片到相册
  108. PHAssetChangeRequest *req = [PHAssetChangeRequest creationRequestForAssetFromImage:image];
  109. } completionHandler:^(BOOL success, NSError * _Nullable error) {
  110. //NSLog(@"success = %d, error = %@", success, error);
  111. if (success) {
  112. HLog(@"已将图片保存至相册");
  113. NSString *fileName = [self.curDownloadFileModel getFileNameFun];
  114. [cachesFileManager removeItemAtPath:fileName type:DownLoadFileType error:nil];
  115. } else {
  116. HLog(@"未能将图片保存至相册");
  117. }
  118. [[downloadManager shareInstance] DownloadFileDoneOneFileFun];
  119. }];
  120. }
  121. - (void)loadVideoFinished
  122. {
  123. NSString *fileName = [self.curDownloadFileModel getFileNameFun];
  124. NSString*pathStr = [cachesFileManager getFilePathWithName:fileName type:DownLoadFileType];
  125. PHPhotoLibrary *photoLibrary = [PHPhotoLibrary sharedPhotoLibrary];
  126. [photoLibrary performChanges:^{
  127. [PHAssetChangeRequest creationRequestForAssetFromVideoAtFileURL:[NSURL
  128. fileURLWithPath:pathStr]];
  129. } completionHandler:^(BOOL success, NSError * _Nullable error) {
  130. if (success) {
  131. HLog(@"已将视频保存至相册");
  132. //NSString *fileName = [self.curDownloadFileModel getFileNameFun];
  133. [cachesFileManager removeItemAtPath:fileName type:DownLoadFileType error:nil];
  134. } else {
  135. HLog(@"未能将视频保存至相册");
  136. }
  137. [[downloadManager shareInstance] DownloadFileDoneOneFileFun];
  138. }];
  139. }
  140. @end