Browse Source

fix share

Marino Faggiana 6 years ago
parent
commit
d9b5635d83

+ 0 - 3
Share/CCloadItemData.swift

@@ -88,9 +88,6 @@ class CCloadItemData: NSObject {
                                         print("item as url: \(String(describing: item))")
                                         
                                         let fileName = url.lastPathComponent
-                                        // OLD fileName with date
-                                        //let pathExtention = URL(fileURLWithPath: url.lastPathComponent).pathExtension
-                                        //let fileName = "\(dateFormatter.string(from: Date()))\(conuter).\(pathExtention)"
                                         let filenamePath = directory + fileName
                                         
                                         do {

+ 1 - 1
Share/ShareViewController.h

@@ -35,7 +35,7 @@
 #import "CCError.h"
 #import "CCHud.h"
 
-@interface ShareViewController : UIViewController <UITableViewDelegate, MBProgressHUDDelegate, BKPasscodeViewControllerDelegate, CCNetworkingDelegate, CCMoveDelegate>
+@interface ShareViewController : UIViewController <UITableViewDelegate, MBProgressHUDDelegate, BKPasscodeViewControllerDelegate, CCMoveDelegate>
 
 @property (nonatomic, strong) NSString *activeAccount;
 @property (nonatomic, strong) NSString *activeUrl;

+ 38 - 77
Share/ShareViewController.m

@@ -84,14 +84,11 @@
             [CCUtility setTitleServerUrlExt:NSLocalizedString(@"_home_", nil)];
         }
     }
-
-    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(triggerProgressTask:) name:@"NotificationProgressTask" object:nil];
     
-    _filesName = [[NSMutableArray alloc] init];
-    _hud = [[CCHud alloc] initWithView:self.navigationController.view];
+    self.filesName = [[NSMutableArray alloc] init];
+    
+    self.hud = [[CCHud alloc] initWithView:self.navigationController.view];
     
-    [CCNetworking sharedNetworking].delegate = self;
-        
     [self.shareTable registerNib:[UINib nibWithNibName:@"CCCellShareExt" bundle:nil] forCellReuseIdentifier:@"ShareExtCell"];
     
     [self navigationBarToolBar];
@@ -205,32 +202,46 @@
 {
     if ([self.filesName count] > 0) {
     
-        NSString *fileName = [[NCUtility sharedInstance] createFileName:[self.filesName objectAtIndex:0] serverUrl:self.serverUrl account:self.activeAccount];
-        
-        tableMetadata *metadataForUpload = [tableMetadata new];
+        [self.hud visibleHudTitle:NSLocalizedString(@"_uploading_", nil) mode:MBProgressHUDModeDeterminate color:[NCBrandColor sharedInstance].brandElement];
         
-        metadataForUpload.account = self.activeAccount;
-        metadataForUpload.date = [NSDate new];
-        metadataForUpload.fileID = [CCUtility createMetadataIDFromAccount:self.activeAccount serverUrl:self.serverUrl fileNameView:fileName directory:false];
-        metadataForUpload.fileName = fileName;
-        metadataForUpload.fileNameView = fileName;
-        metadataForUpload.serverUrl = self.serverUrl;
-        metadataForUpload.session = k_upload_session_foreground;
-        metadataForUpload.sessionSelector = selectorUploadFile;
-        metadataForUpload.status = k_metadataStatusWaitUpload;
+        NSString *fileName = [[NCUtility sharedInstance] createFileName:[self.filesName objectAtIndex:0] serverUrl:self.serverUrl account:self.activeAccount];
         
-        // Prepare file and directory
-        [CCUtility copyFileAtPath:[NSTemporaryDirectory() stringByAppendingString:[self.filesName objectAtIndex:0]] toPath:[CCUtility getDirectoryProviderStorageFileID:metadataForUpload.fileID fileNameView:fileName]];
-        [self.filesName replaceObjectAtIndex:0 withObject:fileName];
+        [[OCNetworking sharedManager] uploadWithAccount:self.activeAccount fileNameServerUrl:[NSString stringWithFormat:@"%@/%@", self.serverUrl, fileName] fileNameLocalPath:[NSTemporaryDirectory() stringByAppendingString:[self.filesName objectAtIndex:0]] progress:^(NSProgress *progress) {
+            
+            dispatch_async(dispatch_get_main_queue(), ^{
+                [self.hud progress:progress.fractionCompleted];
+            });
+            
+        } completion:^(NSString *account, NSString *fileID, NSString *etag, NSDate *date, NSString *message, NSInteger errorCode) {
+            
+            [self.hud hideHud];
+            
+            [self.filesName removeObject:fileName];
+
+            if (errorCode == 0) {
+                
+                [CCUtility copyFileAtPath:[NSTemporaryDirectory() stringByAppendingString:[self.filesName objectAtIndex:0]] toPath:[CCUtility getDirectoryProviderStorageFileID:fileID fileNameView:fileName]];
+                
+                [self.shareTable performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
+                [self performSelector:@selector(selectPost) withObject:nil];
+                
+            } else {
+                
+                UIAlertController * alert= [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_error_", nil) message:message preferredStyle:UIAlertControllerStyleAlert];
+                UIAlertAction* ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
+                                                           handler:^(UIAlertAction * action) {
+                                                               [alert dismissViewControllerAnimated:YES completion:nil];
+                                                               [self closeShareViewController];
+                                                           }];
+                [alert addAction:ok];
+                [self presentViewController:alert animated:YES completion:nil];
+            }
+        }];
         
-        // Add Medtadata for upload
-        tableMetadata *metadata = [[NCManageDatabase sharedInstance] addMetadata:metadataForUpload];
-        [[CCNetworking sharedNetworking] uploadFile:metadata taskStatus:k_taskStatusResume];
+    } else {
         
-        [self.hud visibleHudTitle:NSLocalizedString(@"_uploading_", nil) mode:MBProgressHUDModeDeterminate color:[NCBrandColor sharedInstance].brandElement];
-    }
-    else
         [self closeShareViewController];
+    }
 }
 
 - (void)cancelPost
@@ -244,56 +255,6 @@
     [self closeShareViewController];
 }
 
-#pragma --------------------------------------------------------------------------------------------
-#pragma mark ======================= NetWorking ==================================
-#pragma --------------------------------------------------------------------------------------------
-
-- (void)triggerProgressTask:(NSNotification *)notification
-{
-    NSDictionary *dict = notification.userInfo;
-    float progress = [[dict valueForKey:@"progress"] floatValue];
-
-    [self.hud progress:progress];
-}
-
-- (void)uploadFileSuccessFailure:(NSString *)fileName fileID:(NSString *)fileID assetLocalIdentifier:(NSString *)assetLocalIdentifier serverUrl:(NSString *)serverUrl selector:(NSString *)selector errorMessage:(NSString *)errorMessage errorCode:(NSInteger)errorCode
-{    
-    [self.hud hideHud];
-
-    if (errorCode == 0) {
-        
-        tableMetadata *metadata = [[NCManageDatabase sharedInstance] getMetadataWithPredicate:[NSPredicate predicateWithFormat:@"fileID == %@", fileID]];
-        
-        [self.filesName removeObject:metadata.fileName];
-        [self.shareTable performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
-        
-        [self performSelector:@selector(selectPost) withObject:nil afterDelay:0.1];
-        
-    } else {
-        
-        // remove file
-        [[NCManageDatabase sharedInstance] deleteMetadataWithPredicate:[NSPredicate predicateWithFormat:@"fileID == %@", fileID]];
-        
-        [[NSFileManager defaultManager] removeItemAtPath:[NSTemporaryDirectory() stringByAppendingString:fileID] error:nil];
-        [[NSFileManager defaultManager] removeItemAtPath:[CCUtility getDirectoryProviderStorageFileID:fileID] error:nil];
-        
-        // message error
-        if (errorCode != kCFURLErrorCancelled) {
-            
-            UIAlertController * alert= [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_error_", nil) message:errorMessage preferredStyle:UIAlertControllerStyleAlert];
-            UIAlertAction* ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
-                                                       handler:^(UIAlertAction * action) {
-                                                           [alert dismissViewControllerAnimated:YES completion:nil];
-                                                           [self closeShareViewController];
-                                                       }];
-            [alert addAction:ok];
-            [self presentViewController:alert animated:YES completion:nil];
-        }
-        else
-            [self closeShareViewController];
-    }
-}
-
 #pragma --------------------------------------------------------------------------------------------
 #pragma mark ===== Lock Password =====
 #pragma --------------------------------------------------------------------------------------------

+ 1 - 1
iOSClient/Networking/OCNetworking.h

@@ -44,7 +44,7 @@
 
 - (NSURLSessionTask *)downloadWithAccount:(NSString *)account fileNameServerUrl:(NSString *)fileNameServerUrl fileNameLocalPath:(NSString *)fileNameLocalPath communication:(OCCommunication *)communication completion:(void (^)(NSString *account, int64_t length, NSString *etag, NSDate *date, NSString *message, NSInteger errorCode))completion;
 - (NSURLSessionTask *)downloadWithAccount:(NSString *)account url:(NSString *)url fileNameLocalPath:(NSString *)fileNameLocalPath completion:(void (^)(NSString *account, NSString *message, NSInteger errorCode))completion;
-- (NSURLSessionTask *)uploadWithAccount:(NSString *)account fileNameServerUrl:(NSString *)fileNameServerUrl fileNameLocalPath:(NSString *)fileNameLocalPath communication:(OCCommunication *)communication completion:(void(^)(NSString *account, NSString *fileID, NSString *etag, NSDate *date, NSString *message, NSInteger errorCode))completion;
+- (NSURLSessionTask *)uploadWithAccount:(NSString *)account fileNameServerUrl:(NSString *)fileNameServerUrl fileNameLocalPath:(NSString *)fileNameLocalPath progress:(void(^)(NSProgress *progress))uploadProgress completion:(void(^)(NSString *account, NSString *fileID, NSString *etag, NSDate *date, NSString *message, NSInteger errorCode))completion;
 
 #pragma mark ===== WebDav =====
 

+ 4 - 1
iOSClient/Networking/OCNetworking.m

@@ -362,17 +362,20 @@
     return sessionTask;
 }
 
-- (NSURLSessionTask *)uploadWithAccount:(NSString *)account fileNameServerUrl:(NSString *)fileNameServerUrl fileNameLocalPath:(NSString *)fileNameLocalPath communication:(OCCommunication *)communication completion:(void(^)(NSString *account, NSString *fileID, NSString *etag, NSDate *date, NSString *message, NSInteger errorCode))completion
+- (NSURLSessionTask *)uploadWithAccount:(NSString *)account fileNameServerUrl:(NSString *)fileNameServerUrl fileNameLocalPath:(NSString *)fileNameLocalPath progress:(void(^)(NSProgress *progress))uploadProgress completion:(void(^)(NSString *account, NSString *fileID, NSString *etag, NSDate *date, NSString *message, NSInteger errorCode))completion
 {    
     tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
     if (tableAccount == nil) {
         completion(account, nil, nil, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
     }
     
+    OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
+
     [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:tableAccount.password];
     [communication setUserAgent:[CCUtility getUserAgent]];
     
     NSURLSessionTask *sessionTask = [communication uploadFileSession:fileNameLocalPath toDestiny:fileNameServerUrl onCommunication:communication progress:^(NSProgress *progress) {
+        uploadProgress(progress);
         //float percent = roundf (progress.fractionCompleted * 100);
     } successRequest:^(NSURLResponse *response, NSString *redirectedServer) {