Browse Source

review Automatic Upload

Marino Faggiana 8 years ago
parent
commit
fbc4e6c46c

+ 1 - 1
iOSClient/AppDelegate.h

@@ -169,7 +169,7 @@
 - (NSMutableArray *)verifyExistsInQueuesUploadSelector:(NSString *)selector;
 
 - (void)loadTableAutomaticUploadForSelector:(NSString *)selector;
-- (BOOL)createFolderSubFolderAutomaticUploadFolderPhotos:(NSString *)folderPhotos useSubFolder:(BOOL)useSubFolder assets:(NSArray *)assets;
+- (BOOL)createFolderSubFolderAutomaticUploadFolderPhotos:(NSString *)folderPhotos useSubFolder:(BOOL)useSubFolder assets:(NSArray *)assets selector:(NSString *)selector;
 - (void)dropAutomaticUploadWithSelector:(NSString *)selector;
 
 @end

+ 28 - 12
iOSClient/AppDelegate.m

@@ -289,10 +289,10 @@
     dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 5.0 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
         
         if ([CCCoreData countTableAutomaticUploadForAccount:self.activeAccount selector:selectorUploadAutomatic] > 0)
-            [app loadTableAutomaticUploadForSelector:selectorUploadAutomatic];
-    
+            [self performSelectorOnMainThread:@selector(loadTableAutomaticUploadForSelector:) withObject:selectorUploadAutomatic waitUntilDone:NO];
+        
         if ([CCCoreData countTableAutomaticUploadForAccount:self.activeAccount selector:selectorUploadAutomaticAll] > 0)
-            [app loadTableAutomaticUploadForSelector:selectorUploadAutomaticAll];
+            [self performSelectorOnMainThread:@selector(loadTableAutomaticUploadForSelector:) withObject:selectorUploadAutomaticAll waitUntilDone:NO];
     });
 }
 
@@ -1255,6 +1255,23 @@
     
     // Add Network queue
     CCMetadataNet *metadataNet = [CCCoreData getTableAutomaticUploadForAccount:self.activeAccount selector:selector context:nil];
+
+    // For UploadAutomatic create the folder for Photos & if request the subfolders
+    if ([selector isEqualToString:selectorUploadAutomatic] && metadataNet) {
+        
+        NSString *folderPhotos = [CCCoreData getCameraUploadFolderNamePathActiveAccount:app.activeAccount activeUrl:app.activeUrl];
+        BOOL useSubFolder = [CCCoreData getCameraUploadCreateSubfolderActiveAccount:app.activeAccount];
+
+        PHFetchResult *result = [PHAsset fetchAssetsWithLocalIdentifiers:@[metadataNet.identifier] options:nil];
+
+        if (!result.count) {
+           [CCCoreData addActivityClient:metadataNet.fileName fileID:metadataNet.identifier action:k_activityDebugActionUpload selector:selector note:@"Internal error image/video not found" type:k_activityVerboseDefault verbose:k_activityVerboseHigh account:_activeAccount activeUrl:_activeUrl];
+            return;
+        }
+        
+        if(![self createFolderSubFolderAutomaticUploadFolderPhotos:folderPhotos useSubFolder:useSubFolder assets:[[NSArray alloc] initWithObjects:result[0], nil] selector:selectorUploadAutomatic])
+            return;
+    }
     
     if (metadataNet) {
         
@@ -1266,6 +1283,9 @@
             queue = app.netQueueUpload;
         
         [self addNetworkingOperationQueue:queue delegate:app.activeMain metadataNet:metadataNet];
+        
+        // Delete record
+        [CCCoreData deleteTableAutomaticUploadForAccount:self.activeAccount identifier:metadataNet.identifier context:nil];
     }
 }
 
@@ -1415,16 +1435,14 @@
     });
 }
 
-- (BOOL)createFolderSubFolderAutomaticUploadFolderPhotos:(NSString *)folderPhotos useSubFolder:(BOOL)useSubFolder assets:(NSArray *)assets
+- (BOOL)createFolderSubFolderAutomaticUploadFolderPhotos:(NSString *)folderPhotos useSubFolder:(BOOL)useSubFolder assets:(NSArray *)assets selector:(NSString *)selector
 {
     OCnetworking *ocNetworking = [[OCnetworking alloc] initWithDelegate:nil metadataNet:nil withUser:_activeUser withPassword:_activePassword withUrl:_activeUrl isCryptoCloudMode:NO];
 
     if(![ocNetworking automaticCreateFolderSync:folderPhotos]) {
         
-        // Activity & Message
-        NSString *description = NSLocalizedStringFromTable(@"_not_possible_create_folder_", @"Error", nil);
-        [app messageNotification:@"_error_" description:description visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeInfo];
-        [CCCoreData addActivityClient:@"" fileID:@"" action:k_activityDebugActionAutomaticUpload selector:@"" note:description type:k_activityTypeFailure verbose:k_activityVerboseDefault account:_activeAccount activeUrl:_activeUrl];
+        // Activity
+        [CCCoreData addActivityClient:@"" fileID:@"" action:k_activityDebugActionAutomaticUpload selector:selector note:NSLocalizedStringFromTable(@"_not_possible_create_folder_", @"Error", nil) type:k_activityTypeFailure verbose:k_activityVerboseDefault account:_activeAccount activeUrl:_activeUrl];
         
         return false;
     }
@@ -1436,10 +1454,8 @@
             
             if(![ocNetworking automaticCreateFolderSync:[NSString stringWithFormat:@"%@/%@", folderPhotos, dateSubFolder]]) {
                 
-                // Activity & Message
-                NSString *description = NSLocalizedString(@"_error_createsubfolders_upload_",nil);
-                [app messageNotification:@"_error_" description:description visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeInfo];
-                [CCCoreData addActivityClient:@"" fileID:@"" action:k_activityDebugActionAutomaticUpload selector:@"" note:description type:k_activityTypeFailure verbose:k_activityVerboseDefault account:_activeAccount activeUrl:_activeUrl];
+                // Activity
+                [CCCoreData addActivityClient:@"" fileID:@"" action:k_activityDebugActionAutomaticUpload selector:selector note:NSLocalizedString(@"_error_createsubfolders_upload_",nil) type:k_activityTypeFailure verbose:k_activityVerboseDefault account:_activeAccount activeUrl:_activeUrl];
                 
                 return false;
             }

+ 1 - 0
iOSClient/FileSystem/CCCoreData.h

@@ -191,6 +191,7 @@
 
 + (void)addTableAutomaticUpload:(CCMetadataNet *)metadataNet account:(NSString *)account;
 + (CCMetadataNet *)getTableAutomaticUploadForAccount:(NSString *)account selector:(NSString *)selector context:(NSManagedObjectContext *)context;
++ (void)deleteTableAutomaticUploadForAccount:(NSString *)account identifier:(NSString *)identifier context:(NSManagedObjectContext *)context;
 + (NSUInteger)countTableAutomaticUploadForAccount:(NSString *)account selector:(NSString *)selector;
 
 // ===== GPS =====

+ 14 - 4
iOSClient/FileSystem/CCCoreData.m

@@ -1533,7 +1533,7 @@
     
     if (record) {
     
-        CCMetadataNet *metadataNet = [[CCMetadataNet alloc] init];
+        CCMetadataNet *metadataNet = [CCMetadataNet new];
         
         metadataNet.action = actionUploadAsset;                             // Default
         metadataNet.identifier = record.identifier;
@@ -1545,15 +1545,25 @@
         metadataNet.session = record.session;
         metadataNet.taskStatus = k_taskStatusResume;                        // Default
         
-        [record MR_deleteEntityInContext:context];                          // Remove record
-        [context MR_saveToPersistentStoreAndWait];
-        
         return metadataNet;
     }
     
     return nil;
 }
 
++ (void)deleteTableAutomaticUploadForAccount:(NSString *)account identifier:(NSString *)identifier context:(NSManagedObjectContext *)context
+{
+    if (context == nil)
+        context = [NSManagedObjectContext MR_context];
+    
+    TableAutomaticUpload *record = [TableAutomaticUpload MR_findFirstWithPredicate:[NSPredicate predicateWithFormat:@"(account == %@) AND (identifier == %@)", account, identifier] inContext:context];
+    
+    if (record) {
+        [record MR_deleteEntityInContext:context];
+        [context MR_saveToPersistentStoreAndWait];
+    }
+}
+
 + (NSUInteger)countTableAutomaticUploadForAccount:(NSString *)account selector:(NSString *)selector
 {
     if (selector)

+ 6 - 6
iOSClient/Main/CCMain.m

@@ -1542,8 +1542,8 @@
 {
     // Automatic upload
     if([selector isEqualToString:selectorUploadAutomatic] || [selector isEqualToString:selectorUploadAutomaticAll])
-        [app loadTableAutomaticUploadForSelector:selector];
-
+        [app performSelectorOnMainThread:@selector(loadTableAutomaticUploadForSelector:) withObject:selector waitUntilDone:NO];
+    
     // Read File test do not exists
     if (errorCode == k_CCErrorFileUploadNotFound && fileID) {
        
@@ -1567,7 +1567,7 @@
 {
     // Automatic upload
     if([selector isEqualToString:selectorUploadAutomatic] || [selector isEqualToString:selectorUploadAutomaticAll])
-        [app loadTableAutomaticUploadForSelector:selector];
+        [app performSelectorOnMainThread:@selector(loadTableAutomaticUploadForSelector:) withObject:selector waitUntilDone:NO];
     
     if ([selectorPost isEqualToString:selectorReadFolderForced] ) {
             
@@ -1595,7 +1595,7 @@
     NSString *serverUrl = [arguments objectAtIndex:1];
     BOOL cryptated = [[arguments objectAtIndex:2] boolValue];
     BOOL useSubFolder = [[arguments objectAtIndex:3] boolValue];
-    NSString * session = [arguments objectAtIndex:4];
+    NSString *session = [arguments objectAtIndex:4];
     
     // remove title (graphics)
     [self setTitleBackgroundTableView:nil];
@@ -1603,8 +1603,8 @@
     NSString *folderPhotos = [CCCoreData getCameraUploadFolderNamePathActiveAccount:app.activeAccount activeUrl:app.activeUrl];
     NSString *directoryID = [CCCoreData getDirectoryIDFromServerUrl:serverUrl activeAccount:app.activeAccount];
     
-    // Create if request the folder for Photos & if request the subfolders
-    if (![app createFolderSubFolderAutomaticUploadFolderPhotos:folderPhotos useSubFolder:useSubFolder assets:assets])
+    // Create the folder for Photos & if request the subfolders
+    if (![app createFolderSubFolderAutomaticUploadFolderPhotos:folderPhotos useSubFolder:useSubFolder assets:assets selector:selectorUploadFile])
         return;
     
     NSLog(@"[LOG] Asset N. %lu", (unsigned long)[assets count]);

+ 13 - 41
iOSClient/PhotosCameraUpload/CCPhotosCameraUpload.m

@@ -1143,9 +1143,7 @@
     NSMutableArray *newItemsPHAssetToUpload = [[NSMutableArray alloc] init];
     
     NSString *folderPhotos = [CCCoreData getCameraUploadFolderNamePathActiveAccount:app.activeAccount activeUrl:app.activeUrl];
-    BOOL createSubfolders = [CCCoreData getCameraUploadCreateSubfolderActiveAccount:app.activeAccount];
-    
-    OCnetworking *ocNetworking = [[OCnetworking alloc] initWithDelegate:nil metadataNet:nil withUser:app.activeUser withPassword:app.activePassword withUrl:app.activeUrl isCryptoCloudMode:NO];
+    BOOL useSubFolder = [CCCoreData getCameraUploadCreateSubfolderActiveAccount:app.activeAccount];
     
     // Conversion from ALAsset -to-> PHAsset
     for (ALAsset *asset in newItemsToUpload) {
@@ -1154,44 +1152,16 @@
         PHFetchResult *fetchResult = [PHAsset fetchAssetsWithALAssetURLs:@[url] options:nil];
         PHAsset *asset = [fetchResult firstObject];
         [newItemsPHAssetToUpload addObject:asset];
+        NSLog(@"Convert url %@", url);
     }
     
-    // verify/create folder Camera Upload, if error exit
-    if(![ocNetworking automaticCreateFolderSync:folderPhotos] && assetsFull) {
-        
-        NSString *description = NSLocalizedStringFromTable(@"_not_possible_create_folder_", @"Error", nil);
-        
-        // Full Upload ?
-        if (assetsFull)
-            [app messageNotification:@"_error_" description:description visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeInfo];
-        
-        // Activity
-        [CCCoreData addActivityClient:@"" fileID:@"" action:k_activityDebugActionAutomaticUpload selector:@"" note:description type:k_activityTypeFailure verbose:k_activityVerboseDefault account:app.activeAccount activeUrl:app.activeUrl];
-        
-        [self endLoadingAssets];
-        
-        return;
-    }
-    
-    // Use subfolders verify/create subfolder, if error exit
-    if (createSubfolders && assetsFull) {
+    // Create the folder for Photos & if request the subfolders
+    if (assetsFull) {
         
-        for (NSString *dateSubFolder in [CCUtility createNameSubFolder:newItemsPHAssetToUpload]) {
+        if(![app createFolderSubFolderAutomaticUploadFolderPhotos:folderPhotos useSubFolder:useSubFolder assets:newItemsPHAssetToUpload selector:selectorUploadAutomaticAll]) {
             
-            if (![ocNetworking automaticCreateFolderSync:[NSString stringWithFormat:@"%@/%@", folderPhotos, dateSubFolder]]) {
-                
-                [self endLoadingAssets];
-                
-                if (assetsFull)
-                    [app messageNotification:@"_error_" description:@"_error_createsubfolders_upload_" visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeInfo];
-                
-                // Activity
-                [CCCoreData addActivityClient:@"" fileID:@"" action:k_activityDebugActionAutomaticUpload selector:@"" note:NSLocalizedString(@"_error_createsubfolders_upload_",nil) type:k_activityTypeFailure verbose:k_activityVerboseDefault account:app.activeAccount activeUrl:app.activeUrl];
-                
-                [self endLoadingAssets];
-                
-                return;
-            }
+            [self endLoadingAssets];
+            return;
         }
     }
     
@@ -1210,7 +1180,7 @@
         if (assetMediaType == PHAssetMediaTypeImage && [CCCoreData getCameraUploadWWanPhotoActiveAccount:app.activeAccount]) session = k_upload_session_wwan;
         if (assetMediaType == PHAssetMediaTypeVideo && [CCCoreData getCameraUploadWWanVideoActiveAccount:app.activeAccount]) session = k_upload_session_wwan;
 
-        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
+        NSDateFormatter *formatter = [NSDateFormatter new];
         
         [formatter setDateFormat:@"yyyy"];
         NSString *yearString = [formatter stringFromDate:assetDate];
@@ -1218,7 +1188,7 @@
         [formatter setDateFormat:@"MM"];
         NSString *monthString = [formatter stringFromDate:assetDate];
 
-        if (createSubfolders)
+        if (useSubFolder)
             serverUrl = [NSString stringWithFormat:@"%@/%@/%@", folderPhotos, yearString, monthString];
         else
             serverUrl = folderPhotos;
@@ -1256,9 +1226,9 @@
     
     // start upload
     if (assetsFull)
-        [app loadTableAutomaticUploadForSelector:selectorUploadAutomaticAll];
+        [app performSelectorOnMainThread:@selector(loadTableAutomaticUploadForSelector:) withObject:selectorUploadAutomaticAll waitUntilDone:NO];
     else
-        [app loadTableAutomaticUploadForSelector:selectorUploadAutomatic];
+        [app performSelectorOnMainThread:@selector(loadTableAutomaticUploadForSelector:) withObject:selectorUploadAutomatic waitUntilDone:NO];
 
     // end loading
     [self endLoadingAssets];
@@ -1269,6 +1239,8 @@
 
 -(void)endLoadingAssets
 {
+    [_hud hideHud];
+    
     // START new request : initStateCameraUpload
     _AutomaticCameraUploadInProgress = NO;
     

+ 1 - 1
iOSClient/Utility/CCUtility.m

@@ -745,7 +745,7 @@
 
 + (NSArray *)createNameSubFolder:(NSArray *)alassets
 {
-    NSMutableOrderedSet *datesSubFolder = [[NSMutableOrderedSet alloc] init];
+    NSMutableOrderedSet *datesSubFolder = [NSMutableOrderedSet new];
     
     for (PHAsset *asset in alassets) {