|
@@ -0,0 +1,393 @@
|
|
|
+//
|
|
|
+// nasUploadFileManager.m
|
|
|
+// 双子星云手机
|
|
|
+//
|
|
|
+// Created by xd h on 2024/8/7.
|
|
|
+//
|
|
|
+
|
|
|
+#import "nasUploadFileManager.h"
|
|
|
+#import "nasUploadManager.h"
|
|
|
+
|
|
|
+@implementation nasUploadFileManager
|
|
|
++ (instancetype)shareInstance {
|
|
|
+ static nasUploadFileManager *_instance;
|
|
|
+ static dispatch_once_t onceToken;
|
|
|
+ dispatch_once(&onceToken, ^{
|
|
|
+ _instance = [[self alloc] init];
|
|
|
+ });
|
|
|
+ return _instance;
|
|
|
+}
|
|
|
+
|
|
|
+#pragma mark 读取数据库数据
|
|
|
+- (void)getDataInDatabaseFun:(BOOL)isReGet complete:(custom_complete_Arr)complete
|
|
|
+{
|
|
|
+ if(_databaseArr && _databaseArr.count == 3 && !isReGet){
|
|
|
+ complete(_databaseArr);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ if(!_databaseArr)
|
|
|
+ {
|
|
|
+ _databaseArr = [NSMutableArray new];
|
|
|
+ }
|
|
|
+
|
|
|
+ //KWeakSelf
|
|
|
+ dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{
|
|
|
+// NSLock *lock = [NSLock new];
|
|
|
+// [lock lock];
|
|
|
+ [uploadFileDataModel bg_findAsync:nasUpLoadFile_tableName limit:0 orderBy:nil desc:YES complete:^(NSArray * _Nullable array) {
|
|
|
+ NSMutableArray *failArr = [NSMutableArray new];
|
|
|
+ NSMutableArray *doneArr = [NSMutableArray new];
|
|
|
+ NSMutableArray *otherArr = [NSMutableArray new];
|
|
|
+
|
|
|
+ for (uploadFileDataModel * curModel in array) {
|
|
|
+
|
|
|
+ //图片 和视频 还原
|
|
|
+ if(curModel.curUploadFileType == uploadFileTypeImage){
|
|
|
+
|
|
|
+ }
|
|
|
+ else if(curModel.curUploadFileType == uploadFileTypeVideo){
|
|
|
+
|
|
|
+ if(curModel.curUploadStateType != uploadStateDone){
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if(curModel.curUploadStateType == uploadStateFail){
|
|
|
+ [failArr addObject:curModel];
|
|
|
+ }
|
|
|
+ else if(curModel.curUploadStateType == uploadStateDone){
|
|
|
+ [doneArr addObject:curModel];
|
|
|
+ }
|
|
|
+ else{
|
|
|
+ [otherArr addObject:curModel];
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ NSLock *lock = [NSLock new];
|
|
|
+ [lock lock];
|
|
|
+
|
|
|
+ NSMutableArray *newArr = [NSMutableArray new];
|
|
|
+ [newArr addObject:otherArr];
|
|
|
+ [newArr addObject:doneArr];
|
|
|
+ [newArr addObject:failArr];
|
|
|
+ self->_databaseArr = newArr;
|
|
|
+
|
|
|
+ [lock unlock];
|
|
|
+
|
|
|
+ complete(self->_databaseArr);
|
|
|
+ }];
|
|
|
+// [lock unlock];
|
|
|
+ });
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+//把TZAssetModel 转成 我们需要上传的model
|
|
|
+- (void)handlTZAssetModelToUploadFileDataFunBy:(NSMutableArray*)indexPathsForSelectedItems complete:(custom_complete_Arr)complete
|
|
|
+{
|
|
|
+ if(!indexPathsForSelectedItems && indexPathsForSelectedItems.count == 0){
|
|
|
+ complete(_databaseArr);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ if(ksharedAppDelegate.DisabledFileTransferType){
|
|
|
+ if(ksharedAppDelegate.isImageNewFor130){
|
|
|
+ [[iToast makeText:NSLocalizedString(@"File_Transfer_Disable_tip",nil)] show];
|
|
|
+ }
|
|
|
+ else{
|
|
|
+ [[iToast makeText:NSLocalizedString(@"File_Transfer_Disable_tip2",nil)] show];
|
|
|
+ }
|
|
|
+
|
|
|
+ complete(_databaseArr);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ if(!_fileModelDataArr){
|
|
|
+ _fileModelDataArr = [NSMutableArray new];
|
|
|
+ }
|
|
|
+
|
|
|
+ _isNewAddTaskType = YES;
|
|
|
+
|
|
|
+ self.curUploadModelNumbers = indexPathsForSelectedItems.count;
|
|
|
+
|
|
|
+ NSMutableArray *newUploadTaskArr = [NSMutableArray new];
|
|
|
+
|
|
|
+ NSLock *lock = [NSLock new];
|
|
|
+
|
|
|
+ for (TZAssetModel * model in indexPathsForSelectedItems) {
|
|
|
+ uploadFileDataModel * curModel = [uploadFileDataModel new];
|
|
|
+ curModel.asset = model.asset;
|
|
|
+ curModel.localIdentifier = model.asset.localIdentifier;
|
|
|
+
|
|
|
+ BOOL isRepeatingTasksType = NO;
|
|
|
+ [lock lock];
|
|
|
+ //查询上传任务是否存在
|
|
|
+ if(_databaseArr && _databaseArr.count == 3 ){
|
|
|
+ NSMutableArray *uploadingArr = [[NSMutableArray alloc] initWithArray:_databaseArr[0]];
|
|
|
+ for (uploadFileDataModel *preModel in uploadingArr) {
|
|
|
+ if([preModel.localIdentifier isEqualToString:curModel.localIdentifier]){
|
|
|
+ isRepeatingTasksType = YES;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ [lock unlock];
|
|
|
+
|
|
|
+ if(isRepeatingTasksType){
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ NSString * uploadDefaultPath = [HWDataManager getStringWithKey:stringKeyAddSn(Const_photo_upload_default_path)];
|
|
|
+ if(!uploadDefaultPath || uploadDefaultPath.length == 0){
|
|
|
+ [HWDataManager setStringWithKey:stringKeyAddSn(Const_photo_upload_default_path) value:Const_default_upload_path];
|
|
|
+ uploadDefaultPath = Const_default_upload_path;
|
|
|
+ }
|
|
|
+
|
|
|
+ //判断最后一个是否是/
|
|
|
+ NSString *lastStr= [uploadDefaultPath substringFromIndex:uploadDefaultPath.length-1];
|
|
|
+ if(![lastStr isEqualToString:@"/"]){
|
|
|
+ uploadDefaultPath = [[NSString alloc] initWithFormat:@"%@/",uploadDefaultPath];
|
|
|
+ }
|
|
|
+
|
|
|
+ //NSString*savePath = [[NSString alloc] initWithFormat:@"%@%@",uploadDefaultPath,filePath];
|
|
|
+ curModel.savePath = uploadDefaultPath;
|
|
|
+
|
|
|
+// curModel.imageData = model.imageData;
|
|
|
+// curModel.videoData = model.videoData;
|
|
|
+ curModel.filename = [model.asset valueForKey:@"filename"];
|
|
|
+ //curModel.curUploadStateType = uploadStateWait;
|
|
|
+ curModel.curUploadStateType = uploadStateUploading;
|
|
|
+
|
|
|
+ if(model.type == TZAssetModelMediaTypeVideo){
|
|
|
+ curModel.curUploadFileType = uploadFileTypeVideo;
|
|
|
+ [cachesFileManager getFileNameWithContent:curModel.videoData fileName:curModel.filename type:uploadFileTypeVideo];
|
|
|
+
|
|
|
+
|
|
|
+ //curModel.totalBytes = [model.videoData length];
|
|
|
+ curModel.totalBytes = model.totalBytes;
|
|
|
+ curModel.videoData = [NSData new];//视频文件存储到文件后内存清空
|
|
|
+
|
|
|
+ NSString *imgName1 = [curModel.filename stringByReplacingOccurrencesOfString:@"." withString:@"_"];
|
|
|
+ curModel.videoFirstImageName = [[NSString alloc] initWithFormat:@"%@.png",imgName1];
|
|
|
+
|
|
|
+ //第一帧图片
|
|
|
+ [[PHImageManager defaultManager] requestImageDataForAsset:curModel.asset options:nil resultHandler:^(NSData * _Nullable imageData, NSString * _Nullable dataUTI, UIImageOrientation orientation, NSDictionary * _Nullable info) {
|
|
|
+ // 直接得到最终的 NSData 数据
|
|
|
+ if (imageData) {
|
|
|
+ curModel.imageData = imageData;
|
|
|
+ [cachesFileManager getFileNameWithContent:curModel.imageData fileName:curModel.videoFirstImageName type:uploadFileTypeImage];
|
|
|
+ }
|
|
|
+ }];
|
|
|
+
|
|
|
+ }
|
|
|
+ else{
|
|
|
+ curModel.curUploadFileType = uploadFileTypeImage;
|
|
|
+ curModel.totalBytes = model.totalBytes;
|
|
|
+
|
|
|
+ if(curModel.imageData)
|
|
|
+ {
|
|
|
+ [cachesFileManager getFileNameWithContent:curModel.imageData fileName:curModel.filename type:uploadFileTypeImage];
|
|
|
+ }
|
|
|
+ else{
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ //保存到数据库
|
|
|
+ curModel.bg_tableName = nasUpLoadFile_tableName;
|
|
|
+ [newUploadTaskArr addObject:curModel];
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ KWeakSelf
|
|
|
+ if(newUploadTaskArr.count == 0){
|
|
|
+ [self getDataInDatabaseFun:YES complete:^(NSMutableArray * _Nonnull Arr) {
|
|
|
+ complete(Arr);
|
|
|
+ [weakSelf handleUploadFileModelBg_idFun:Arr];
|
|
|
+ }];
|
|
|
+ }
|
|
|
+ else{
|
|
|
+ [uploadFileDataModel bg_saveOrUpdateArrayAsync:newUploadTaskArr complete:^(BOOL isSuccess) {
|
|
|
+ [weakSelf getDataInDatabaseFun:YES complete:^(NSMutableArray * _Nonnull Arr) {
|
|
|
+ complete(Arr);
|
|
|
+ [weakSelf handleUploadFileModelBg_idFun:Arr];
|
|
|
+ }];
|
|
|
+ }];
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+#pragma mark 处理当前的model 加上bg_id
|
|
|
+- (void)handleUploadFileModelBg_idFun:(NSMutableArray*)totalArr
|
|
|
+{
|
|
|
+ if(!totalArr || totalArr.count != 3){
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ _fileModelDataArr = [NSMutableArray arrayWithArray:totalArr[0]];
|
|
|
+
|
|
|
+ [self beginUploadFileFun];
|
|
|
+}
|
|
|
+
|
|
|
+- (void)beginUploadFileFun
|
|
|
+{
|
|
|
+// if(_isUploadIngType && _reUploadIngSelectIndex <= 0){
|
|
|
+// return;
|
|
|
+// }
|
|
|
+
|
|
|
+ BOOL isCanUseCellular = [HWDataManager getBoolWithKey:stringKeyAddSn(Const_file_Transfe_canUse_Cellular_all)];
|
|
|
+ if(!isCanUseCellular){//不允许流量上传
|
|
|
+ //
|
|
|
+ if([AFNetworkReachabilityManager sharedManager].networkReachabilityStatus == AFNetworkReachabilityStatusReachableViaWWAN){
|
|
|
+ mainBlock(^{
|
|
|
+ [[NSNotificationCenter defaultCenter] postNotificationName:uploadFileSuspendAllNotification object:nil];
|
|
|
+ [[iToast makeText:NSLocalizedString(@"File_Transfer_By_Cellular_tip",nil)] show];
|
|
|
+ });
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ if(ksharedAppDelegate.DisabledFileTransferType){
|
|
|
+ if(ksharedAppDelegate.isImageNewFor130){
|
|
|
+ [[iToast makeText:NSLocalizedString(@"File_Transfer_Disable_tip",nil)] show];
|
|
|
+ }
|
|
|
+ else{
|
|
|
+ [[iToast makeText:NSLocalizedString(@"File_Transfer_Disable_tip2",nil)] show];
|
|
|
+ }
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ if(_isSuspendType)
|
|
|
+ {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ _isUploadIngType = YES;
|
|
|
+
|
|
|
+
|
|
|
+ _curUploadFileDataModel.imageData = nil;
|
|
|
+ _curUploadFileDataModel.videoData = nil;
|
|
|
+ _curUploadFileDataModel = nil;
|
|
|
+
|
|
|
+ if(_fileModelDataArr.count == 0){
|
|
|
+ //[self didUploadAllTaskDoneFun];
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ if(_reUploadIngSelectIndex > 0 && _reUploadIngSelectIndex < _fileModelDataArr.count){
|
|
|
+ _curUploadFileDataModel = _fileModelDataArr[_reUploadIngSelectIndex];
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ _curUploadFileDataModel = _fileModelDataArr.firstObject;
|
|
|
+ }
|
|
|
+
|
|
|
+ //做个异常处理
|
|
|
+ if(_databaseArr.count == 3 && _isNewAddTaskType){
|
|
|
+ NSMutableArray *uploadIngArr = _databaseArr[0];
|
|
|
+ if(_fileModelDataArr.count != uploadIngArr.count){
|
|
|
+ _databaseArr[0] = [NSMutableArray arrayWithArray:_fileModelDataArr];
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if(!_curUploadFileDataModel){
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ if(_curUploadFileDataModel.curUploadFileType == uploadFileTypeImage){
|
|
|
+ NSString*pathStr = [cachesFileManager getFilePathWithName:_curUploadFileDataModel.filename type:_curUploadFileDataModel.curUploadFileType];
|
|
|
+ _curUploadFileDataModel.imageData = [NSData dataWithContentsOfFile:pathStr];
|
|
|
+
|
|
|
+ if(_curUploadFileDataModel.imageData && _curUploadFileDataModel.imageData.length >0){
|
|
|
+ if(_curUploadFileDataModel.totalBytes == 0){
|
|
|
+ _curUploadFileDataModel.totalBytes = _curUploadFileDataModel.imageData.length;
|
|
|
+ }
|
|
|
+
|
|
|
+ [self beginGotoUploadDataFun];
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else{
|
|
|
+ if([cachesFileManager checkFileIsSaveState:_curUploadFileDataModel.filename withType:uploadFileTypeVideo]){
|
|
|
+ [self beginGotoUploadDataFun];
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if(!_curUploadFileDataModel.asset){
|
|
|
+ NSString *curLocalIdentifier = _curUploadFileDataModel.localIdentifier;
|
|
|
+ PHFetchResult *fetchResult = [PHAsset fetchAssetsWithLocalIdentifiers:@[curLocalIdentifier] options:nil];
|
|
|
+ PHAsset *asset = fetchResult.firstObject;
|
|
|
+ _curUploadFileDataModel.asset = asset;
|
|
|
+ }
|
|
|
+
|
|
|
+ if(!_curUploadFileDataModel.asset){
|
|
|
+ //[self getDataWrongToChangeFailFun];
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ KWeakSelf
|
|
|
+ if(_curUploadFileDataModel.curUploadFileType == uploadFileTypeImage)
|
|
|
+ {
|
|
|
+ if(!_curUploadFileDataModel.imageData || _curUploadFileDataModel.imageData.length == 0){
|
|
|
+ [[PHImageManager defaultManager] requestImageDataForAsset:_curUploadFileDataModel.asset options:nil resultHandler:^(NSData * _Nullable imageData, NSString * _Nullable dataUTI, UIImageOrientation orientation, NSDictionary * _Nullable info) {
|
|
|
+ // 直接得到最终的 NSData 数据
|
|
|
+ if (imageData) {
|
|
|
+ self->_curUploadFileDataModel.imageData = imageData;
|
|
|
+ [weakSelf afterGetImageDataFun];
|
|
|
+ }
|
|
|
+ else{
|
|
|
+ //[weakSelf getDataWrongToChangeFailFun];
|
|
|
+ }
|
|
|
+ }];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else if(_curUploadFileDataModel.curUploadFileType == uploadFileTypeVideo){
|
|
|
+
|
|
|
+ //第一帧图片
|
|
|
+ [[PHImageManager defaultManager] requestImageDataForAsset:_curUploadFileDataModel.asset options:nil resultHandler:^(NSData * _Nullable imageData, NSString * _Nullable dataUTI, UIImageOrientation orientation, NSDictionary * _Nullable info) {
|
|
|
+ // 直接得到最终的 NSData 数据
|
|
|
+ if (imageData) {
|
|
|
+ self->_curUploadFileDataModel.imageData = imageData;
|
|
|
+ //[weakSelf afterGetImageDataInVideoFun];
|
|
|
+ }
|
|
|
+ }];
|
|
|
+
|
|
|
+ //真正的视频数据
|
|
|
+ PHVideoRequestOptions *options = [[PHVideoRequestOptions alloc] init];
|
|
|
+ options.version = PHVideoRequestOptionsVersionOriginal;
|
|
|
+ [[PHImageManager defaultManager] requestAVAssetForVideo:_curUploadFileDataModel.asset 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->_curUploadFileDataModel.filename error:nil];
|
|
|
+
|
|
|
+ if (isSuc) {
|
|
|
+ //[weakSelf afterGetVideoDataFun];
|
|
|
+ }
|
|
|
+ else{
|
|
|
+ //[weakSelf getDataWrongToChangeFailFun];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else{
|
|
|
+ //[weakSelf getDataWrongToChangeFailFun];
|
|
|
+ }
|
|
|
+ }];
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+#pragma mark 根据 asset 获取到图片数据
|
|
|
+- (void)afterGetImageDataFun
|
|
|
+{
|
|
|
+ [cachesFileManager getFileNameWithContent:_curUploadFileDataModel.imageData fileName:_curUploadFileDataModel.filename type:uploadFileTypeImage];
|
|
|
+
|
|
|
+ [self beginGotoUploadDataFun];
|
|
|
+}
|
|
|
+
|
|
|
+- (void)beginGotoUploadDataFun{
|
|
|
+ [[nasUploadManager shareInstance] beginUploadDataBy:_curUploadFileDataModel];
|
|
|
+}
|
|
|
+
|
|
|
+@end
|