Skip to content

Commit

Permalink
1.4.0
Browse files Browse the repository at this point in the history
Chunking
Upload to already exisiting file URL
Retry count
Save to disk fix
  • Loading branch information
MMasterson committed Apr 1, 2019
1 parent 4cffefe commit 95320a6
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Example/TUSKit/TKViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ - (void)imagePickerController:(UIImagePickerController *)picker didFinishPicking
}

// If a file has not been created yet by your TUS backend
TUSResumableUpload *upload = [self.tusSession createUploadFromFile:fileUrl headers:@{} metadata:@{}];
TUSResumableUpload *upload = [self.tusSession createUploadFromFile:fileUrl retry:-1 headers:@{} metadata:@{}];

upload.progressBlock = progressBlock;
upload.resultBlock = resultBlock;
Expand Down
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ To run the example project, clone the repo, and run `pod install` from the Examp
## The Protocol
You'll need a tus.io friendly server before using TUSKit or any other tus client. You can find a list of [tus implementations here](http://tus.io/implementations.html).

# Usage (1.3.0)
# Usage (1.4.0)
------
## TUSSession
A NSURLSession that manages, creates, and reloads TUS uploads using a single NSURLSession and data store.
Expand Down Expand Up @@ -50,11 +50,14 @@ The data storage for uploads.
## TUSResumableUpload
Easily add uploads to your data storage using the your TUSSession.

TUSResumableUpload *upload = [self.tusSession createUploadFromFile:fileUrl headers:@{} metadata:@{}];
TUSResumableUpload *upload = [self.tusSession createUploadFromFile:fileUrl retry:3 headers:@{} metadata:@{}];


**fileUrl** - URL To Local File.

**retry** - The number of times you wish the upload to retry.


**Headers** - An `NSDictionary` of your custom headers for the upload.

**Metadata** - Any extra metadata you wanna attach to your upload
Expand Down Expand Up @@ -122,9 +125,7 @@ TUSKit is a ready to use tus client for iOS.

# Todo
------
- [Add retry count](https://github.com/tus/TUSKit/issues/29)
- [SSL Support](https://github.com/tus/TUSKit/issues/32)
- [Uploadstore save to disk](https://github.com/tus/TUSKit/issues/33)
- [SSL Pinning](https://github.com/tus/TUSKit/issues/32)

# License
------
Expand Down
2 changes: 1 addition & 1 deletion TUSKit.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "TUSKit"
s.version = "1.3.13"
s.version = "1.4.0"
s.summary = "The tus client for iOS."
s.description = <<-DESC
An iOS implementation of the tus resumable video upload protocol.
Expand Down
1 change: 1 addition & 0 deletions TUSKit/TUSResumableUpload+Private.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@

- (instancetype _Nullable)initWithUploadId:(NSString * _Nonnull)uploadId
file:(NSURL * _Nonnull)fileUrl
retry:(int)retryCount
delegate:(id <TUSResumableUploadDelegate> _Nonnull)delegate
uploadHeaders:(NSDictionary <NSString *, NSString *>* _Nonnull)headers
metadata:(NSDictionary <NSString *, NSString *>* _Nullable)metadata;
Expand Down
30 changes: 25 additions & 5 deletions TUSKit/TUSResumableUpload.m
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ @interface TUSResumableUpload ()
@property (nonatomic, strong) NSURLSessionTask *currentTask; // Nonatomic because we know we will assign it, then start the thread that will remove it.
@property (nonatomic, strong) NSURL *fileUrl; // File URL for saving if we created our own TUSData
@property (readonly) long long length;
@property (nonatomic) int rertyCount; // Number of times to try
@property (nonatomic) int attempts; // Number of times tried


@property (nonatomic) long long chunkSize; //how big chunks we send to the server

Expand Down Expand Up @@ -138,6 +141,7 @@ - (instancetype _Nullable)initWithUploadId:(NSString * _Nonnull)uploadId

- (instancetype _Nullable)initWithUploadId:(NSString * _Nonnull)uploadId
file:(NSURL * _Nonnull)fileUrl
retry:(int)retryCount
delegate:(id <TUSResumableUploadDelegate> _Nonnull)delegate
uploadHeaders:(NSDictionary <NSString *, NSString *>* _Nonnull)headers
metadata:(NSDictionary <NSString *, NSString *>* _Nullable)metadata
Expand All @@ -157,6 +161,7 @@ - (instancetype _Nullable)initWithUploadId:(NSString * _Nonnull)uploadId

return [self initWithUploadId:uploadId
file:fileUrl
retry:retryCount
delegate:delegate
uploadHeaders:headers
finalMetadata:uploadMetadata
Expand All @@ -172,6 +177,7 @@ - (instancetype _Nullable)initWithUploadId:(NSString * _Nonnull)uploadId
*/
- (instancetype _Nullable) initWithUploadId:(NSString *)uploadId
file:(NSURL* _Nullable)fileUrl
retry:(int)retryCount
delegate:(id<TUSResumableUploadDelegate> _Nonnull)delegate
uploadHeaders:(NSDictionary <NSString *, NSString *>* _Nonnull)headers
finalMetadata:(NSDictionary <NSString *, NSString *>* _Nonnull)metadata
Expand All @@ -189,6 +195,8 @@ - (instancetype _Nullable) initWithUploadId:(NSString *)uploadId
_uploadUrl = uploadUrl;
_idle = YES;
_chunkSize = -1;
_rertyCount = retryCount;
_attempts = 0;

if (_state != TUSResumableUploadStateComplete){
_data = [[TUSFileData alloc] initWithFileURL:fileUrl];
Expand Down Expand Up @@ -349,10 +357,16 @@ - (BOOL)createFile
}
break;
default:
self.attempts++;
if (self.rertyCount == -1){
TUSLog(@"Infinite retry.");
}else if (self.attempts >= self.rertyCount){
[weakself stop];
}
//TODO: Fail after a certain number of delayed attempts
delayTime = DELAY_TIME;
TUSLog(@"Error or no response during attempt to create file, retrying");
}
TUSLog(@"Server not responding or error. Trying again. Attempt %i",
self.attempts); }
} else if (httpResponse.statusCode >= 500 && httpResponse.statusCode < 600) {
TUSLog(@"Server error, stopping");
[weakself stop]; // Will prevent continueUpload from doing anything
Expand All @@ -366,10 +380,16 @@ - (BOOL)createFile
}];
}
} else if (httpResponse.statusCode < 200 || httpResponse.statusCode > 204){
self.attempts++;
if (self.rertyCount == -1){
TUSLog(@"Infinite retry.");
}else if (self.attempts >= self.rertyCount){
[weakself stop];
}
//TODO: FAIL after a certain number of errors.
delayTime = DELAY_TIME;
TUSLog(@"Server responded to create file with %ld. Trying again",
(long)httpResponse.statusCode);
TUSLog(@"Server responded to create file with %ld. Trying again. Attempt %i",
(long)httpResponse.statusCode, self.attempts);
} else {
// Got a valid status code, so update url
NSString *location = [httpResponse.allHeaderFields valueForKey:HTTP_LOCATION];
Expand Down Expand Up @@ -650,7 +670,7 @@ -(NSDictionary *) serialize

return @{STORE_KEY_ID: self.uploadId,
STORE_KEY_DELEGATE_ENDPOINT: self.delegate.createUploadURL.absoluteString,
STORE_KEY_UPLOAD_URL: self.state == TUSResumableUploadStateCreatingFile? [NSNull null] : self.uploadUrl.absoluteString, //If we are creating the file, there is no upload URL
STORE_KEY_UPLOAD_URL: self.state == TUSResumableUploadStateCreatingFile? @"": self.uploadUrl.absoluteString, //If we are creating the file, there is no upload URL
STORE_KEY_LENGTH: @(self.length),
STORE_KEY_LAST_STATE: @(self.state),
STORE_KEY_METADATA: self.metadata,
Expand Down
1 change: 1 addition & 0 deletions TUSKit/TUSSession.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
Create an upload, but do not start it
*/
- (TUSResumableUpload * _Nullable) createUploadFromFile:(NSURL * _Nonnull)fileURL
retry:(int)retryCount
headers:(NSDictionary <NSString *, NSString *> * __nullable)headers
metadata:(NSDictionary <NSString *, NSString *> * __nullable)metadata;

Expand Down
2 changes: 2 additions & 0 deletions TUSKit/TUSSession.m
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,13 @@ - (id)initWithEndpoint:(NSURL *)endpoint

#pragma mark public methods
- (TUSResumableUpload *) createUploadFromFile:(NSURL *)fileURL
retry:(int)retryCount
headers:(NSDictionary <NSString *, NSString *> * __nullable)headers
metadata:(NSDictionary <NSString *, NSString *> * __nullable)metadata
{
TUSResumableUpload *upload = [[TUSResumableUpload alloc] initWithUploadId:[self.store generateUploadId]
file:fileURL
retry:retryCount
delegate:self
uploadHeaders:headers?:@{}
metadata:metadata];
Expand Down

0 comments on commit 95320a6

Please sign in to comment.