Skip to content

Commit

Permalink
TUSFailure delegate method added in TUSFileManager
Browse files Browse the repository at this point in the history
  • Loading branch information
MMasterson committed May 5, 2020
1 parent c3827ce commit 3ac9418
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 14 deletions.
Binary file modified .DS_Store
Binary file not shown.
Binary file modified Example/.DS_Store
Binary file not shown.
4 changes: 2 additions & 2 deletions Example/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -40,7 +40,7 @@ SPEC CHECKSUMS:
Nimble: 051e3d8912d40138fa5591c78594f95fb172af37
Nimble-Snapshots: f5459b5b091678dc942d03ec4741cacb58ba4a52
Quick: 58d203b1c5e27fff7229c4c1ae445ad7069a7a08
TUSKit: 7626bfb27096011d3eb55f515b53b08f005913be
TUSKit: 95b4ad77dd21df1cc6662e6041d117104cd3cafe

PODFILE CHECKSUM: 572a5d87651b95d2aa1ecdf921075dfd78b37d95

Expand Down
10 changes: 9 additions & 1 deletion Example/TUSKit/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion TUSKit.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down
10 changes: 8 additions & 2 deletions TUSKit/Classes/TUSClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion TUSKit/Classes/TUSDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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?)

}
24 changes: 17 additions & 7 deletions TUSKit/Classes/TUSFileManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)

}
}
}
Expand All @@ -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
}
}

Expand All @@ -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
}
Expand Down
2 changes: 2 additions & 0 deletions TUSKit/Classes/TUSResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ import Foundation

public struct TUSResponse: Codable {

public var message: String?

}

0 comments on commit 3ac9418

Please sign in to comment.