Browse Source

restruct autoUpload

Marino Faggiana 7 years ago
parent
commit
eb95edc94b

+ 23 - 39
iOSClient/AutoUpload/NCAutoUpload.m

@@ -334,10 +334,13 @@
      if (!app.activeAccount || app.maintenanceMode)
          return;
     
-    tableAccount *account = [[NCManageDatabase sharedInstance] getAccountActive];
-    
+    tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountActive];
+    BOOL useSubFolder = tableAccount.autoUploadCreateSubfolder;
+    NSMutableArray *metadataNetFull = [NSMutableArray new];
+    NSString *autoUploadPath = [[NCManageDatabase sharedInstance] getAccountAutoUploadPath:app.activeUrl];
+
     // Check Asset : NEW or FULL
-    PHFetchResult *newAssetToUpload = [self getCameraRollAssets:account assetsFull:assetsFull alignPhotoLibrary:NO];
+    PHFetchResult *newAssetToUpload = [self getCameraRollAssets:tableAccount assetsFull:assetsFull alignPhotoLibrary:NO];
     
     // News Assets ? if no verify if blocked Table Auto Upload -> Autostart
     if ([newAssetToUpload count] == 0) {
@@ -352,7 +355,6 @@
     
     dispatch_async(dispatch_get_main_queue(), ^{
         if (assetsFull) {
-        
             if (!_hud)
                 _hud = [[CCHud alloc] initWithView:[[[UIApplication sharedApplication] delegate] window]];
         
@@ -360,38 +362,12 @@
         }
     });
     
-    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.01 * NSEC_PER_SEC), dispatch_get_main_queue(), ^(void) {
-        if (assetsFull)
-            [self performSelectorOnMainThread:@selector(uploadFullAssetsToNetwork:) withObject:newAssetToUpload waitUntilDone:NO];
-        else
-            [self performSelectorOnMainThread:@selector(uploadNewAssetsToNetwork:) withObject:newAssetToUpload waitUntilDone:NO];
-    });
-}
-
-- (void)uploadNewAssetsToNetwork:(PHFetchResult *)newAssetToUpload
-{
-    [self uploadAssetsToNetwork:newAssetToUpload assetsFull:NO];
-}
-
-- (void)uploadFullAssetsToNetwork:(PHFetchResult *)newAssetToUpload
-{
-    [self uploadAssetsToNetwork:newAssetToUpload assetsFull:YES];
-}
-
-- (void)uploadAssetsToNetwork:(PHFetchResult *)newAssetToUpload assetsFull:(BOOL)assetsFull
-{
-    tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountActive];
-    NSMutableArray *metadataNetFull = [NSMutableArray new];
-  
-    NSString *autoUploadPath = [[NCManageDatabase sharedInstance] getAccountAutoUploadPath:app.activeUrl];
-    BOOL useSubFolder = tableAccount.autoUploadCreateSubfolder;
-    
     // Create the folder for Photos & if request the subfolders
     if(![[NCAutoUpload sharedInstance] createFolderSubFolderAutoUploadFolderPhotos:autoUploadPath useSubFolder:useSubFolder assets:newAssetToUpload selector:selectorUploadAutoUploadAll]) {
-        
-        // end loading
-        [_hud hideHud];
-        
+        dispatch_async(dispatch_get_main_queue(), ^{
+            // end loading
+            [_hud hideHud];
+        });
         return;
     }
     
@@ -458,11 +434,15 @@
         [[NCManageDatabase sharedInstance] addQueueUploadWithMetadatasNet:metadataNetFull];
         
         // Update icon badge number
-        [app updateApplicationIconBadgeNumber];
+        dispatch_async(dispatch_get_main_queue(), ^{
+            [app updateApplicationIconBadgeNumber];
+        });
     }
     
     // end loading
-    [_hud hideHud];
+    dispatch_async(dispatch_get_main_queue(), ^{
+        [_hud hideHud];
+    });
 }
 
 - (void)addQueueUploadAndPhotoLibrary:(CCMetadataNet *)metadataNet asset:(PHAsset *)asset
@@ -498,9 +478,11 @@
 
 - (BOOL)createFolderSubFolderAutoUploadFolderPhotos:(NSString *)folderPhotos useSubFolder:(BOOL)useSubFolder assets:(PHFetchResult *)assets selector:(NSString *)selector
 {
-    OCnetworking *ocNetworking = [[OCnetworking alloc] initWithDelegate:nil metadataNet:nil withUser:app.activeUser withUserID:app.activeUserID withPassword:app.activePassword withUrl:app.activeUrl];
+    NSError *error;
     
-    if ([ocNetworking automaticCreateFolderSync:folderPhotos]) {
+    error = [[NCNetworkingSync sharedManager] createFolderAutomaticUpload:folderPhotos user:app.activeUser userID:app.activeUserID password:app.activePassword];
+    
+    if (error == nil) {
         
         (void)[[NCManageDatabase sharedInstance] addDirectoryWithServerUrl:folderPhotos permissions:nil encrypted:false];
         
@@ -520,7 +502,9 @@
         
         for (NSString *dateSubFolder in [CCUtility createNameSubFolder:assets]) {
             
-            if ([ocNetworking automaticCreateFolderSync:[NSString stringWithFormat:@"%@/%@", folderPhotos, dateSubFolder]]) {
+            error = [[NCNetworkingSync sharedManager] createFolderAutomaticUpload:[NSString stringWithFormat:@"%@/%@", folderPhotos, dateSubFolder] user:app.activeUser userID:app.activeUserID password:app.activePassword];
+            
+            if ( error == nil) {
                 
                 (void)[[NCManageDatabase sharedInstance] addDirectoryWithServerUrl:[NSString stringWithFormat:@"%@/%@", folderPhotos, dateSubFolder] permissions:nil encrypted:false];
                 

+ 1 - 0
iOSClient/Networking/NCNetworkingSync.h

@@ -17,6 +17,7 @@
 - (NSError *)checkServer:(NSString *)serverUrl user:(NSString *)user userID:(NSString *)userID password:(NSString *)password;
 - (NSError *)readFile:(NSString *)filePathName user:(NSString *)user userID:(NSString *)userID password:(NSString *)password items:(NSArray  **)items;
 - (NSError *)readFolder:(NSString *)serverUrl user:(NSString *)user userID:(NSString *)userID password:(NSString *)password items:(NSArray  **)items;
+- (NSError *)createFolderAutomaticUpload:(NSString *)folderPathName user:(NSString *)user userID:(NSString *)userID password:(NSString *)password;
 
 // ===== End-to-End Encryption =====
 

+ 40 - 0
iOSClient/Networking/NCNetworkingSync.m

@@ -150,6 +150,46 @@
     return returnError;
 }
 
+- (NSError *)createFolderAutomaticUpload:(NSString *)folderPathName user:(NSString *)user userID:(NSString *)userID password:(NSString *)password
+{
+    OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
+    
+    __block NSError *returnError = nil;
+
+    dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
+    
+    [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
+    [communication setUserAgent:[CCUtility getUserAgent]];
+    
+    [communication readFile:folderPathName onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer) {
+        
+        dispatch_semaphore_signal(semaphore);
+        
+    } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
+        
+        [communication createFolder:folderPathName onCommunication:communication withForbiddenCharactersSupported:YES successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
+            
+            [[NCManageDatabase sharedInstance] clearDateReadWithServerUrl:[CCUtility deletingLastPathComponentFromServerUrl:folderPathName] directoryID:nil];
+            dispatch_semaphore_signal(semaphore);
+            
+        } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
+            
+            returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"Read folder error %lu", (unsigned long)response.statusCode] forKey:NSLocalizedDescriptionKey]];
+            dispatch_semaphore_signal(semaphore);
+            
+        } errorBeforeRequest:^(NSError *error) {
+            
+            returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"Read folder error %lu", (unsigned long)response.statusCode] forKey:NSLocalizedDescriptionKey]];
+            dispatch_semaphore_signal(semaphore);
+
+        }];
+    }];
+    
+    while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
+        [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
+    
+    return returnError;
+}
 #pragma --------------------------------------------------------------------------------------------
 #pragma mark ===== End-to-End Encryption =====
 #pragma --------------------------------------------------------------------------------------------

+ 0 - 1
iOSClient/Networking/OCNetworking.h

@@ -43,7 +43,6 @@
 @property (nonatomic, assign) BOOL isFinished;
 
 - (NSError *)readFileSync:(NSString *)filePathName;
-- (BOOL)automaticCreateFolderSync:(NSString *)folderPathName;
 
 @end
 

+ 0 - 41
iOSClient/Networking/OCNetworking.m

@@ -697,47 +697,6 @@
     }];
 }
 
-- (BOOL)automaticCreateFolderSync:(NSString *)folderPathName
-{
-    OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
-    __block BOOL noError = YES;
-    
-    dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
-    
-    [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
-    [communication setUserAgent:[CCUtility getUserAgent]];
-    
-    [communication readFile:folderPathName onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer) {
-        
-        dispatch_semaphore_signal(semaphore);
-        
-    } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
-        
-        [communication createFolder:folderPathName onCommunication:communication withForbiddenCharactersSupported:YES successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
-            
-            [[NCManageDatabase sharedInstance] clearDateReadWithServerUrl:[CCUtility deletingLastPathComponentFromServerUrl:folderPathName] directoryID:nil];
-            
-            dispatch_semaphore_signal(semaphore);
-            
-        } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
-            
-            noError = NO;
-            
-            dispatch_semaphore_signal(semaphore);
-            
-        } errorBeforeRequest:^(NSError *error) {
-            
-            noError = NO;
-            dispatch_semaphore_signal(semaphore);
-        }];
-    }];
-    
-    while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_NOW))
-        [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
-    
-    return noError;
-}
-
 #pragma --------------------------------------------------------------------------------------------
 #pragma mark =====  Delete =====
 #pragma --------------------------------------------------------------------------------------------