backupsFileManager.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. [self beginBackupsFileFun];
  180. }
  181. - (void)RefreshDatabaseFun
  182. {
  183. HLog(@"3 bg_id:%@",_curPhotosBackupsTaskMod.bg_id);
  184. [_curPhotosBackupsTaskMod bg_saveOrUpdateAsync:^(BOOL isSuccess) {
  185. if(isSuccess){
  186. }
  187. }];
  188. }
  189. - (void)afterGetImageDataFun:(NSData*)imageData
  190. {
  191. _curPhotosBackupsTaskMod.imageData = imageData;
  192. _curPhotosBackupsTaskMod.totalBytes = [imageData length];
  193. [[NSNotificationCenter defaultCenter] postNotificationName:backupsFileBeginNotification object:_curPhotosBackupsTaskMod];
  194. }
  195. - (void)deleteVideoFun
  196. {
  197. if(_curPhotosBackupsTaskMod.curUploadFileType == uploadFileTypeVideo){
  198. [cachesFileManager removeItemAtPath:_curPhotosBackupsTaskMod.filename type:uploadFileTypeVideo error:nil];
  199. }
  200. }
  201. - (void)afterGetVideoDataFun
  202. {
  203. [[NSNotificationCenter defaultCenter] postNotificationName:backupsFileBeginNotification object:_curPhotosBackupsTaskMod];
  204. }
  205. - (void)backupsFileDoneFun
  206. {
  207. [self deleteVideoFun];
  208. _curPhotosBackupsTaskMod.didBackupsCount += 1;
  209. [self RefreshDatabaseFun];
  210. [self beginBackupsFileFun];
  211. }
  212. //修改文件上传的状态
  213. - (void)changeBackupsFileStateWithSpeedFun
  214. {
  215. [[NSNotificationCenter defaultCenter] postNotificationName:backupsFileRefreshNotification object:_curPhotosBackupsTaskMod];
  216. }
  217. - (void)suspendBackupsFileFun
  218. {
  219. if(!_curPhotosBackupsTaskMod){
  220. return;
  221. }
  222. _curPhotosBackupsTaskMod.curBackupsState = backupsStateSuspend;
  223. _curPhotosBackupsTaskMod.backupsTipMsg = NSLocalizedString(@"File_backups_Record_tip_Suspend",nil);
  224. [_curPhotosBackupsTaskMod bg_saveOrUpdateAsync:^(BOOL isSuccess) {
  225. if(isSuccess){
  226. [self changeBackupsFileStateWithSpeedFun];
  227. }
  228. }];
  229. }
  230. //文件重新备份
  231. - (void)reBackupsFileFunBy:(photosBackupsTaskModel*)model
  232. {
  233. model.isBackupsSuspendType = backupsSuspendNone;
  234. model.curBackupsState = backupsStateUploading;
  235. _curPhotosBackupsTaskMod = model;
  236. [_curPhotosBackupsTaskMod bg_saveOrUpdateAsync:^(BOOL isSuccess) {
  237. if(isSuccess){
  238. [self beginBackupsFileFun];
  239. }
  240. }];
  241. }
  242. //检查是否需要重新备份
  243. - (void)checkReBackupsFileFun
  244. {
  245. BOOL haveOpenBackups = [HWDataManager getBoolWithKey:Const_photo_backups_state];
  246. if(!haveOpenBackups){
  247. return;
  248. }
  249. //相册权限
  250. if (![[TZImageManager manager] authorizationStatusAuthorized]){
  251. [HWDataManager setBoolWithKey:Const_photo_backups_state value:NO];
  252. return;
  253. }
  254. [photosBackupsTaskModel bg_findAsync:backups_photos_tableName limit:1 orderBy:nil desc:YES complete:^(NSArray * _Nullable array) {
  255. if(!array || array.count == 0){
  256. return;
  257. }
  258. photosBackupsTaskModel *lastModel = array.firstObject;
  259. HLog(@"1 bg_id:%@",lastModel.bg_id);
  260. if(lastModel.isBackupsSuspendType == backupsSuspendByUser){
  261. return;
  262. }
  263. if(lastModel.curBackupsState == backupsStateFail
  264. ||lastModel.curBackupsState == backupsStateDone){//上次备份完成
  265. }
  266. else{//继续上次的备份任务
  267. [self handelBackupsModelFun:lastModel];
  268. }
  269. }];
  270. }
  271. @end