backupsFileManager.m 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. //
  2. // backupsFileManager.m
  3. // 隐私保护
  4. //
  5. // Created by xd h on 2024/1/3.
  6. //
  7. #import "backupsFileManager.h"
  8. #import "cachesFileManager.h"
  9. @implementation backupsFileManager
  10. static backupsFileManager * cur_backupsFileShareInstance = nil;
  11. +(backupsFileManager *)shareInstance;
  12. {
  13. static dispatch_once_t onceToken;
  14. dispatch_once(&onceToken, ^{
  15. cur_backupsFileShareInstance = [[backupsFileManager alloc] init];
  16. });
  17. return cur_backupsFileShareInstance;
  18. }
  19. - (id)init
  20. {
  21. self = [super init];
  22. if (self) {
  23. //[self initManager];
  24. }
  25. return self;
  26. }
  27. - (void)handlePhotosBackupsFun
  28. {
  29. BOOL haveOpenBackups = [HWDataManager getBoolWithKey:Const_photo_backups_state];
  30. if(!haveOpenBackups){
  31. return;
  32. }
  33. //相册权限
  34. if (![[TZImageManager manager] authorizationStatusAuthorized]){
  35. [HWDataManager setBoolWithKey:Const_photo_backups_state value:NO];
  36. return;
  37. }
  38. [photosBackupsTaskModel bg_findAsync:backups_photos_tableName limit:1 orderBy:nil desc:YES complete:^(NSArray * _Nullable array) {
  39. if(!array || array.count == 0){
  40. [self getAllAlbumsFun];
  41. return;
  42. }
  43. photosBackupsTaskModel *lastModel = array.firstObject;
  44. HLog(@"1 bg_id:%@",lastModel.bg_id);
  45. if(lastModel.isSuspendType){
  46. return;
  47. }
  48. if(lastModel.curBackupsState == backupsStateFail
  49. ||lastModel.curBackupsState == backupsStateDone){//上次备份完成
  50. [self getAllAlbumsFun];
  51. }
  52. else{//继续上次的备份任务
  53. [self handelBackupsModelFun:lastModel];
  54. }
  55. }];
  56. }
  57. //备份开始查询相册
  58. - (void)getAllAlbumsFun
  59. {
  60. //
  61. [[TZImageManager manager] getAllAlbums:YES allowPickingImage:YES needFetchAssets:NO completion:^(NSArray<TZAlbumModel *> *models) {
  62. if(models.count >= 1){
  63. TZAlbumModel *curAlbumModel= models.firstObject;
  64. photosBackupsTaskModel * taskModel = [photosBackupsTaskModel new];
  65. taskModel.count = curAlbumModel.count;
  66. //taskModel.result = curAlbumModel.result;
  67. taskModel.failCount = 0;
  68. taskModel.didBackupsCount = 0;
  69. taskModel.bg_tableName = backups_photos_tableName;
  70. NSMutableString *allLocalIdentifier= [NSMutableString new];
  71. for (PHAsset *asset in curAlbumModel.result) {
  72. if (asset.localIdentifier) {
  73. if(allLocalIdentifier.length >0){
  74. [allLocalIdentifier appendString:@"&"];
  75. }
  76. [allLocalIdentifier appendString:asset.localIdentifier];
  77. }
  78. }
  79. taskModel.totalLocalIdentifier = allLocalIdentifier;
  80. HLog(@"2 bg_id:%@",taskModel.bg_id);
  81. [taskModel bg_saveAsync:^(BOOL isSuccess) {
  82. if(!isSuccess){
  83. HLog(@"\n\nError!!!\n\n");
  84. }
  85. else{
  86. //[self handelBackupsModelFun:taskModel];
  87. [self handlePhotosBackupsFun];
  88. }
  89. }];
  90. }
  91. }];
  92. }
  93. - (void)handelBackupsModelFun:(photosBackupsTaskModel*)backupsTaskModel
  94. {
  95. _curPhotosBackupsTaskMod = backupsTaskModel;
  96. _curPhotosBackupsTaskMod.LocalIdentifierArr = [backupsTaskModel.totalLocalIdentifier componentsSeparatedByString:@"&"];
  97. if(_curPhotosBackupsTaskMod.LocalIdentifierArr.count
  98. <= (_curPhotosBackupsTaskMod.didBackupsCount + _curPhotosBackupsTaskMod.failCount))
  99. {
  100. _curPhotosBackupsTaskMod.curBackupsState = backupsStateDone;
  101. [self RefreshDatabaseFun];
  102. return;
  103. }
  104. [self beginBackupsFileFun];
  105. }
  106. - (void)beginBackupsFileFun
  107. {
  108. NSInteger index = _curPhotosBackupsTaskMod.didBackupsCount + _curPhotosBackupsTaskMod.failCount;
  109. if(index >= _curPhotosBackupsTaskMod.LocalIdentifierArr.count){
  110. _curPhotosBackupsTaskMod.curBackupsState = backupsStateDone;
  111. [self RefreshDatabaseFun];
  112. return;
  113. }
  114. NSString *curLocalIdentifier = _curPhotosBackupsTaskMod.LocalIdentifierArr[index];
  115. if(!curLocalIdentifier || curLocalIdentifier.length == 0)
  116. {
  117. [self getDataWrongToChangeFailFun];
  118. return;
  119. }
  120. PHFetchResult *fetchResult = [PHAsset fetchAssetsWithLocalIdentifiers:@[curLocalIdentifier] options:nil];
  121. PHAsset *curAsset = fetchResult.firstObject;
  122. _curPhotosBackupsTaskMod.filename = [curAsset valueForKey:@"filename"];
  123. KWeakSelf
  124. if(curAsset.mediaType == PHAssetMediaTypeImage){
  125. _curPhotosBackupsTaskMod.curUploadFileType = uploadFileTypeImage;
  126. [[PHImageManager defaultManager] requestImageDataForAsset:curAsset options:nil resultHandler:^(NSData * _Nullable imageData, NSString * _Nullable dataUTI, UIImageOrientation orientation, NSDictionary * _Nullable info) {
  127. // 直接得到最终的 NSData 数据
  128. if (imageData) {
  129. [weakSelf afterGetImageDataFun:imageData];
  130. }
  131. else{
  132. [weakSelf getDataWrongToChangeFailFun];
  133. }
  134. }];
  135. }
  136. else{
  137. _curPhotosBackupsTaskMod.curUploadFileType = uploadFileTypeVideo;
  138. //判断文件是否在app内
  139. BOOL didSaveFile = [cachesFileManager checkFileIsSaveState:_curPhotosBackupsTaskMod.filename withType:uploadFileTypeVideo];
  140. if(didSaveFile){
  141. [self afterGetVideoDataFun];
  142. return;
  143. }
  144. //真正的视频数据
  145. PHVideoRequestOptions *options = [[PHVideoRequestOptions alloc] init];
  146. options.version = PHVideoRequestOptionsVersionOriginal;
  147. [[PHImageManager defaultManager] requestAVAssetForVideo:curAsset options:options resultHandler:^(AVAsset *asset, AVAudioMix *audioMix, NSDictionary *info) {
  148. if ([asset isKindOfClass:[AVURLAsset class]]) {
  149. AVURLAsset* urlAsset = (AVURLAsset*)asset;
  150. BOOL isSuc = [cachesFileManager copyVideoItemAtPath:[urlAsset.URL path] fileName:self->_curPhotosBackupsTaskMod.filename error:nil];
  151. if (isSuc) {
  152. [weakSelf afterGetVideoDataFun];
  153. }
  154. else{
  155. [weakSelf getDataWrongToChangeFailFun];
  156. }
  157. }
  158. }];
  159. }
  160. }
  161. - (void)getDataWrongToChangeFailFun
  162. {
  163. [self deleteVideoFun];
  164. _curPhotosBackupsTaskMod.failCount += 1;
  165. [self RefreshDatabaseFun];
  166. [self beginBackupsFileFun];
  167. }
  168. - (void)RefreshDatabaseFun
  169. {
  170. HLog(@"3 bg_id:%@",_curPhotosBackupsTaskMod.bg_id);
  171. [_curPhotosBackupsTaskMod bg_saveOrUpdateAsync:^(BOOL isSuccess) {
  172. if(isSuccess){
  173. }
  174. }];
  175. }
  176. - (void)afterGetImageDataFun:(NSData*)imageData
  177. {
  178. _curPhotosBackupsTaskMod.imageData = imageData;
  179. _curPhotosBackupsTaskMod.totalBytes = [imageData length];
  180. [[NSNotificationCenter defaultCenter] postNotificationName:backupsFileBeginNotification object:_curPhotosBackupsTaskMod];
  181. }
  182. - (void)deleteVideoFun
  183. {
  184. if(_curPhotosBackupsTaskMod.curUploadFileType == uploadFileTypeVideo){
  185. [cachesFileManager removeItemAtPath:_curPhotosBackupsTaskMod.filename type:uploadFileTypeVideo error:nil];
  186. }
  187. }
  188. - (void)afterGetVideoDataFun
  189. {
  190. [[NSNotificationCenter defaultCenter] postNotificationName:backupsFileBeginNotification object:_curPhotosBackupsTaskMod];
  191. }
  192. - (void)backupsFileDoneFun
  193. {
  194. [self deleteVideoFun];
  195. _curPhotosBackupsTaskMod.didBackupsCount +=1;
  196. [self RefreshDatabaseFun];
  197. [self beginBackupsFileFun];
  198. // long totalSizeByte = _curUploadFileDataModel.totalBytes;
  199. //
  200. // [self changeUploadFileState:uploadStateDone withDidUploadBytes:totalSizeByte withModel:_curUploadFileDataModel complete:^(BOOL isSuccess) {
  201. // [self->_fileModelDataArr removeObject:self->_curUploadFileDataModel];
  202. //
  203. // self->_isUploadIngType = NO;
  204. // if(self->_fileModelDataArr.count > 0){
  205. // [self beginUploadFileFun];
  206. // }
  207. // }];
  208. }
  209. @end