123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348 |
- //
- // backupsFileManager.m
- // 隐私保护
- //
- // Created by xd h on 2024/1/3.
- //
- #import "backupsFileManager.h"
- #import "cachesFileManager.h"
- @implementation backupsFileManager
- static backupsFileManager * cur_backupsFileShareInstance = nil;
- +(backupsFileManager *)shareInstance;
- {
- static dispatch_once_t onceToken;
-
- dispatch_once(&onceToken, ^{
- cur_backupsFileShareInstance = [[backupsFileManager alloc] init];
-
- });
-
- return cur_backupsFileShareInstance;
- }
- - (id)init
- {
- self = [super init];
-
- if (self) {
- //[self initManager];
- }
-
- return self;
- }
- - (void)handlePhotosBackupsFun
- {
- //return;
-
- BOOL haveOpenBackups = [HWDataManager getBoolWithKey:Const_photo_backups_state];
-
- if(!haveOpenBackups){
- return;
- }
-
- //相册权限
- if (![[TZImageManager manager] authorizationStatusAuthorized]){
- [HWDataManager setBoolWithKey:Const_photo_backups_state value:NO];
- return;
- }
-
-
- [photosBackupsTaskModel bg_findAsync:backups_photos_tableName limit:0 orderBy:nil desc:YES complete:^(NSArray * _Nullable array) {
- if(!array || array.count == 0){
- [self getAllAlbumsFun];
- return;
- }
-
- if(array.count >1){
- [self keepDatabaseOneDataFun];
- }
-
- photosBackupsTaskModel *lastModel = array.firstObject;
-
- HLog(@"1 bg_id:%@",lastModel.bg_id);
-
- if(lastModel.isBackupsSuspendType == backupsSuspendByUser){
- return;
- }
-
- if(lastModel.curBackupsState == backupsStateFail
- ||lastModel.curBackupsState == backupsStateDone){//上次备份完成
- [self getAllAlbumsFun];
- }
- else{//继续上次的备份任务
- [self handelBackupsModelFun:lastModel];
- }
- }];
-
- }
- - (void)keepDatabaseOneDataFun
- {
- [photosBackupsTaskModel bg_deleteFirstObject:backups_photos_tableName];
- //[photosBackupsTaskModel bg_findAsync:backups_photos_tableName
- }
- //备份开始查询相册
- - (void)getAllAlbumsFun
- {
- //
- [[TZImageManager manager] getAllAlbums:YES allowPickingImage:YES needFetchAssets:NO completion:^(NSArray<TZAlbumModel *> *models) {
-
- if(models.count >= 1){
- TZAlbumModel *curAlbumModel= models.firstObject;
-
- photosBackupsTaskModel * taskModel = [photosBackupsTaskModel new];
- taskModel.count = curAlbumModel.count;
- //taskModel.result = curAlbumModel.result;
-
- taskModel.failCount = 0;
- taskModel.didBackupsCount = 0;
- taskModel.bg_tableName = backups_photos_tableName;
-
- NSMutableString *allLocalIdentifier= [NSMutableString new];
-
- for (PHAsset *asset in curAlbumModel.result) {
- if (asset.localIdentifier) {
- if(allLocalIdentifier.length >0){
- [allLocalIdentifier appendString:@"&"];
- }
- [allLocalIdentifier appendString:asset.localIdentifier];
- }
- }
-
- taskModel.totalLocalIdentifier = allLocalIdentifier;
-
- HLog(@"2 bg_id:%@",taskModel.bg_id);
-
- [taskModel bg_saveAsync:^(BOOL isSuccess) {
- if(!isSuccess){
- HLog(@"\n\nError!!!\n\n");
- }
- else{
- //[self handelBackupsModelFun:taskModel];
- [self handlePhotosBackupsFun];
- }
- }];
- }
- }];
- }
- - (void)handelBackupsModelFun:(photosBackupsTaskModel*)backupsTaskModel
- {
- _curPhotosBackupsTaskMod = backupsTaskModel;
- _curPhotosBackupsTaskMod.isBackupsSuspendType = backupsSuspendNone;
- _curPhotosBackupsTaskMod.curBackupsState = backupsStateUploading;
-
- _curPhotosBackupsTaskMod.LocalIdentifierArr = [backupsTaskModel.totalLocalIdentifier componentsSeparatedByString:@"&"];
-
- if(_curPhotosBackupsTaskMod.LocalIdentifierArr.count
- <= (_curPhotosBackupsTaskMod.didBackupsCount + _curPhotosBackupsTaskMod.failCount))
- {
- _curPhotosBackupsTaskMod.curBackupsState = backupsStateDone;
- [self RefreshDatabaseFun];
- [self changeBackupsFileStateWithSpeedFun];
- return;
- }
-
- [self beginBackupsFileFun];
- }
- - (void)beginBackupsFileFun
- {
- NSInteger index = _curPhotosBackupsTaskMod.didBackupsCount + _curPhotosBackupsTaskMod.failCount;
-
- if(index >= _curPhotosBackupsTaskMod.LocalIdentifierArr.count){
- _curPhotosBackupsTaskMod.curBackupsState = backupsStateDone;
- [self RefreshDatabaseFun];
- [self changeBackupsFileStateWithSpeedFun];
- return;
- }
-
- NSString *curLocalIdentifier = _curPhotosBackupsTaskMod.LocalIdentifierArr[index];
-
- if(!curLocalIdentifier || curLocalIdentifier.length == 0)
- {
- [self getDataWrongToChangeFailFun];
- return;
- }
-
- PHFetchResult *fetchResult = [PHAsset fetchAssetsWithLocalIdentifiers:@[curLocalIdentifier] options:nil];
- PHAsset *curAsset = fetchResult.firstObject;
-
- _curPhotosBackupsTaskMod.filename = [curAsset valueForKey:@"filename"];
-
-
- KWeakSelf
- if(curAsset.mediaType == PHAssetMediaTypeImage){
- _curPhotosBackupsTaskMod.curUploadFileType = uploadFileTypeImage;
- [[PHImageManager defaultManager] requestImageDataForAsset:curAsset options:nil resultHandler:^(NSData * _Nullable imageData, NSString * _Nullable dataUTI, UIImageOrientation orientation, NSDictionary * _Nullable info) {
- // 直接得到最终的 NSData 数据
- if (imageData) {
- [weakSelf afterGetImageDataFun:imageData];
- }
- else{
- [weakSelf getDataWrongToChangeFailFun];
- }
- }];
- }
- else{
- _curPhotosBackupsTaskMod.curUploadFileType = uploadFileTypeVideo;
-
- //判断文件是否在app内
- BOOL didSaveFile = [cachesFileManager checkFileIsSaveState:_curPhotosBackupsTaskMod.filename withType:uploadFileTypeVideo];
-
- if(didSaveFile){
- [self afterGetVideoDataFun];
- return;
- }
-
- //真正的视频数据
- PHVideoRequestOptions *options = [[PHVideoRequestOptions alloc] init];
- options.version = PHVideoRequestOptionsVersionOriginal;
- [[PHImageManager defaultManager] requestAVAssetForVideo:curAsset options:options resultHandler:^(AVAsset *asset, AVAudioMix *audioMix, NSDictionary *info) {
- if ([asset isKindOfClass:[AVURLAsset class]]) {
-
- AVURLAsset* urlAsset = (AVURLAsset*)asset;
-
- BOOL isSuc = [cachesFileManager copyVideoItemAtPath:[urlAsset.URL path] fileName:self->_curPhotosBackupsTaskMod.filename error:nil];
-
- if (isSuc) {
- [weakSelf afterGetVideoDataFun];
- }
- else{
- [weakSelf getDataWrongToChangeFailFun];
- }
- }
- }];
- }
-
- }
- - (void)getDataWrongToChangeFailFun
- {
- [self deleteVideoFun];
- _curPhotosBackupsTaskMod.failCount += 1;
-
- [self RefreshDatabaseFun];
- [self beginBackupsFileFun];
- }
- - (void)RefreshDatabaseFun
- {
- HLog(@"3 bg_id:%@",_curPhotosBackupsTaskMod.bg_id);
- [_curPhotosBackupsTaskMod bg_saveOrUpdateAsync:^(BOOL isSuccess) {
- if(isSuccess){
- }
- }];
- }
- - (void)afterGetImageDataFun:(NSData*)imageData
- {
- _curPhotosBackupsTaskMod.imageData = imageData;
- _curPhotosBackupsTaskMod.totalBytes = [imageData length];
- [[NSNotificationCenter defaultCenter] postNotificationName:backupsFileBeginNotification object:_curPhotosBackupsTaskMod];
- }
- - (void)deleteVideoFun
- {
- if(_curPhotosBackupsTaskMod.curUploadFileType == uploadFileTypeVideo){
- [cachesFileManager removeItemAtPath:_curPhotosBackupsTaskMod.filename type:uploadFileTypeVideo error:nil];
- }
- }
- - (void)afterGetVideoDataFun
- {
- [[NSNotificationCenter defaultCenter] postNotificationName:backupsFileBeginNotification object:_curPhotosBackupsTaskMod];
- }
- - (void)backupsFileDoneFun
- {
- [self deleteVideoFun];
- _curPhotosBackupsTaskMod.didBackupsCount += 1;
- [self RefreshDatabaseFun];
- [self beginBackupsFileFun];
-
- }
- //修改文件上传的状态
- - (void)changeBackupsFileStateWithSpeedFun
- {
- [[NSNotificationCenter defaultCenter] postNotificationName:backupsFileRefreshNotification object:_curPhotosBackupsTaskMod];
- }
- - (void)suspendBackupsFileFun
- {
- if(!_curPhotosBackupsTaskMod){
- return;
- }
-
- _curPhotosBackupsTaskMod.curBackupsState = backupsStateSuspend;
- _curPhotosBackupsTaskMod.backupsTipMsg = NSLocalizedString(@"File_backups_Record_tip_Suspend",nil);
-
- [_curPhotosBackupsTaskMod bg_saveOrUpdateAsync:^(BOOL isSuccess) {
- if(isSuccess){
- [self changeBackupsFileStateWithSpeedFun];
- }
- }];
-
- }
- //文件重新备份
- - (void)reBackupsFileFunBy:(photosBackupsTaskModel*)model
- {
- model.isBackupsSuspendType = backupsSuspendNone;
- model.curBackupsState = backupsStateUploading;
- _curPhotosBackupsTaskMod = model;
-
- [_curPhotosBackupsTaskMod bg_saveOrUpdateAsync:^(BOOL isSuccess) {
- if(isSuccess){
- [self beginBackupsFileFun];
- }
- }];
-
- }
- //检查是否需要重新备份
- - (void)checkReBackupsFileFun
- {
- BOOL haveOpenBackups = [HWDataManager getBoolWithKey:Const_photo_backups_state];
-
- if(!haveOpenBackups){
- return;
- }
-
- //相册权限
- if (![[TZImageManager manager] authorizationStatusAuthorized]){
- [HWDataManager setBoolWithKey:Const_photo_backups_state value:NO];
- return;
- }
-
-
- [photosBackupsTaskModel bg_findAsync:backups_photos_tableName limit:1 orderBy:nil desc:YES complete:^(NSArray * _Nullable array) {
- if(!array || array.count == 0){
- return;
- }
-
- photosBackupsTaskModel *lastModel = array.firstObject;
-
- HLog(@"1 bg_id:%@",lastModel.bg_id);
-
- if(lastModel.isBackupsSuspendType == backupsSuspendByUser){
- return;
- }
-
- if(lastModel.curBackupsState == backupsStateFail
- ||lastModel.curBackupsState == backupsStateDone){//上次备份完成
-
- }
- else{//继续上次的备份任务
- [self handelBackupsModelFun:lastModel];
- }
- }];
-
- }
- @end
|