diff --git a/.DS_Store b/.DS_Store index 8924fe17..d8b20931 100644 Binary files a/.DS_Store and b/.DS_Store differ diff --git a/Example/.DS_Store b/Example/.DS_Store index 48b7d8a2..31cb4ece 100644 Binary files a/Example/.DS_Store and b/Example/.DS_Store differ diff --git a/Example/Podfile.lock b/Example/Podfile.lock index 8b966a73..2582bc56 100644 --- a/Example/Podfile.lock +++ b/Example/Podfile.lock @@ -12,7 +12,7 @@ PODS: - FBSnapshotTestCase (~> 2.0) - Nimble (~> 7.0) - Quick (1.2.0) - - TUSKit (0.1.0) + - TUSKit (2.0.0.alpha) DEPENDENCIES: - AppSpectorSDK @@ -40,7 +40,7 @@ SPEC CHECKSUMS: Nimble: 051e3d8912d40138fa5591c78594f95fb172af37 Nimble-Snapshots: f5459b5b091678dc942d03ec4741cacb58ba4a52 Quick: 58d203b1c5e27fff7229c4c1ae445ad7069a7a08 - TUSKit: 7626bfb27096011d3eb55f515b53b08f005913be + TUSKit: 95b4ad77dd21df1cc6662e6041d117104cd3cafe PODFILE CHECKSUM: 572a5d87651b95d2aa1ecdf921075dfd78b37d95 diff --git a/Example/TUSKit/ViewController.swift b/Example/TUSKit/ViewController.swift index be8d3731..73ac9bd4 100644 --- a/Example/TUSKit/ViewController.swift +++ b/Example/TUSKit/ViewController.swift @@ -35,6 +35,8 @@ class ViewController: UIViewController, TUSDelegate, UIImagePickerControllerDele let number = Int.random(in: 0 ..< 100) //TODO: Remove before release: this is only set so we can run multiple files while developer //When you have a file, create an upload, and give it a Id. + + let upload: TUSUpload = TUSUpload(withId: String(format: "%@%@", "im", String(number)), andFilePathURL: imageURL as! URL, andFileType: ".jpeg") //Create or resume upload TUSClient.shared.createOrResume(forUpload: upload) @@ -69,8 +71,14 @@ class ViewController: UIViewController, TUSDelegate, UIImagePickerControllerDele // } - func TUSFailure(forUpload upload: TUSUpload, withResponse response: TUSResponse, andError error: Error) { + func TUSFailure(forUpload upload: TUSUpload?, withResponse response: TUSResponse?, andError error: Error?) { // + if (response != nil) { + print(response!.message!) + } + if (error != nil) { + print(error!.localizedDescription) + } } override func didReceiveMemoryWarning() { diff --git a/TUSKit.podspec b/TUSKit.podspec index 1819a388..06fe528c 100644 --- a/TUSKit.podspec +++ b/TUSKit.podspec @@ -8,7 +8,7 @@ Pod::Spec.new do |s| s.name = 'TUSKit' - s.version = '2.0.0.alpha' + s.version = '2.0.1.alpha' s.summary = 'A rewrite of TUSKit, in Swift' s.swift_version = '5.0' diff --git a/TUSKit/Classes/TUSClient.swift b/TUSKit/Classes/TUSClient.swift index 89506e38..67c4c880 100644 --- a/TUSKit/Classes/TUSClient.swift +++ b/TUSKit/Classes/TUSClient.swift @@ -74,9 +74,15 @@ public class TUSClient: NSObject, URLSessionTaskDelegate { let fileName = String(format: "%@%@", upload.id!, upload.fileType!) if (fileManager.fileExists(withName: fileName) == false) { if (upload.filePath != nil) { - fileManager.moveFile(atLocation: upload.filePath!, withFileName: fileName) + if fileManager.moveFile(atLocation: upload.filePath!, withFileName: fileName) == false{ + //fail out + return + } } else if(upload.data != nil) { - fileManager.writeData(withData: upload.data!, andFileName: fileName) + if fileManager.writeData(withData: upload.data!, andFileName: fileName) == false { + //fail out + return + } } } diff --git a/TUSKit/Classes/TUSDelegate.swift b/TUSKit/Classes/TUSDelegate.swift index 24117518..799baadf 100644 --- a/TUSKit/Classes/TUSDelegate.swift +++ b/TUSKit/Classes/TUSDelegate.swift @@ -15,6 +15,6 @@ public protocol TUSDelegate { func TUSSuccess(forUpload upload: TUSUpload) - func TUSFailure(forUpload upload: TUSUpload, withResponse response: TUSResponse, andError error: Error) + func TUSFailure(forUpload upload: TUSUpload?, withResponse response: TUSResponse?, andError error: Error?) } diff --git a/TUSKit/Classes/TUSFileManager.swift b/TUSKit/Classes/TUSFileManager.swift index ce68b347..d949c63f 100644 --- a/TUSKit/Classes/TUSFileManager.swift +++ b/TUSKit/Classes/TUSFileManager.swift @@ -21,7 +21,9 @@ class TUSFileManager: NSObject { try FileManager.default.createDirectory(atPath: fileStorePath(), withIntermediateDirectories: false, attributes: nil) } catch let error as NSError { if (error.code != 516) { //516 is failed creating due to already existing - print(error.localizedDescription) + let response: TUSResponse = TUSResponse(message: "Failed creating TUS directory in documents") + TUSClient.shared.delegate?.TUSFailure(forUpload: nil, withResponse: response, andError: error) + } } } @@ -30,19 +32,25 @@ class TUSFileManager: NSObject { return FileManager.default.fileExists(atPath: fileStorePath().appending(name)) } - internal func moveFile(atLocation location: URL, withFileName name: String) { + internal func moveFile(atLocation location: URL, withFileName name: String) -> Bool { do { try FileManager.default.moveItem(at: location, to: URL(fileURLWithPath: fileStorePath().appending(name))) + return true } catch let error as NSError { - print(error.localizedDescription) + let response: TUSResponse = TUSResponse(message: "Failed moving file \(location.absoluteString) to \(fileStorePath().appending(name)) for TUS folder storage") + TUSClient.shared.delegate?.TUSFailure(forUpload: nil, withResponse: response, andError: error) + return false } } - internal func writeData(withData data: Data, andFileName name: String) { + internal func writeData(withData data: Data, andFileName name: String) -> Bool { do { try data.write(to: URL(string: fileStorePath().appending(name))!) + return true } catch let error as NSError { - print(error.localizedDescription) + let response: TUSResponse = TUSResponse(message: "Failed writing data to file \(fileStorePath().appending(name))") + TUSClient.shared.delegate?.TUSFailure(forUpload: nil, withResponse: response, andError: error) + return false } } @@ -52,10 +60,12 @@ class TUSFileManager: NSObject { if let fileSize = fileAttributes[FileAttributeKey.size] { return (fileSize as! NSNumber).uint64Value } else { - print("Failed to get a size attribute from path: \(filePath)") + let response: TUSResponse = TUSResponse(message: "Failed to get a size attribute from path: \(filePath)") + TUSClient.shared.delegate?.TUSFailure(forUpload: nil, withResponse: response, andError: nil) } } catch { - print("Failed to get file attributes for local path: \(filePath) with error: \(error)") + let response: TUSResponse = TUSResponse(message: "Failed to get a size attribute from path: \(filePath)") + TUSClient.shared.delegate?.TUSFailure(forUpload: nil, withResponse: response, andError: error) } return 0 } diff --git a/TUSKit/Classes/TUSResponse.swift b/TUSKit/Classes/TUSResponse.swift index d34cf9e7..6c80d3ac 100644 --- a/TUSKit/Classes/TUSResponse.swift +++ b/TUSKit/Classes/TUSResponse.swift @@ -9,4 +9,6 @@ import Foundation public struct TUSResponse: Codable { + public var message: String? + }