Skip to content

Commit 95320a6

Browse files
committed
1.4.0
Chunking Upload to already exisiting file URL Retry count Save to disk fix
1 parent 4cffefe commit 95320a6

File tree

7 files changed

+37
-12
lines changed

7 files changed

+37
-12
lines changed

Example/TUSKit/TKViewController.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ - (void)imagePickerController:(UIImagePickerController *)picker didFinishPicking
107107
}
108108

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

112112
upload.progressBlock = progressBlock;
113113
upload.resultBlock = resultBlock;

README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ To run the example project, clone the repo, and run `pod install` from the Examp
2222
## The Protocol
2323
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).
2424

25-
# Usage (1.3.0)
25+
# Usage (1.4.0)
2626
------
2727
## TUSSession
2828
A NSURLSession that manages, creates, and reloads TUS uploads using a single NSURLSession and data store.
@@ -50,11 +50,14 @@ The data storage for uploads.
5050
## TUSResumableUpload
5151
Easily add uploads to your data storage using the your TUSSession.
5252

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

5555

5656
**fileUrl** - URL To Local File.
5757

58+
**retry** - The number of times you wish the upload to retry.
59+
60+
5861
**Headers** - An `NSDictionary` of your custom headers for the upload.
5962

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

123126
# Todo
124127
------
125-
- [Add retry count](https://github.com/tus/TUSKit/issues/29)
126-
- [SSL Support](https://github.com/tus/TUSKit/issues/32)
127-
- [Uploadstore save to disk](https://github.com/tus/TUSKit/issues/33)
128+
- [SSL Pinning](https://github.com/tus/TUSKit/issues/32)
128129

129130
# License
130131
------

TUSKit.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Pod::Spec.new do |s|
22
s.name = "TUSKit"
3-
s.version = "1.3.13"
3+
s.version = "1.4.0"
44
s.summary = "The tus client for iOS."
55
s.description = <<-DESC
66
An iOS implementation of the tus resumable video upload protocol.

TUSKit/TUSResumableUpload+Private.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666

6767
- (instancetype _Nullable)initWithUploadId:(NSString * _Nonnull)uploadId
6868
file:(NSURL * _Nonnull)fileUrl
69+
retry:(int)retryCount
6970
delegate:(id <TUSResumableUploadDelegate> _Nonnull)delegate
7071
uploadHeaders:(NSDictionary <NSString *, NSString *>* _Nonnull)headers
7172
metadata:(NSDictionary <NSString *, NSString *>* _Nullable)metadata;

TUSKit/TUSResumableUpload.m

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ @interface TUSResumableUpload ()
6767
@property (nonatomic, strong) NSURLSessionTask *currentTask; // Nonatomic because we know we will assign it, then start the thread that will remove it.
6868
@property (nonatomic, strong) NSURL *fileUrl; // File URL for saving if we created our own TUSData
6969
@property (readonly) long long length;
70+
@property (nonatomic) int rertyCount; // Number of times to try
71+
@property (nonatomic) int attempts; // Number of times tried
72+
7073

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

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

139142
- (instancetype _Nullable)initWithUploadId:(NSString * _Nonnull)uploadId
140143
file:(NSURL * _Nonnull)fileUrl
144+
retry:(int)retryCount
141145
delegate:(id <TUSResumableUploadDelegate> _Nonnull)delegate
142146
uploadHeaders:(NSDictionary <NSString *, NSString *>* _Nonnull)headers
143147
metadata:(NSDictionary <NSString *, NSString *>* _Nullable)metadata
@@ -157,6 +161,7 @@ - (instancetype _Nullable)initWithUploadId:(NSString * _Nonnull)uploadId
157161

158162
return [self initWithUploadId:uploadId
159163
file:fileUrl
164+
retry:retryCount
160165
delegate:delegate
161166
uploadHeaders:headers
162167
finalMetadata:uploadMetadata
@@ -172,6 +177,7 @@ - (instancetype _Nullable)initWithUploadId:(NSString * _Nonnull)uploadId
172177
*/
173178
- (instancetype _Nullable) initWithUploadId:(NSString *)uploadId
174179
file:(NSURL* _Nullable)fileUrl
180+
retry:(int)retryCount
175181
delegate:(id<TUSResumableUploadDelegate> _Nonnull)delegate
176182
uploadHeaders:(NSDictionary <NSString *, NSString *>* _Nonnull)headers
177183
finalMetadata:(NSDictionary <NSString *, NSString *>* _Nonnull)metadata
@@ -189,6 +195,8 @@ - (instancetype _Nullable) initWithUploadId:(NSString *)uploadId
189195
_uploadUrl = uploadUrl;
190196
_idle = YES;
191197
_chunkSize = -1;
198+
_rertyCount = retryCount;
199+
_attempts = 0;
192200

193201
if (_state != TUSResumableUploadStateComplete){
194202
_data = [[TUSFileData alloc] initWithFileURL:fileUrl];
@@ -349,10 +357,16 @@ - (BOOL)createFile
349357
}
350358
break;
351359
default:
360+
self.attempts++;
361+
if (self.rertyCount == -1){
362+
TUSLog(@"Infinite retry.");
363+
}else if (self.attempts >= self.rertyCount){
364+
[weakself stop];
365+
}
352366
//TODO: Fail after a certain number of delayed attempts
353367
delayTime = DELAY_TIME;
354-
TUSLog(@"Error or no response during attempt to create file, retrying");
355-
}
368+
TUSLog(@"Server not responding or error. Trying again. Attempt %i",
369+
self.attempts); }
356370
} else if (httpResponse.statusCode >= 500 && httpResponse.statusCode < 600) {
357371
TUSLog(@"Server error, stopping");
358372
[weakself stop]; // Will prevent continueUpload from doing anything
@@ -366,10 +380,16 @@ - (BOOL)createFile
366380
}];
367381
}
368382
} else if (httpResponse.statusCode < 200 || httpResponse.statusCode > 204){
383+
self.attempts++;
384+
if (self.rertyCount == -1){
385+
TUSLog(@"Infinite retry.");
386+
}else if (self.attempts >= self.rertyCount){
387+
[weakself stop];
388+
}
369389
//TODO: FAIL after a certain number of errors.
370390
delayTime = DELAY_TIME;
371-
TUSLog(@"Server responded to create file with %ld. Trying again",
372-
(long)httpResponse.statusCode);
391+
TUSLog(@"Server responded to create file with %ld. Trying again. Attempt %i",
392+
(long)httpResponse.statusCode, self.attempts);
373393
} else {
374394
// Got a valid status code, so update url
375395
NSString *location = [httpResponse.allHeaderFields valueForKey:HTTP_LOCATION];
@@ -650,7 +670,7 @@ -(NSDictionary *) serialize
650670

651671
return @{STORE_KEY_ID: self.uploadId,
652672
STORE_KEY_DELEGATE_ENDPOINT: self.delegate.createUploadURL.absoluteString,
653-
STORE_KEY_UPLOAD_URL: self.state == TUSResumableUploadStateCreatingFile? [NSNull null] : self.uploadUrl.absoluteString, //If we are creating the file, there is no upload URL
673+
STORE_KEY_UPLOAD_URL: self.state == TUSResumableUploadStateCreatingFile? @"": self.uploadUrl.absoluteString, //If we are creating the file, there is no upload URL
654674
STORE_KEY_LENGTH: @(self.length),
655675
STORE_KEY_LAST_STATE: @(self.state),
656676
STORE_KEY_METADATA: self.metadata,

TUSKit/TUSSession.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
Create an upload, but do not start it
2929
*/
3030
- (TUSResumableUpload * _Nullable) createUploadFromFile:(NSURL * _Nonnull)fileURL
31+
retry:(int)retryCount
3132
headers:(NSDictionary <NSString *, NSString *> * __nullable)headers
3233
metadata:(NSDictionary <NSString *, NSString *> * __nullable)metadata;
3334

TUSKit/TUSSession.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,13 @@ - (id)initWithEndpoint:(NSURL *)endpoint
8484

8585
#pragma mark public methods
8686
- (TUSResumableUpload *) createUploadFromFile:(NSURL *)fileURL
87+
retry:(int)retryCount
8788
headers:(NSDictionary <NSString *, NSString *> * __nullable)headers
8889
metadata:(NSDictionary <NSString *, NSString *> * __nullable)metadata
8990
{
9091
TUSResumableUpload *upload = [[TUSResumableUpload alloc] initWithUploadId:[self.store generateUploadId]
9192
file:fileURL
93+
retry:retryCount
9294
delegate:self
9395
uploadHeaders:headers?:@{}
9496
metadata:metadata];

0 commit comments

Comments
 (0)