Skip to content

Commit

Permalink
Move offset setting and store in userdefaults
Browse files Browse the repository at this point in the history
  • Loading branch information
MMasterson committed May 4, 2020
1 parent cee088b commit 9c08336
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 4 deletions.
1 change: 1 addition & 0 deletions TUSKit/Classes/TUSClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public class TUSClient: NSObject {
case .new:
logger.log(String(format: "Creating file %@ on server", upload.id!))
upload.contentLength = "0"
upload.uploadOffset = "0"
upload.uploadLength = String(fileManager.sizeForLocalFilePath(filePath: String(format: "%@%@", fileManager.fileStorePath(), fileName)))
executor.create(forUpload: upload)
break
Expand Down
6 changes: 6 additions & 0 deletions TUSKit/Classes/TUSConstants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ struct TUSConstants {
static let kSavedTUSUploadStatusDefaultsKey = "tusUploadSavedStatusForId-"
static let kSavedTUSUploadLengthDefaultsKey = "tusUploadSavedUploadLengthForId-"
static let kSavedTUSUContentLengthDefaultsKey = "tusUploadSavedContentLengthForId-"
static let kSavedTUSUploadOffsetDefaultsKey = "tusUploadSavedUploadOffsetForId-"


static let chunkSize = 5 // in MB

Expand All @@ -29,4 +31,8 @@ struct TUSConstants {
static func defaultsUploadLengthKey(forId id: String) -> String {
return String(format: "%@%@", TUSConstants.kSavedTUSUploadLengthDefaultsKey, id)
}

static func defaultsUploadOffsetKey(forId id: String) -> String {
return String(format: "%@%@", TUSConstants.kSavedTUSUploadOffsetDefaultsKey, id)
}
}
9 changes: 5 additions & 4 deletions TUSKit/Classes/TUSExecutor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,20 +67,21 @@ class TUSExecutor: NSObject {

let chunks = dataIntoChunks(data: uploadData as NSData, chunkSize: 200000) as [Data]
//Then we start the upload from the first chunk
self.upload(forChunks: chunks, withUpload: upload, atPosition: 0, andOffset: "0")
self.upload(forChunks: chunks, withUpload: upload, atPosition: 0)
}

private func upload(forChunks chunks: [Data], withUpload upload: TUSUpload, atPosition position: Int, andOffset offset: String ) {
private func upload(forChunks chunks: [Data], withUpload upload: TUSUpload, atPosition position: Int) {
TUSClient.shared.logger.log(String(format: "Upload starting for file %@ - Chunk %u / %u", upload.id!, position + 1, chunks.count))
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": offset, "Content-Length": String(chunks[position].count)])
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
if (chunks.count > position+1 ){
self.upload(forChunks: chunks, withUpload: upload, atPosition: position+1, andOffset: httpResponse.allHeaderFields["upload-offset"] as! String)
upload.uploadOffset = httpResponse.allHeaderFields["upload-offset"] as! String
self.upload(forChunks: chunks, withUpload: upload, atPosition: position+1)
} else
if (httpResponse.statusCode == 204) {
TUSClient.shared.logger.log(String(format: "Chunk %u / %u complete", position + 1, chunks.count))
Expand Down
12 changes: 12 additions & 0 deletions TUSKit/Classes/TUSUpload.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,18 @@ public class TUSUpload: NSObject {
}
}

var uploadOffset: String? {
get {
guard let uploadLength = UserDefaults.standard.value(forKey: TUSConstants.defaultsUploadOffsetKey(forId: id!)) as? String else {
return nil
}
return uploadLength
}
set(uploadLength) {
UserDefaults.standard.set(uploadLength, forKey: String(format: "%@", TUSConstants.defaultsUploadOffsetKey(forId: id!)))
}
}

var status: TUSUploadStatus? {
get {
guard let status = UserDefaults.standard.value(forKey: TUSConstants.defaultsStatusKey(forId: id!)) as? String else {
Expand Down

0 comments on commit 9c08336

Please sign in to comment.