backupsFileManager.m 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. //return;
  30. BOOL haveOpenBackups = [HWDataManager getBoolWithKey:Const_photo_backups_state];
  31. if(!haveOpenBackups){
  32. return;
  33. }
  34. //相册权限
  35. if (![[TZImageManager manager] authorizationStatusAuthorized]){
  36. [HWDataManager setBoolWithKey:Const_photo_backups_state value:NO];
  37. return;
  38. }
  39. [photosBackupsTaskModel bg_findAsync:backups_photos_tableName limit:1 orderBy:nil desc:YES complete:^(NSArray * _Nullable array) {
  40. if(!array || array.count == 0){
  41. [self getAllAlbumsFun];
  42. return;
  43. }
  44. photosBackupsTaskModel *lastModel = array.firstObject;
  45. HLog(@"1 bg_id:%@",lastModel.bg_id);
  46. if(lastModel.isSuspendType){
  47. return;
  48. }
  49. if(lastModel.curBackupsState == backupsStateFail
  50. ||lastModel.curBackupsState == backupsStateDone){//上次备份完成
  51. [self getAllAlbumsFun];
  52. }
  53. else{//继续上次的备份任务
  54. [self handelBackupsModelFun:lastModel];
  55. }
  56. }];
  57. }
  58. //备份开始查询相册
  59. - (void)getAllAlbumsFun
  60. {
  61. //
  62. [[TZImageManager manager] getAllAlbums:YES allowPickingImage:YES needFetchAssets:NO completion:^(NSArray<TZAlbumModel *> *models) {
  63. if(models.count >= 1){
  64. TZAlbumModel *curAlbumModel= models.firstObject;
  65. photosBackupsTaskModel * taskModel = [photosBackupsTaskModel new];
  66. taskModel.count = curAlbumModel.count;
  67. //taskModel.result = curAlbumModel.result;
  68. taskModel.failCount = 0;
  69. taskModel.didBackupsCount = 0;
  70. taskModel.bg_tableName = backups_photos_tableName;
  71. NSMutableString *allLocalIdentifier= [NSMutableString new];
  72. for (PHAsset *asset in curAlbumModel.result) {
  73. if (asset.localIdentifier) {
  74. if(allLocalIdentifier.length >0){
  75. [allLocalIdentifier appendString:@"&"];
  76. }
  77. [allLocalIdentifier appendString:asset.localIdentifier];
  78. }
  79. }
  80. taskModel.totalLocalIdentifier = allLocalIdentifier;
  81. HLog(@"2 bg_id:%@",taskModel.bg_id);
  82. [taskModel bg_saveAsync:^(BOOL isSuccess) {
  83. if(!isSuccess){
  84. HLog(@"\n\nError!!!\n\n");
  85. }
  86. else{
  87. //[self handelBackupsModelFun:taskModel];
  88. [self handlePhotosBackupsFun];
  89. }
  90. }];
  91. }
  92. }];
  93. }
  94. - (void)handelBackupsModelFun:(photosBackupsTaskModel*)backupsTaskModel
  95. {
  96. _curPhotosBackupsTaskMod = backupsTaskModel;
  97. _curPhotosBackupsTaskMod.LocalIdentifierArr = [backupsTaskModel.totalLocalIdentifier componentsSeparatedByString:@"&"];
  98. if(_curPhotosBackupsTaskMod.LocalIdentifierArr.count
  99. <= (_curPhotosBackupsTaskMod.didBackupsCount + _curPhotosBackupsTaskMod.failCount))
  100. {
  101. _curPhotosBackupsTaskMod.curBackupsState = backupsStateDone;
  102. [self RefreshDatabaseFun];
  103. [self changeBackupsFileStateWithSpeedFun];
  104. return;
  105. }
  106. [self beginBackupsFileFun];
  107. }
  108. - (void)beginBackupsFileFun
  109. {
  110. NSInteger index = _curPhotosBackupsTaskMod.didBackupsCount + _curPhotosBackupsTaskMod.failCount;
  111. if(index >= _curPhotosBackupsTaskMod.LocalIdentifierArr.count){
  112. _curPhotosBackupsTaskMod.curBackupsState = backupsStateDone;
  113. [self RefreshDatabaseFun];
  114. [self changeBackupsFileStateWithSpeedFun];
  115. return;
  116. }
  117. NSString *curLocalIdentifier = _curPhotosBackupsTaskMod.LocalIdentifierArr[index];
  118. if(!curLocalIdentifier || curLocalIdentifier.length == 0)
  119. {
  120. [self getDataWrongToChangeFailFun];
  121. return;
  122. }
  123. PHFetchResult *fetchResult = [PHAsset fetchAssetsWithLocalIdentifiers:@[curLocalIdentifier] options:nil];
  124. PHAsset *curAsset = fetchResult.firstObject;
  125. _curPhotosBackupsTaskMod.filename = [curAsset valueForKey:@"filename"];
  126. KWeakSelf
  127. if(curAsset.mediaType == PHAssetMediaTypeImage){
  128. _curPhotosBackupsTaskMod.curUploadFileType = uploadFileTypeImage;
  129. [[PHImageManager defaultManager] requestImageDataForAsset:curAsset options:nil resultHandler:^(NSData * _Nullable imageData, NSString * _Nullable dataUTI, UIImageOrientation orientation, NSDictionary * _Nullable info) {
  130. // 直接得到最终的 NSData 数据
  131. if (imageData) {
  132. [weakSelf afterGetImageDataFun:imageData];
  133. }
  134. else{
  135. [weakSelf getDataWrongToChangeFailFun];
  136. }
  137. }];
  138. }
  139. else{
  140. _curPhotosBackupsTaskMod.curUploadFileType = uploadFileTypeVideo;
  141. //判断文件是否在app内
  142. BOOL didSaveFile = [cachesFileManager checkFileIsSaveState:_curPhotosBackupsTaskMod.filename withType:uploadFileTypeVideo];
  143. if(didSaveFile){
  144. [self afterGetVideoDataFun];
  145. return;
  146. }
  147. //真正的视频数据
  148. PHVideoRequestOptions *options = [[PHVideoRequestOptions alloc] init];
  149. options.version = PHVideoRequestOptionsVersionOriginal;
  150. [[PHImageManager defaultManager] requestAVAssetForVideo:curAsset options:options resultHandler:^(AVAsset *asset, AVAudioMix *audioMix, NSDictionary *info) {
  151. if ([asset isKindOfClass:[AVURLAsset class]]) {
  152. AVURLAsset* urlAsset = (AVURLAsset*)asset;
  153. BOOL isSuc = [cachesFileManager copyVideoItemAtPath:[urlAsset.URL path] fileName:self->_curPhotosBackupsTaskMod.filename error:nil];
  154. if (isSuc) {
  155. [weakSelf afterGetVideoDataFun];
  156. }
  157. else{
  158. [weakSelf getDataWrongToChangeFailFun];
  159. }
  160. }
  161. }];
  162. }
  163. }
  164. - (void)getDataWrongToChangeFailFun
  165. {
  166. [self deleteVideoFun];
  167. _curPhotosBackupsTaskMod.failCount += 1;
  168. [self RefreshDatabaseFun];
  169. [self beginBackupsFileFun];
  170. }
  171. - (void)RefreshDatabaseFun
  172. {
  173. HLog(@"3 bg_id:%@",_curPhotosBackupsTaskMod.bg_id);
  174. [_curPhotosBackupsTaskMod bg_saveOrUpdateAsync:^(BOOL isSuccess) {
  175. if(isSuccess){
  176. }
  177. }];
  178. }
  179. - (void)afterGetImageDataFun:(NSData*)imageData
  180. {
  181. _curPhotosBackupsTaskMod.imageData = imageData;
  182. _curPhotosBackupsTaskMod.totalBytes = [imageData length];
  183. [[NSNotificationCenter defaultCenter] postNotificationName:backupsFileBeginNotification object:_curPhotosBackupsTaskMod];
  184. }
  185. - (void)deleteVideoFun
  186. {
  187. if(_curPhotosBackupsTaskMod.curUploadFileType == uploadFileTypeVideo){
  188. [cachesFileManager removeItemAtPath:_curPhotosBackupsTaskMod.filename type:uploadFileTypeVideo error:nil];
  189. }
  190. }
  191. - (void)afterGetVideoDataFun
  192. {
  193. [[NSNotificationCenter defaultCenter] postNotificationName:backupsFileBeginNotification object:_curPhotosBackupsTaskMod];
  194. }
  195. - (void)backupsFileDoneFun
  196. {
  197. [self deleteVideoFun];
  198. _curPhotosBackupsTaskMod.didBackupsCount +=1;
  199. [self RefreshDatabaseFun];
  200. [self beginBackupsFileFun];
  201. }
  202. //修改文件上传的状态
  203. - (void)changeBackupsFileStateWithSpeedFun
  204. {
  205. [[NSNotificationCenter defaultCenter] postNotificationName:backupsFileRefreshNotification object:_curPhotosBackupsTaskMod];
  206. }
  207. - (void)suspendBackupsFileFun
  208. {
  209. if(!_curPhotosBackupsTaskMod){
  210. return;
  211. }
  212. _curPhotosBackupsTaskMod.curBackupsState = backupsStateSuspend;
  213. _curPhotosBackupsTaskMod.backupsTipMsg = NSLocalizedString(@"File_backups_Record_tip_Suspend",nil);
  214. [_curPhotosBackupsTaskMod bg_saveOrUpdateAsync:^(BOOL isSuccess) {
  215. if(isSuccess){
  216. [self changeBackupsFileStateWithSpeedFun];
  217. }
  218. }];
  219. }
  220. //文件重新备份
  221. - (void)reBackupsFileFunBy:(photosBackupsTaskModel*)model
  222. {
  223. model.isSuspendType = NO;
  224. model.curBackupsState = backupsStateUploading;
  225. _curPhotosBackupsTaskMod = model;
  226. [_curPhotosBackupsTaskMod bg_saveOrUpdateAsync:^(BOOL isSuccess) {
  227. if(isSuccess){
  228. [self beginBackupsFileFun];
  229. }
  230. }];
  231. }
  232. @end