backupsFileManager.m 11 KB

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