Skip to content

Commit

Permalink
fix: absolute paths not working in sandbox and not automatically resu…
Browse files Browse the repository at this point in the history
…ming upload tasks that were pending upon app kill/restart
  • Loading branch information
harsh62 committed Sep 28, 2021
1 parent 212806d commit ebdf97d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 15 deletions.
30 changes: 17 additions & 13 deletions AWSS3/AWSS3TransferUtility.m
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
static NSString *const AWSS3TransferUtilityRetrySucceeded = @"AWSS3TransferUtilityRetrySucceeded";
static NSUInteger const AWSS3TransferUtilityMultiPartSize = 5 * 1024 * 1024;
static NSString *const AWSS3TransferUtiltityRequestTimeoutErrorCode = @"RequestTimeout";
static int const AWSS3TransferUtilityMultiPartDefaultConcurrencyLimit = 5;
static int const AWSS3TransferUtilityMultiPartDefaultConcurrencyLimit = 10;


#pragma mark - Private classes
Expand Down Expand Up @@ -67,7 +67,7 @@ @interface AWSS3TransferUtility() <NSURLSessionDelegate, NSURLSessionTaskDelegat
@property (strong, nonatomic) AWSS3 *s3;
@property (strong, nonatomic) NSURLSession *session;
@property (strong, nonatomic) NSString *sessionIdentifier;
@property (strong, nonatomic) NSString *cacheDirectoryPath;
@property (strong, nonatomic, readonly) NSString *cacheDirectoryPath;
@property (strong, nonatomic) AWSSynchronizedMutableDictionary *taskDictionary;
@property (strong, nonatomic) AWSSynchronizedMutableDictionary *completedTaskDictionary;
@property (copy, nonatomic) void (^backgroundURLSessionCompletionHandler)(void);
Expand Down Expand Up @@ -245,6 +245,13 @@ @implementation AWSS3TransferUtility
static AWSSynchronizedMutableDictionary *_serviceClients = nil;
static AWSS3TransferUtility *_defaultS3TransferUtility = nil;

- (NSString *)cacheDirectoryPath {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachePath = [paths objectAtIndex:0];
[cachePath stringByAppendingPathComponent:AWSInfoS3TransferUtility];
return cachePath;
}

#pragma mark - Initialization methods

+ (instancetype)defaultS3TransferUtility {
Expand Down Expand Up @@ -325,8 +332,10 @@ + (void)registerS3TransferUtilityWithConfiguration:(AWSServiceConfiguration *)co
recoverState:NO
completionHandler:completionHandler];
if (s3TransferUtility) {
NSAssert(_serviceClients != nil, @"Value is required");
[_serviceClients setObject:s3TransferUtility
forKey:key];
NSAssert(_serviceClients.allKeys.count > 0, @"A value must now be set");
[s3TransferUtility recover:completionHandler];
}
}
Expand Down Expand Up @@ -407,13 +416,8 @@ - (instancetype)initWithConfiguration:(AWSServiceConfiguration *)serviceConfigur
if (self = [super init]) {

// Create a temporary directory for data uploads in the caches directory
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachePath = [paths objectAtIndex:0];

_cacheDirectoryPath = [cachePath stringByAppendingPathComponent:AWSInfoS3TransferUtility];
AWSDDLogDebug(@"Temporary dir Path is %@", _cacheDirectoryPath);

NSURL *directoryURL = [NSURL fileURLWithPath:_cacheDirectoryPath];
AWSDDLogDebug(@"Temporary dir Path is %@", self.cacheDirectoryPath);
NSURL *directoryURL = [NSURL fileURLWithPath:self.cacheDirectoryPath];
NSError *error = nil;
BOOL result = [[NSFileManager defaultManager] createDirectoryAtURL:directoryURL
withIntermediateDirectories:YES
Expand Down Expand Up @@ -484,7 +488,7 @@ - (instancetype)initWithConfiguration:(AWSServiceConfiguration *)serviceConfigur
_completedTaskDictionary = [AWSSynchronizedMutableDictionary new];

//Instantiate the Database Helper
self.databaseQueue = [AWSS3TransferUtilityDatabaseHelper createDatabase:_cacheDirectoryPath];
self.databaseQueue = [AWSS3TransferUtilityDatabaseHelper createDatabase:self.cacheDirectoryPath];

if (recoverState) {
//Recover the state from the previous time this was instantiated
Expand Down Expand Up @@ -1374,9 +1378,6 @@ - (void) retryUpload: (AWSS3TransferUtilityUploadTask *) transferUtilityUploadTa
subTask.file = @"";
subTask.eTag = @"";

//Save in Database
[AWSS3TransferUtilityDatabaseHelper insertMultiPartUploadRequestSubTaskInDB:transferUtilityMultiPartUploadTask subTask:subTask databaseQueue:self.databaseQueue];

NSError *subTaskCreationError;

//Move to inProgress or Waiting based on concurrency limit
Expand All @@ -1395,6 +1396,9 @@ - (void) retryUpload: (AWSS3TransferUtilityUploadTask *) transferUtilityUploadTa
}
}

//Save in Database after the file has been created, so that file can be referenced incase upload is paused and needs to be restarted.
[AWSS3TransferUtilityDatabaseHelper insertMultiPartUploadRequestSubTaskInDB:transferUtilityMultiPartUploadTask subTask:subTask databaseQueue:self.databaseQueue];

if ( subTaskCreationError) {
//Abort the request, so the server can clean up any partials.
[self callAbortMultiPartForUploadTask:transferUtilityMultiPartUploadTask];
Expand Down
18 changes: 16 additions & 2 deletions AWSS3/AWSS3TransferUtilityDatabaseHelper.m
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ + (void) insertTransferRequestInDB: (NSString *) transferID
@"part_number": partNumber,
@"multi_part_id": multiPartID,
@"etag": eTag,
@"file": file,
@"file": [AWSS3TransferUtilityDatabaseHelper relativePathFromAbsolutePath:file],
@"temporary_file_created": tempFileCreated,
@"content_length": contentLength,
@"status": [AWSS3TransferUtilityDatabaseHelper getStringRepresentation:status],
Expand Down Expand Up @@ -363,7 +363,7 @@ + (NSMutableArray *) getTransferTaskDataFromDB:(NSString *)nsURLSessionID
[transfer setObject:@([rs intForColumn:@"part_number"]) forKey:@"part_number"];
[transfer setObject:[rs stringForColumn:@"multi_part_id"] forKey:@"multi_part_id"];
[transfer setObject:[rs stringForColumn:@"etag"] forKey:@"etag"];
[transfer setObject:[rs stringForColumn:@"file"] forKey:@"file"];
[transfer setObject:[AWSS3TransferUtilityDatabaseHelper absolutePathFromRelativePath:[rs stringForColumn:@"file"]] forKey:@"file"];
[transfer setObject:@([rs intForColumn:@"temporary_file_created"]) forKey:@"temporary_file_created"];
[transfer setObject:@([rs intForColumn:@"content_length"]) forKey:@"content_length"];
[transfer setObject:@([rs intForColumn:@"retry_count"]) forKey:@"retry_count"];
Expand Down Expand Up @@ -433,5 +433,19 @@ + (AWSS3TransferUtilityTransferStatusType) getEnumRepresentation: (NSString *) s
}
return AWSS3TransferUtilityTransferStatusUnknown;
}

+ (NSString*)absolutePathFromRelativePath:(NSString*)relativePath
{
if (relativePath.length == 0) {
return relativePath;
}
return [NSHomeDirectory() stringByAppendingPathComponent:relativePath];
}

+ (NSString*)relativePathFromAbsolutePath:(NSString*)absolutePath
{
return [absolutePath stringByReplacingOccurrencesOfString:NSHomeDirectory() withString:@""];
}

@end

0 comments on commit ebdf97d

Please sign in to comment.