Browse Source

get Image preview with auth

Marino Faggiana 6 years ago
parent
commit
4586100cc8

+ 0 - 1
iOSClient/Library/OCCommunicationLib/NCRichDocumentTemplate.h

@@ -18,7 +18,6 @@ NS_ASSUME_NONNULL_BEGIN
 @property (nonatomic, strong) NSString *name;
 @property (nonatomic, strong) NSString *preview;
 @property (nonatomic, strong) NSString *type;
-@property (nonatomic, strong) UIImage *image;
 
 @end
 

+ 24 - 33
iOSClient/Main/Create cloud/NCCreateFormUploadRichdocuments.swift

@@ -132,7 +132,18 @@ class NCCreateFormUploadRichdocuments: XLFormViewController, NCSelectDelegate, U
         let imageView = cell.viewWithTag(100) as! UIImageView
         let name = cell.viewWithTag(200) as! UILabel
         
-        imageView.image = template.image
+        if template.preview != "" {
+            let fileNameLocalPath = CCUtility.getDirectoryUserData() + "/" + template.name + ".png"
+            if FileManager.default.fileExists(atPath: fileNameLocalPath) {
+                let imageURL = URL(fileURLWithPath: fileNameLocalPath)
+                if let image = UIImage(contentsOfFile: imageURL.path) {
+                    imageView.image = image
+                }
+            } else {
+                getImage(template: template, indexPath: indexPath)
+            }
+        }
+        
         name.text = template.name
 
         return cell
@@ -202,38 +213,6 @@ class NCCreateFormUploadRichdocuments: XLFormViewController, NCSelectDelegate, U
         ocNetworking?.createTemplateRichdocuments(withTemplate: typeTemplate, success: { (listOfTemplate) in
             
             self.listOfTemplate = listOfTemplate as! [NCRichDocumentTemplate]
-            for template: NCRichDocumentTemplate in self.listOfTemplate {
-                if let url = URL(string: template.preview.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!) {
-                    if let imageData = try? Data(contentsOf: url) {
-                        if let image = UIImage.init(data: imageData) {
-                            template.image = image
-                        }
-                    }
-                }
-                
-                /*
- self.listOfTemplate = listOfTemplate as! [NCRichDocumentTemplate]
- for template: NCRichDocumentTemplate in self.listOfTemplate {
- 
- if let url = URL(string: template.preview.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!) {
- let task = URLSession.shared.dataTask(with: NSURLRequest(url: url) as URLRequest, completionHandler: { data,response,error in
- if error == nil && data != nil && data!.count > 0 {
- if let image = UIImage.init(data: data!) {
- template.image = image
- self.collectionView.reloadData()
- }
- } else {
- print(error?.localizedDescription)
- }
- 
- })
- task.resume()
- }
- }
- */
-                
-            }
-            
             self.collectionView.reloadData()
             
         }, failure: { (message, errorCode) in
@@ -241,4 +220,16 @@ class NCCreateFormUploadRichdocuments: XLFormViewController, NCSelectDelegate, U
         })
     }
     
+    func getImage(template: NCRichDocumentTemplate, indexPath: IndexPath) {
+        
+        let ocNetworking = OCnetworking.init(delegate: nil, metadataNet: nil, withUser: appDelegate.activeUser, withUserID: appDelegate.activeUserID, withPassword: appDelegate.activePassword, withUrl: appDelegate.activeUrl)
+        
+        let fileNameLocalPath = CCUtility.getDirectoryUserData() + "/" + template.name + ".png"
+        
+        ocNetworking?.downloadFile(template.preview, fileNameLocalPath: fileNameLocalPath, success: {
+            self.collectionView.reloadItems(at: [indexPath])
+        }, failure: { (message, errorCode) in
+            print("\(errorCode)")
+        })
+    }
 }

+ 2 - 0
iOSClient/Networking/OCNetworking.h

@@ -47,6 +47,8 @@
 
 - (NSURLSessionTask *)downloadFileNameServerUrl:(NSString *)fileNameServerUrl fileNameLocalPath:(NSString *)fileNameLocalPath communication:(OCCommunication *)communication success:(void (^)(int64_t length, NSString *etag, NSDate *date))success failure:(void (^)(NSString *message, NSInteger errorCode))failure;
 
+- (NSURLSessionTask *)downloadFile:(NSString *)url fileNameLocalPath:(NSString *)fileNameLocalPath  success:(void (^)())success failure:(void (^)(NSString *message, NSInteger errorCode))failure;
+
 - (NSURLSessionTask *)uploadFileNameServerUrl:(NSString *)fileNameServerUrl fileNameLocalPath:(NSString *)fileNameLocalPath communication:(OCCommunication *)communication success:(void(^)(NSString *fileID, NSString *etag, NSDate *date))success failure:(void (^)(NSString *message, NSInteger errorCode))failure;
 
 - (void)downloadThumbnailWithMetadata:(tableMetadata*)metadata serverUrl:(NSString *)serverUrl withWidth:(CGFloat)width andHeight:(CGFloat)height completion:(void (^)(NSString *message, NSInteger errorCode))completion;

+ 37 - 0
iOSClient/Networking/OCNetworking.m

@@ -312,6 +312,43 @@
     return sessionTask;
 }
 
+- (NSURLSessionTask *)downloadFile:(NSString *)url fileNameLocalPath:(NSString *)fileNameLocalPath success:(void (^)())success failure:(void (^)(NSString *message, NSInteger errorCode))failure
+{
+    OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
+
+    [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
+    [communication setUserAgent:[CCUtility getUserAgent]];
+    
+    NSURLSessionTask *sessionTask = [communication downloadFileSession:url toDestiny:fileNameLocalPath defaultPriority:YES onCommunication:communication progress:^(NSProgress *progress) {
+        //float percent = roundf (progress.fractionCompleted * 100);
+    } successRequest:^(NSURLResponse *response, NSURL *filePath) {
+        
+        success();
+        
+    } failureRequest:^(NSURLResponse *response, NSError *error) {
+        
+        NSString *message;
+        NSInteger errorCode;
+        
+        NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
+        errorCode = httpResponse.statusCode;
+        
+        if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
+            errorCode = error.code;
+        
+        // Error
+        if (errorCode == 503)
+            message = NSLocalizedString(@"_server_error_retry_", nil);
+        else
+            message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
+        
+        failure(message, errorCode);
+    }];
+    
+    return sessionTask;
+}
+
+
 #pragma --------------------------------------------------------------------------------------------
 #pragma mark ===== upload =====
 #pragma --------------------------------------------------------------------------------------------