From 19bdb5311cb5afe2a55164dd49f48a301a1b4bcc Mon Sep 17 00:00:00 2001 From: "Mark R. Masterson" Date: Mon, 4 May 2020 03:37:10 -0700 Subject: [PATCH] Delegate early --- Example/TUSKit/ViewController.swift | 7 ++++- TUSKit/Classes/TUSClient.swift | 43 +++++++++++++++++------------ TUSKit/Classes/TUSDelegate.swift | 2 +- TUSKit/Classes/TUSExecutor.swift | 4 +-- TUSKit/Classes/TUSSession.swift | 13 ++++++--- TUSKit/Classes/TUSUpload.swift | 2 +- 6 files changed, 45 insertions(+), 26 deletions(-) diff --git a/Example/TUSKit/ViewController.swift b/Example/TUSKit/ViewController.swift index 230ca6b9..be8d3731 100644 --- a/Example/TUSKit/ViewController.swift +++ b/Example/TUSKit/ViewController.swift @@ -54,13 +54,18 @@ class ViewController: UIViewController, TUSDelegate, UIImagePickerControllerDele func TUSProgress(bytesUploaded uploaded: Int, bytesRemaining remaining: Int) { // + print(uploaded) + print(remaining) } func TUSProgress(forUpload upload: TUSUpload, bytesUploaded uploaded: Int, bytesRemaining remaining: Int) { // + print(uploaded) + print(remaining) } - func TUSSuccess(forUpload upload: TUSUpload, withResponse response: TUSResponse) { + func TUSSuccess(forUpload upload: TUSUpload) { + print(upload.uploadLocationURL) // } diff --git a/TUSKit/Classes/TUSClient.swift b/TUSKit/Classes/TUSClient.swift index 05bbee2f..89506e38 100644 --- a/TUSKit/Classes/TUSClient.swift +++ b/TUSKit/Classes/TUSClient.swift @@ -7,11 +7,12 @@ import UIKit -public class TUSClient: NSObject { +public class TUSClient: NSObject, URLSessionTaskDelegate { + // MARK: Properties - internal var tusSession: TUSSession + internal var tusSession: TUSSession = TUSSession() public var uploadURL: URL? public var delegate: TUSDelegate? private let executor: TUSExecutor @@ -20,19 +21,20 @@ public class TUSClient: NSObject { private static var config: TUSConfig? internal var logger: TUSLogger public var chunkSize: Int = TUSConstants.chunkSize //Default chunksize can be overwritten - - public var currentUploads: [TUSUpload]? { - get { - guard let data = UserDefaults.standard.object(forKey: TUSConstants.kSavedTUSUploadsDefaultsKey) as? Data else { - return nil - } - return NSKeyedUnarchiver.unarchiveObject(with: data) as? [TUSUpload] - } - set(currentUploads) { - let data = NSKeyedArchiver.archivedData(withRootObject: currentUploads!) - UserDefaults.standard.set(data, forKey: TUSConstants.kSavedTUSUploadsDefaultsKey) - } - } + public var currentUploads: [TUSUpload]? + //TODO: Fix this +// public var currentUploads: [TUSUpload]? { +// get { +// guard let data = UserDefaults.standard.object(forKey: TUSConstants.kSavedTUSUploadsDefaultsKey) as? Data else { +// return nil +// } +// return NSKeyedUnarchiver.unarchiveObject(with: data) as? [TUSUpload] +// } +// set(currentUploads) { +// let data = NSKeyedArchiver.archivedData(withRootObject: currentUploads!) +// UserDefaults.standard.set(data, forKey: TUSConstants.kSavedTUSUploadsDefaultsKey) +// } +// } public var status: TUSClientStaus? { get { @@ -56,15 +58,19 @@ public class TUSClient: NSObject { fatalError("Error - you must call setup before accessing TUSClient") } uploadURL = config.uploadURL - tusSession = TUSSession(customConfiguration: config.URLSessionConfig) executor = TUSExecutor() logger = TUSLogger(config.debugLogEnabled) fileManager.createFileDirectory() + super.init() + tusSession = TUSSession(customConfiguration: config.URLSessionConfig, andDelegate: self) + currentUploads = [] + } // MARK: Create methods public func createOrResume(forUpload upload: TUSUpload, withRetries retries: Int) { + currentUploads?.append(upload) let fileName = String(format: "%@%@", upload.id!, upload.fileType!) if (fileManager.fileExists(withName: fileName) == false) { if (upload.filePath != nil) { @@ -137,6 +143,9 @@ public class TUSClient: NSObject { //Delete stuff here } - + public func urlSession(_ session: URLSession, task: URLSessionTask, didSendBodyData bytesSent: Int64, totalBytesSent: Int64, totalBytesExpectedToSend: Int64) { + var upload = currentUploads![0] + self.delegate?.TUSProgress(bytesUploaded: Int(upload.uploadOffset!)!, bytesRemaining: Int(upload.uploadLength!)!) + } } diff --git a/TUSKit/Classes/TUSDelegate.swift b/TUSKit/Classes/TUSDelegate.swift index c638d6fd..24117518 100644 --- a/TUSKit/Classes/TUSDelegate.swift +++ b/TUSKit/Classes/TUSDelegate.swift @@ -13,7 +13,7 @@ public protocol TUSDelegate { func TUSProgress(forUpload upload: TUSUpload, bytesUploaded uploaded: Int, bytesRemaining remaining: Int) - func TUSSuccess(forUpload upload: TUSUpload, withResponse response: TUSResponse) + func TUSSuccess(forUpload upload: TUSUpload) func TUSFailure(forUpload upload: TUSUpload, withResponse response: TUSResponse, andError error: Error) diff --git a/TUSKit/Classes/TUSExecutor.swift b/TUSKit/Classes/TUSExecutor.swift index d42688b2..380112e5 100644 --- a/TUSKit/Classes/TUSExecutor.swift +++ b/TUSKit/Classes/TUSExecutor.swift @@ -7,7 +7,7 @@ import UIKit -class TUSExecutor: NSObject { +class TUSExecutor: NSObject, URLSessionDelegate { // MARK: Private Networking / Upload methods @@ -75,7 +75,6 @@ class TUSExecutor: NSObject { let request: URLRequest = urlRequest(withFullURL: upload.uploadLocationURL!, andMethod: "PATCH", andContentLength: upload.contentLength!, andUploadLength: upload.uploadLength!, andFilename: upload.id!, andHeaders: ["Content-Type":"application/offset+octet-stream", "Upload-Offset": upload.uploadOffset!, "Content-Length": String(chunks[position].count)]) let task = TUSClient.shared.tusSession.session.uploadTask(with: request, from: chunks[position], completionHandler: { (data, response, error) in if let httpResponse = response as? HTTPURLResponse { - print(httpResponse.debugDescription) switch httpResponse.statusCode { case 200..<300: //success @@ -87,6 +86,7 @@ class TUSExecutor: NSObject { TUSClient.shared.logger.log(String(format: "Chunk %u / %u complete", position + 1, chunks.count)) if (position + 1 == chunks.count) { TUSClient.shared.logger.log(String(format: "File %@ uploaded at %@", upload.id!, upload.uploadLocationURL!.absoluteString)) + TUSClient.shared.delegate?.TUSSuccess(forUpload: upload) } } break diff --git a/TUSKit/Classes/TUSSession.swift b/TUSKit/Classes/TUSSession.swift index a3f37b82..27422dad 100644 --- a/TUSKit/Classes/TUSSession.swift +++ b/TUSKit/Classes/TUSSession.swift @@ -9,12 +9,17 @@ import Foundation class TUSSession { var session: URLSession - + init() { - session = URLSession(configuration: .default) + // + session = URLSession() + } + + init(withDelegate delegate: URLSessionTaskDelegate) { + session = URLSession(configuration: .default, delegate: delegate, delegateQueue: OperationQueue.main) } - init(customConfiguration configuration: URLSessionConfiguration) { - session = URLSession(configuration: configuration) + init(customConfiguration configuration: URLSessionConfiguration, andDelegate delegate: URLSessionTaskDelegate) { + session = URLSession(configuration: configuration, delegate: delegate, delegateQueue: OperationQueue.main) } } diff --git a/TUSKit/Classes/TUSUpload.swift b/TUSKit/Classes/TUSUpload.swift index 7cbf4e55..db5e6ce3 100644 --- a/TUSKit/Classes/TUSUpload.swift +++ b/TUSKit/Classes/TUSUpload.swift @@ -14,7 +14,7 @@ public class TUSUpload: NSObject { var fileType: String? // TODO: Make sure only ".fileExtension" gets set. Current setup sets fileType as something like "1A1F31FE6-BB39-4A78-AECD-3C9BDE6D129E.jpeg" var filePath: URL? var data: Data? - internal var uploadLocationURL: URL? + public var uploadLocationURL: URL? var contentLength: String? {