فهرست منبع

update download

kevin 2 سال پیش
والد
کامیت
62349bf888

+ 1 - 0
.gitignore

@@ -10,3 +10,4 @@ Podfile.lock
 src/__pycache__/
 ExampleCode/
 appbuilder-ios/NexilisLite/NexilisLite.xcodeproj/project.pbxproj
+appbuilder-ios/NexilisLite/Podfile.lock

+ 26 - 0
appbuilder-ios/NexilisLite/NexilisLite/Source/Download.swift

@@ -7,6 +7,7 @@
 //
 
 import Foundation
+import Alamofire
 
 public class Download {
     
@@ -21,6 +22,7 @@ public class Download {
     private var downloadBufferQueue = DispatchQueue(label: "DOWNLOAD_BUFFER", attributes: .concurrent)
     
     var DOWNLOAD_BUFFER = [Data?]()
+    var DOWNLOAD_SESSION = [Session]()
     
     public func start(forKey: String, delegate: DownloadDelegate){
         self.delegate = delegate
@@ -42,6 +44,30 @@ 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 {
+        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)
+            }
+            else {
+                let statusCode = result.response?.statusCode
+                print("Response fail: \(statusCode)")
+                onError?()
+            }
+        }
+        return downloadRequest
+    }
+    
     func put(part: Int, buffer: Data){
         downloadBufferQueue.async (flags: .barrier){
             self.DOWNLOAD_BUFFER.insert(buffer, at: part)

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

@@ -183,25 +183,30 @@ 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){
+    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 {
         
-        guard filename.isEmpty else {
-            let fileManager = FileManager.default
-            let documentDir = try fileManager.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
-            for name in filename {
-                let fileDir = documentDir.appendingPathComponent(name)
-                files.append(fileDir)
+        var filesIn = files
+        
+        if !filename.isEmpty {
+            do {
+                let fileManager = FileManager.default
+                let documentDir = try fileManager.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
+                for name in filename {
+                    let fileDir = documentDir.appendingPathComponent(name)
+                    filesIn.append(fileDir)
+                }
             }
+            catch {}
         }
         
-        AF.upload(multipartFormData: { (multipartFormData: MultipartFormData) in
+        let uploadRequest = AF.upload(multipartFormData: { (multipartFormData: MultipartFormData) in
             for (key, value) in parameters {
                 multipartFormData.append("\(value)".data(using: String.Encoding.utf8)!, withName: key as String)
                 print(multipartFormData)
             }
             
-            for i in 0..<files.count {
-                multipartFormData.append(files[i], withName: "file\(i+1)")
+            for i in 0..<filesIn.count {
+                multipartFormData.append(filesIn[i], withName: "file\(i+1)")
                 print(multipartFormData)
             }
             
@@ -218,11 +223,10 @@ public class Network {
             }
         }
         .uploadProgress { progress in
-            if isCancel {
-                progress
-            }
             onProgress?(progress)
         }
+        
+        return uploadRequest
     }
     
     public func cancel() {