backupsFileManager.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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.isBackupsSuspendType == backupsSuspendByUser){
  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.isBackupsSuspendType = backupsSuspendNone;
  98. _curPhotosBackupsTaskMod.curBackupsState = backupsStateUploading;
  99. _curPhotosBackupsTaskMod.LocalIdentifierArr = [backupsTaskModel.totalLocalIdentifier componentsSeparatedByString:@"&"];
  100. if(_curPhotosBackupsTaskMod.LocalIdentifierArr.count
  101. <= (_curPhotosBackupsTaskMod.didBackupsCount + _curPhotosBackupsTaskMod.failCount))
  102. {
  103. _curPhotosBackupsTaskMod.curBackupsState = backupsStateDone;
  104. [self RefreshDatabaseFun];
  105. [self changeBackupsFileStateWithSpeedFun];
  106. return;
  107. }
  108. [self beginBackupsFileFun];
  109. }
  110. - (void)beginBackupsFileFun
  111. {
  112. NSInteger index = _curPhotosBackupsTaskMod.didBackupsCount + _curPhotosBackupsTaskMod.failCount;
  113. if(index >= _curPhotosBackupsTaskMod.LocalIdentifierArr.count){
  114. _curPhotosBackupsTaskMod.curBackupsState = backupsStateDone;
  115. [self RefreshDatabaseFun];
  116. [self changeBackupsFileStateWithSpeedFun];
  117. return;
  118. }
  119. NSString *curLocalIdentifier = _curPhotosBackupsTaskMod.LocalIdentifierArr[index];
  120. if(!curLocalIdentifier || curLocalIdentifier.length == 0)
  121. {
  122. [self getDataWrongToChangeFailFun];
  123. return;
  124. }
  125. PHFetchResult *fetchResult = [PHAsset fetchAssetsWithLocalIdentifiers:@[curLocalIdentifier] options:nil];
  126. PHAsset *curAsset = fetchResult.firstObject;
  127. _curPhotosBackupsTaskMod.filename = [curAsset valueForKey:@"filename"];
  128. KWeakSelf
  129. if(curAsset.mediaType == PHAssetMediaTypeImage){
  130. _curPhotosBackupsTaskMod.curUploadFileType = uploadFileTypeImage;
  131. [[PHImageManager defaultManager] requestImageDataForAsset:curAsset options:nil resultHandler:^(NSData * _Nullable imageData, NSString * _Nullable dataUTI, UIImageOrientation orientation, NSDictionary * _Nullable info) {
  132. // 直接得到最终的 NSData 数据
  133. if (imageData) {
  134. [weakSelf afterGetImageDataFun:imageData];
  135. }
  136. else{
  137. [weakSelf getDataWrongToChangeFailFun];
  138. }
  139. }];
  140. }
  141. else{
  142. _curPhotosBackupsTaskMod.curUploadFileType = uploadFileTypeVideo;
  143. //判断文件是否在app内
  144. BOOL didSaveFile = [cachesFileManager checkFileIsSaveState:_curPhotosBackupsTaskMod.filename withType:uploadFileTypeVideo];
  145. if(didSaveFile){
  146. [self afterGetVideoDataFun];
  147. return;
  148. }
  149. //真正的视频数据
  150. PHVideoRequestOptions *options = [[PHVideoRequestOptions alloc] init];
  151. options.version = PHVideoRequestOptionsVersionOriginal;
  152. [[PHImageManager defaultManager] requestAVAssetForVideo:curAsset options:options resultHandler:^(AVAsset *asset, AVAudioMix *audioMix, NSDictionary *info) {
  153. if ([asset isKindOfClass:[AVURLAsset class]]) {
  154. AVURLAsset* urlAsset = (AVURLAsset*)asset;
  155. BOOL isSuc = [cachesFileManager copyVideoItemAtPath:[urlAsset.URL path] fileName:self->_curPhotosBackupsTaskMod.filename error:nil];
  156. if (isSuc) {
  157. [weakSelf afterGetVideoDataFun];
  158. }
  159. else{
  160. [weakSelf getDataWrongToChangeFailFun];
  161. }
  162. }
  163. }];
  164. }
  165. }
  166. - (void)getDataWrongToChangeFailFun
  167. {
  168. [self deleteVideoFun];
  169. _curPhotosBackupsTaskMod.failCount += 1;
  170. [self RefreshDatabaseFun];
  171. [self beginBackupsFileFun];
  172. }
  173. - (void)RefreshDatabaseFun
  174. {
  175. HLog(@"3 bg_id:%@",_curPhotosBackupsTaskMod.bg_id);
  176. [_curPhotosBackupsTaskMod bg_saveOrUpdateAsync:^(BOOL isSuccess) {
  177. if(isSuccess){
  178. }
  179. }];
  180. }
  181. - (void)afterGetImageDataFun:(NSData*)imageData
  182. {
  183. _curPhotosBackupsTaskMod.imageData = imageData;
  184. _curPhotosBackupsTaskMod.totalBytes = [imageData length];
  185. [[NSNotificationCenter defaultCenter] postNotificationName:backupsFileBeginNotification object:_curPhotosBackupsTaskMod];
  186. }
  187. - (void)deleteVideoFun
  188. {
  189. if(_curPhotosBackupsTaskMod.curUploadFileType == uploadFileTypeVideo){
  190. [cachesFileManager removeItemAtPath:_curPhotosBackupsTaskMod.filename type:uploadFileTypeVideo error:nil];
  191. }
  192. }
  193. - (void)afterGetVideoDataFun
  194. {
  195. [[NSNotificationCenter defaultCenter] postNotificationName:backupsFileBeginNotification object:_curPhotosBackupsTaskMod];
  196. }
  197. - (void)backupsFileDoneFun
  198. {
  199. [self deleteVideoFun];
  200. _curPhotosBackupsTaskMod.didBackupsCount +=1;
  201. [self RefreshDatabaseFun];
  202. [self beginBackupsFileFun];
  203. }
  204. //修改文件上传的状态
  205. - (void)changeBackupsFileStateWithSpeedFun
  206. {
  207. [[NSNotificationCenter defaultCenter] postNotificationName:backupsFileRefreshNotification object:_curPhotosBackupsTaskMod];
  208. }
  209. - (void)suspendBackupsFileFun
  210. {
  211. if(!_curPhotosBackupsTaskMod){
  212. return;
  213. }
  214. _curPhotosBackupsTaskMod.curBackupsState = backupsStateSuspend;
  215. _curPhotosBackupsTaskMod.backupsTipMsg = NSLocalizedString(@"File_backups_Record_tip_Suspend",nil);
  216. [_curPhotosBackupsTaskMod bg_saveOrUpdateAsync:^(BOOL isSuccess) {
  217. if(isSuccess){
  218. [self changeBackupsFileStateWithSpeedFun];
  219. }
  220. }];
  221. }
  222. //文件重新备份
  223. - (void)reBackupsFileFunBy:(photosBackupsTaskModel*)model
  224. {
  225. model.isBackupsSuspendType = backupsSuspendNone;
  226. model.curBackupsState = backupsStateUploading;
  227. _curPhotosBackupsTaskMod = model;
  228. [_curPhotosBackupsTaskMod bg_saveOrUpdateAsync:^(BOOL isSuccess) {
  229. if(isSuccess){
  230. [self beginBackupsFileFun];
  231. }
  232. }];
  233. }
  234. //检查是否需要重新备份
  235. - (void)checkReBackupsFileFun
  236. {
  237. BOOL haveOpenBackups = [HWDataManager getBoolWithKey:Const_photo_backups_state];
  238. if(!haveOpenBackups){
  239. return;
  240. }
  241. //相册权限
  242. if (![[TZImageManager manager] authorizationStatusAuthorized]){
  243. [HWDataManager setBoolWithKey:Const_photo_backups_state value:NO];
  244. return;
  245. }
  246. [photosBackupsTaskModel bg_findAsync:backups_photos_tableName limit:1 orderBy:nil desc:YES complete:^(NSArray * _Nullable array) {
  247. if(!array || array.count == 0){
  248. return;
  249. }
  250. photosBackupsTaskModel *lastModel = array.firstObject;
  251. HLog(@"1 bg_id:%@",lastModel.bg_id);
  252. if(lastModel.isBackupsSuspendType == backupsSuspendByUser){
  253. return;
  254. }
  255. if(lastModel.curBackupsState == backupsStateFail
  256. ||lastModel.curBackupsState == backupsStateDone){//上次备份完成
  257. }
  258. else{//继续上次的备份任务
  259. [self handelBackupsModelFun:lastModel];
  260. }
  261. }];
  262. }
  263. @end