Forráskód Böngészése

Change upload and download to https

kevin 2 éve
szülő
commit
a8e3272720

+ 25 - 14
appbuilder-ios/NexilisLite/NexilisLite/Source/Download.swift

@@ -23,6 +23,7 @@ public class Download {
     
     var DOWNLOAD_BUFFER = [Data?]()
     var DOWNLOAD_SESSION = [Session]()
+    var DOWNLOAD_URL = "https://newuniverse.io/filepalio/image/"
     
     public func start(forKey: String, delegate: DownloadDelegate){
         self.delegate = delegate
@@ -44,28 +45,38 @@ public class Download {
         _ = Nexilis.write(message: CoreMessage_TMessageBank.getImageDownload(p_image_id: forKey))
     }
     
-    public func startHTTP(filename: String, baseURL: String, onCompletion: ((Data) -> Void)? = nil, onError: (() -> Void)? = nil, onProgress: ((Progress) -> Void)? = nil) -> DownloadRequest {
+    public func startHTTP(forKey: String, completion: @escaping (String, Double)->()) {
+        _ = startHTTP(filename: forKey, baseURL: DOWNLOAD_URL, completion: completion)
+    }
+    
+    public func startHTTP(filename: String, baseURL: String, completion: @escaping (String, Double)->()) -> DownloadRequest {
         var sep = ""
         if baseURL.last != "/" {
             sep = "/"
         }
         let fullURL = "\(baseURL)\(sep)\(filename)"
-        let downloadRequest = AF.download(fullURL)
-        .downloadProgress(queue: downloadBufferQueue) { progress in
-            onProgress?(progress)
-        }
-        .responseData { result in
-            if let successResponse = result.value as? Data{
-                //print("Response success")
-                onCompletion?(successResponse)
+        do {
+            let destination = DownloadRequest.suggestedDownloadDestination(for: .documentDirectory, in: .userDomainMask, options: [.removePreviousFile, .createIntermediateDirectories])
+            let downloadRequest = AF.download(fullURL, to: destination)
+            .downloadProgress(queue: downloadBufferQueue) { progress in
+                completion(filename,progress.fractionCompleted*100)
             }
-            else {
-                let statusCode = result.response?.statusCode
-                //print("Response fail: \(statusCode)")
-                onError?()
+            .response { result in
+                if let successResponse = result.value{
+                    //print("Response success")
+                    completion(filename,100)
+                }
+                else {
+                    let statusCode = result.response?.statusCode
+                    //print("Response fail: \(statusCode)")
+                    completion(filename,0)
+                }
             }
+            return downloadRequest
         }
-        return downloadRequest
+        catch {}
+        
+        
     }
     
     func put(part: Int, buffer: Data){

+ 1 - 1
appbuilder-ios/NexilisLite/NexilisLite/Source/Extension.swift

@@ -286,7 +286,7 @@ extension NSObject {
                 completion(true, false, isCircle ? image?.circleMasked : image)
             } else {
                 completion(false, false, placeholderImage)
-                Download().start(forKey: url) { (name, progress) in
+                Download().startHTTP(forKey: url) { (name, progress) in
                     guard progress == 100 else {
                         return
                     }

+ 2 - 2
appbuilder-ios/NexilisLite/NexilisLite/Source/IncomingThread.swift

@@ -410,7 +410,7 @@ class IncomingThread {
                                     profileImage.image = UIImage(contentsOfFile: file.path)
                                     profileImage.backgroundColor = .clear
                                 } else {
-                                    Download().start(forKey: profile) { (name, progress) in
+                                    Download().startHTTP(forKey: profile) { (name, progress) in
                                         guard progress == 100 else {
                                             return
                                         }
@@ -1087,7 +1087,7 @@ class IncomingThread {
             }
         }
         if (!thumb_id.isEmpty) {
-            Download().start(forKey: thumb_id) { (file, progress) in
+            Download().startHTTP(forKey: thumb_id) { (file, progress) in
                 print ("masuk download \(progress)")
                 if(progress == 100) {
                     Nexilis.saveMessage(message: message, withStatus: false)

+ 13 - 4
appbuilder-ios/NexilisLite/NexilisLite/Source/Network.swift

@@ -17,6 +17,7 @@ public class Network {
     private var isCancel = false
     private var progress = 0.0
     private var CHUNK_SIZE = 200 * 1024
+    private var UPLOAD_URL = "https://newuniverse.io/uploader"
     
     public init() {}
     
@@ -183,7 +184,15 @@ public class Network {
         }
     }
     
-    public func uploadHTTP(_ endUrl: String, files: [URL] = [], filename: [String] = [], parameters: [String : Any] = [:], onCompletion: (([String : Any]) -> Void)? = nil, onError: (() -> Void)? = nil, onProgress: ((Progress) -> Void)? = nil) -> UploadRequest {
+    public func uploadHTTP(name: String, completion: @escaping (Bool, Double, [String:Any]?)->()) {
+        _ = uploadHTTP(UPLOAD_URL, filename: [name], completion: completion)
+    }
+    
+    public func uploadHTTP(fileUrl: URL, completion: @escaping (Bool, Double, [String:Any]?)->()) {
+        _ = uploadHTTP(UPLOAD_URL, files: [fileUrl], completion: completion)
+    }
+    
+    public func uploadHTTP(_ endUrl: String, files: [URL] = [], filename: [String] = [], parameters: [String : Any] = [:], completion: @escaping (Bool, Double, [String:Any]?)->()) -> UploadRequest {
         
         var filesIn = files
         
@@ -214,16 +223,16 @@ public class Network {
         .responseJSON { result in
             if let successResponse = result.value as? [String:Any] {
                 //print("Response success")
-                onCompletion?(successResponse)
+                completion(true,100,successResponse)
             }
             else {
                 let statusCode = result.response?.statusCode
                 //print("Response fail: \(statusCode)")
-                onError?()
+                completion(false,0,nil)
             }
         }
         .uploadProgress { progress in
-            onProgress?(progress)
+            completion(!progress.isCancelled,progress.fractionCompleted*100,nil)
         }
         
         return uploadRequest

+ 4 - 4
appbuilder-ios/NexilisLite/NexilisLite/Source/Nexilis.swift

@@ -148,7 +148,7 @@ public class Nexilis: NSObject {
                                 let documentDir = try FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
                                 let file = documentDir.appendingPathComponent(cursorData.string(forColumnIndex: 0)!)
                                 if !FileManager().fileExists(atPath: file.path) {
-                                    Download().start(forKey: cursorData.string(forColumnIndex: 0)!) { (name, progress) in}
+                                    Download().startHTTP(forKey: cursorData.string(forColumnIndex: 0)!) { (name, progress) in}
                                 }
                             } catch {}
                             cursorData.close()
@@ -1933,7 +1933,7 @@ extension Nexilis: MessageDelegate {
                                     UIApplication.shared.visibleViewController?.present(previewImageVC, animated: true, completion: nil)
                                 }
                             } else {
-                                Download().start(forKey: image) { (name, progress) in
+                                Download().startHTTP(forKey: image) { (name, progress) in
                                     guard progress == 100 else {
                                         return
                                     }
@@ -1982,7 +1982,7 @@ extension Nexilis: MessageDelegate {
                                     UIApplication.shared.visibleViewController?.present(previewController, animated: true, completion: nil)
                                 }
                             } else {
-                                Download().start(forKey: file) { (name, progress) in
+                                Download().startHTTP(forKey: file) { (name, progress) in
                                     DispatchQueue.main.async {
                                         guard progress == 100 else {
                                             return
@@ -2959,7 +2959,7 @@ extension Nexilis: MessageDelegate {
                                 profileImage.image = UIImage(contentsOfFile: file.path)
                                 profileImage.backgroundColor = .clear
                             } else {
-                                Download().start(forKey: profile) { (name, progress) in
+                                Download().startHTTP(forKey: profile) { (name, progress) in
                                     guard progress == 100 else {
                                         return
                                     }

+ 3 - 3
appbuilder-ios/NexilisLite/NexilisLite/Source/OutgoingThread.swift

@@ -136,7 +136,7 @@ class OutgoingThread {
         let isMedia = !fileName.isEmpty
         if isMedia {
             if (!message.getBody(key: CoreMessage_TMessageKey.THUMB_ID).isEmpty) {
-                Network().upload(name: message.getBody(key: CoreMessage_TMessageKey.THUMB_ID)) { (result, progress) in
+                Network().uploadHTTP(name: message.getBody(key: CoreMessage_TMessageKey.THUMB_ID)) { (result, progress, response) in
                     if result, progress == 100 {
                         do {
                             let fileManager = FileManager.default
@@ -148,7 +148,7 @@ class OutgoingThread {
                                 message.setMedia(media: [UInt8] (data))
                             }
                         } catch {}
-                        Network().upload(name: fileName) { (result, progress) in
+                        Network().uploadHTTP(name: fileName) { (result, progress, response) in
                             if result {
                                 if let delegate = Nexilis.shared.messageDelegate {
                                     delegate.onUpload(name: fileName, progress: progress)
@@ -174,7 +174,7 @@ class OutgoingThread {
                     }
                 }
             } else {
-                Network().upload(name: fileName) { (result, progress) in
+                Network().uploadHTTP(name: fileName) { (result, progress, response) in
                     if result {
                         if let delegate = Nexilis.shared.messageDelegate {
                             delegate.onUpload(name: fileName, progress: progress)

+ 1 - 1
appbuilder-ios/NexilisLite/NexilisLite/Source/View/Call/AudioViewController.swift

@@ -195,7 +195,7 @@ extension UIImageView {
                 self.image = UIImage(contentsOfFile: file.path)
                 self.backgroundColor = .clear
             } else {
-                Download().start(forKey: url) { (name, progress) in
+                Download().startHTTP(forKey: url) { (name, progress) in
                     print ("masuk download \(progress)")
                     guard progress == 100 else {
                         return

+ 4 - 4
appbuilder-ios/NexilisLite/NexilisLite/Source/View/Chat/EditorGroup.swift

@@ -1469,7 +1469,7 @@ public class EditorGroup: UIViewController {
             let auto = UserDefaults.standard.bool(forKey: "autoDownload")
             if auto {
                 if dataMessages[index]["image_id"] as? String != nil && !((dataMessages[index]["image_id"] as? String)!.isEmpty) {
-                    Download().start(forKey:dataMessages[index]["image_id"] as! String) { (name, progress) in
+                    Download().startHTTP(forKey:dataMessages[index]["image_id"] as! String) { (name, progress) in
                         guard progress == 100 else {
                             return
                         }
@@ -3949,7 +3949,7 @@ extension EditorGroup: UITableViewDelegate, UITableViewDataSource {
                                                        y: sender.imageView.frame.height/2)
                     activityIndicator.startAnimating()
                     sender.imageView.addSubview(activityIndicator)
-                    Download().start(forKey: sender.image_id) { (name, progress) in
+                    Download().startHTTP(forKey: sender.image_id) { (name, progress) in
                         guard progress == 100 else {
                             return
                         }
@@ -4013,7 +4013,7 @@ extension EditorGroup: UITableViewDelegate, UITableViewDataSource {
                     imageDownload.centerYAnchor.constraint(equalTo: sender.imageView.centerYAnchor).isActive = true
                     imageDownload.widthAnchor.constraint(equalToConstant: 30).isActive = true
                     imageDownload.heightAnchor.constraint(equalToConstant: 30).isActive = true
-                    Download().start(forKey: sender.video_id) { (name, progress) in
+                    Download().startHTTP(forKey: sender.video_id) { (name, progress) in
                         DispatchQueue.main.async {
                             guard progress == 100 else {
                                 shapeLoading.strokeEnd = CGFloat(progress / 100)
@@ -4084,7 +4084,7 @@ extension EditorGroup: UITableViewDelegate, UITableViewDataSource {
                     imageupload.centerYAnchor.constraint(equalTo: containerLoading.centerYAnchor).isActive = true
                     imageupload.centerXAnchor.constraint(equalTo: containerLoading.centerXAnchor).isActive = true
                     
-                    Download().start(forKey: sender.file_id) { (name, progress) in
+                    Download().startHTTP(forKey: sender.file_id) { (name, progress) in
                         DispatchQueue.main.async {
                             guard progress == 100 else {
                                 shapeLoading.strokeEnd = CGFloat(progress / 100)

+ 4 - 4
appbuilder-ios/NexilisLite/NexilisLite/Source/View/Chat/EditorPersonal.swift

@@ -2657,7 +2657,7 @@ public class EditorPersonal: UIViewController, ImageVideoPickerDelegate, UIGestu
             let auto = UserDefaults.standard.bool(forKey: "autoDownload")
             if auto {
                 if dataMessages[index]["image_id"] as? String != nil && !((dataMessages[index]["image_id"] as? String)!.isEmpty) {
-                    Download().start(forKey:dataMessages[index]["image_id"] as! String) { (name, progress) in
+                    Download().startHTTP(forKey:dataMessages[index]["image_id"] as! String) { (name, progress) in
                         guard progress == 100 else {
                             return
                         }
@@ -5331,7 +5331,7 @@ extension EditorPersonal: UITableViewDelegate, UITableViewDataSource {
                                                        y: sender.imageView.frame.height/2)
                     activityIndicator.startAnimating()
                     sender.imageView.addSubview(activityIndicator)
-                    Download().start(forKey: sender.image_id) { (name, progress) in
+                    Download().startHTTP(forKey: sender.image_id) { (name, progress) in
                         guard progress == 100 else {
                             return
                         }
@@ -5393,7 +5393,7 @@ extension EditorPersonal: UITableViewDelegate, UITableViewDataSource {
                     imageDownload.centerYAnchor.constraint(equalTo: sender.imageView.centerYAnchor).isActive = true
                     imageDownload.widthAnchor.constraint(equalToConstant: 30).isActive = true
                     imageDownload.heightAnchor.constraint(equalToConstant: 30).isActive = true
-                    Download().start(forKey: sender.video_id) { (name, progress) in
+                    Download().startHTTP(forKey: sender.video_id) { (name, progress) in
                         DispatchQueue.main.async {
                             guard progress == 100 else {
                                 shapeLoading.strokeEnd = CGFloat(progress / 100)
@@ -5464,7 +5464,7 @@ extension EditorPersonal: UITableViewDelegate, UITableViewDataSource {
                     imageupload.centerYAnchor.constraint(equalTo: containerLoading.centerYAnchor).isActive = true
                     imageupload.centerXAnchor.constraint(equalTo: containerLoading.centerXAnchor).isActive = true
                     
-                    Download().start(forKey: sender.file_id) { (name, progress) in
+                    Download().startHTTP(forKey: sender.file_id) { (name, progress) in
                         DispatchQueue.main.async {
                             guard progress == 100 else {
                                 shapeLoading.strokeEnd = CGFloat(progress / 100)

+ 3 - 3
appbuilder-ios/NexilisLite/NexilisLite/Source/View/Chat/EditorStarMessages.swift

@@ -762,7 +762,7 @@ public class EditorStarMessages: UIViewController, UITableViewDataSource, UITabl
                                                        y: sender.imageView.frame.height/2)
                     activityIndicator.startAnimating()
                     sender.imageView.addSubview(activityIndicator)
-                    Download().start(forKey: sender.image_id) { (name, progress) in
+                    Download().startHTTP(forKey: sender.image_id) { (name, progress) in
                         guard progress == 100 else {
                             return
                         }
@@ -826,7 +826,7 @@ public class EditorStarMessages: UIViewController, UITableViewDataSource, UITabl
                     imageDownload.centerYAnchor.constraint(equalTo: sender.imageView.centerYAnchor).isActive = true
                     imageDownload.widthAnchor.constraint(equalToConstant: 30).isActive = true
                     imageDownload.heightAnchor.constraint(equalToConstant: 30).isActive = true
-                    Download().start(forKey: sender.video_id) { (name, progress) in
+                    Download().startHTTP(forKey: sender.video_id) { (name, progress) in
                         DispatchQueue.main.async {
                             guard progress == 100 else {
                                 shapeLoading.strokeEnd = CGFloat(progress / 100)
@@ -897,7 +897,7 @@ public class EditorStarMessages: UIViewController, UITableViewDataSource, UITabl
                     imageupload.centerYAnchor.constraint(equalTo: containerLoading.centerYAnchor).isActive = true
                     imageupload.centerXAnchor.constraint(equalTo: containerLoading.centerXAnchor).isActive = true
                     
-                    Download().start(forKey: sender.file_id) { (name, progress) in
+                    Download().startHTTP(forKey: sender.file_id) { (name, progress) in
                         DispatchQueue.main.async {
                             guard progress == 100 else {
                                 shapeLoading.strokeEnd = CGFloat(progress / 100)

+ 3 - 3
appbuilder-ios/NexilisLite/NexilisLite/Source/View/Control/BackupRestoreView.swift

@@ -244,7 +244,7 @@ public class BackupRestoreView: UIViewController, UITableViewDataSource, UITable
             if let dirPath = paths.first {
                 let fileURL = URL(fileURLWithPath: dirPath).appendingPathComponent(getFileName(option: optionBackup, fileId: fileIdBackup))
                 if !FileManager.default.fileExists(atPath: fileURL.path) {
-                    Download().start(forKey: getFileName(option: optionBackup, fileId: fileIdBackup)) { (name, progress) in
+                    Download().startHTTP(forKey: getFileName(option: optionBackup, fileId: fileIdBackup)) { (name, progress) in
                         DispatchQueue.main.async { [self] in
                             guard progress == 100 else {
                                 labelRestoring.text = "Downloading...".localized() + "  \(progress)%"
@@ -469,7 +469,7 @@ public class BackupRestoreView: UIViewController, UITableViewDataSource, UITable
                     if let dirPath = paths.first {
                         let thumbURL = URL(fileURLWithPath: dirPath).appendingPathComponent(thumbId)
                         if !FileManager.default.fileExists(atPath: thumbURL.path) {
-                            Download().start(forKey: thumbId) { (name, progress) in}
+                            Download().startHTTP(forKey: thumbId) { (name, progress) in}
                         }
                     }
                 }
@@ -883,7 +883,7 @@ public class BackupRestoreView: UIViewController, UITableViewDataSource, UITable
                     //print("Adding entry to ZIP archive failed with error:\(error)")
                 }
                 self.labelPreparing.text = "Uploading...".localized()
-                Network().upload(fileUrl: zipFiles, completion: { result,progress in
+                Network().uploadHTTP(fileUrl: zipFiles, completion: { result,progress,response in
                     if result {
                         DispatchQueue.main.async { [self] in
                             labelPreparing.text = "Uploading...".localized() + " \(progress)%"

+ 3 - 3
appbuilder-ios/NexilisLite/NexilisLite/Source/View/Control/BroadcastViewController.swift

@@ -404,10 +404,10 @@ class BroadcastViewController: UITableViewController, UITextFieldDelegate, UITex
             clearAttachment()
         }
         if !thumbId.isEmpty {
-            Network().upload(name: String(thumbId)) { (result1, progress) in
+            Network().uploadHTTP(name: String(thumbId)) { (result1, progress, response1) in
                 if result1 {
                     if progress == 100 {
-                        Network().upload(name: String(self.fileId)) { (result2, progress) in
+                        Network().uploadHTTP(name: String(self.fileId)) { (result2, progress, response2) in
                             if result2 {
                                 if progress == 100 {
                                     self.sendMsg(startTime: startTime, endTime: endTime)
@@ -418,7 +418,7 @@ class BroadcastViewController: UITableViewController, UITextFieldDelegate, UITex
                 }
             }
         } else if !fileId.isEmpty {
-            Network().upload(name: String(fileId)) { (result2, progress) in
+            Network().uploadHTTP(name: String(fileId)) { (result2, progress, response2) in
                 if result2 {
                     if progress == 100 {
                         self.sendMsg(startTime: startTime, endTime: endTime)

+ 1 - 1
appbuilder-ios/NexilisLite/NexilisLite/Source/View/Control/GroupDetailViewController.swift

@@ -981,7 +981,7 @@ extension GroupDetailViewController: ImageVideoPickerDelegate {
                     DispatchQueue.main.async {
                         Nexilis.showLoader()
                     }
-                    Network().upload(name: fileDir.lastPathComponent) { result, progress in
+                    Network().uploadHTTP(name: fileDir.lastPathComponent) { result, progress , response in
                         guard result, progress == 100 else {
                             return
                         }

+ 1 - 1
appbuilder-ios/NexilisLite/NexilisLite/Source/View/Control/ProfileViewController.swift

@@ -675,7 +675,7 @@ extension ProfileViewController: ImageVideoPickerDelegate {
                     let fileDir = documentDir.appendingPathComponent("THUMB_\(me)\(Date().currentTimeMillis().toHex())")
                     if !FileManager.default.fileExists(atPath: fileDir.path), let data = resize.jpegData(compressionQuality: 0.8) {
                         try! data.write(to: fileDir)
-                        Network().upload(name: fileDir.lastPathComponent) { result, progress in
+                        Network().uploadHTTP(name: fileDir.lastPathComponent) { result, progress , response in
                             guard result, progress == 100 else {
                                 return
                             }

+ 2 - 2
appbuilder-ios/NexilisLite/NexilisLite/Source/View/Control/SettingTableViewController.swift

@@ -149,7 +149,7 @@ public class SettingTableViewController: UITableViewController, UIGestureRecogni
                                     NotificationCenter.default.post(name: NSNotification.Name(rawValue: "imageFBUpdate"), object: nil, userInfo: dataImage)
                                 }
                             } else {
-                                Download().start(forKey: image!) { (name, progress) in
+                                Download().startHTTP(forKey: image!) { (name, progress) in
                                     guard progress == 100 else {
                                         return
                                     }
@@ -188,7 +188,7 @@ public class SettingTableViewController: UITableViewController, UIGestureRecogni
                             dataImage["name"] = imageSignIn
                             NotificationCenter.default.post(name: NSNotification.Name(rawValue: "imageFBUpdate"), object: nil, userInfo: dataImage)
                         } else {
-                            Download().start(forKey: imageSignIn) { (name, progress) in
+                            Download().startHTTP(forKey: imageSignIn) { (name, progress) in
                                 guard progress == 100 else {
                                     return
                                 }