Skip to content
This repository has been archived by the owner on Oct 1, 2018. It is now read-only.

Commit

Permalink
Refactored to use centralized downloader.
Browse files Browse the repository at this point in the history
Implemented using NSURLSession which can be configured with headers. (In
a future PR).

Removed now unused network related files.
  • Loading branch information
davidovich committed Nov 17, 2015
1 parent b3ff31f commit e553c77
Show file tree
Hide file tree
Showing 13 changed files with 147 additions and 438 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.DS_Store
2 changes: 0 additions & 2 deletions src/ios/HCPPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
#import <Cordova/CDVConfigParser.h>

#import "HCPPlugin.h"
#import "HCPApplicationConfig+Downloader.h"
#import "HCPContentManifest+Downloader.h"
#import "HCPFileDownloader.h"
#import "HCPFilesStructure.h"
#import "HCPFilesStructureImpl.h"
Expand Down
42 changes: 0 additions & 42 deletions src/ios/Network/HCPApplicationConfig+Downloader.h

This file was deleted.

35 changes: 0 additions & 35 deletions src/ios/Network/HCPApplicationConfig+Downloader.m

This file was deleted.

42 changes: 0 additions & 42 deletions src/ios/Network/HCPContentManifest+Downloader.h

This file was deleted.

35 changes: 0 additions & 35 deletions src/ios/Network/HCPContentManifest+Downloader.m

This file was deleted.

28 changes: 16 additions & 12 deletions src/ios/Network/HCPFileDownloader.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,38 @@
*
* @param holds information about occured error; <code>nil</code> if everything is fine
*/
typedef void (^HCPFileDownloadComplitionBlock)(NSError *error);
typedef void (^HCPFileDownloadCompletionBlock)(NSError *);
typedef void (^HCPDataDownloadCompletionBlock)(NSData*, NSError *);

/**
* Helper class to download files from the server.
*/
@interface HCPFileDownloader : NSObject

/**
* Download file asynchronously.
*
* @param url url to the downloaded file
* @param filePath url in local file system where to put loaded file
* @param checksum hash of the file to check if it's not corrupted
* @param block download complition block
*/
- (void)downloadFileFromUrl:(NSURL *)url saveToFile:(NSURL *)filePath checksum:(NSString *)checksum complitionBlock:(HCPFileDownloadComplitionBlock)block;
- (void) downloadDataFromUrl:(NSURL*) url completionBlock:(HCPDataDownloadCompletionBlock) block;

/**
* Download list of files asynchronously.
*
* @param filesList list of files to download. Files are instances of HCPManifestFile class.
* @param contentURL url on the server where all files are located. Full URL to the file is constructed from this one and the files mame.
* @param folderURL url to the directory in local file system where to put all loaded files
* @param block download complition block
* @param block download completion block
*
* @see HCPManifestFile
*/
- (void)downloadFiles:(NSArray *)filesList fromURL:(NSURL *)contentURL toFolder:(NSURL *)folderURL complitionBlock:(HCPFileDownloadComplitionBlock)block;
- (void) downloadFiles:(NSArray *)filesList fromURL:(NSURL *)contentURL toFolder:(NSURL *)folderURL completionBlock:(HCPFileDownloadCompletionBlock)block;


/**
* Download file asynchronously.
*
* @param url url to the downloaded file
* @param filePath url in local file system where to put loaded file
* @param checksum hash of the file to check if it's not corrupted
* @param block download completion block
*/
- (void)downloadFileFromUrl:(NSURL *)url saveToFile:(NSURL *)filePath checksum:(NSString *)checksum completionBlock:(HCPFileDownloadCompletionBlock)block;

/**
* Download list of files synchronously.
Expand Down
115 changes: 47 additions & 68 deletions src/ios/Network/HCPFileDownloader.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,83 +13,62 @@ @implementation HCPFileDownloader

#pragma mark Public API

- (void)downloadFileFromUrl:(NSURL *)url saveToFile:(NSURL *)filePath checksum:(NSString *)checksum complitionBlock:(HCPFileDownloadComplitionBlock)block {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSError *error = nil;
[self executeFileDownloadFromURL:url saveToFile:filePath checksum:checksum error:&error];
block(error);
});
}

- (void)downloadFiles:(NSArray *)filesList fromURL:(NSURL *)contentURL toFolder:(NSURL *)folderURL complitionBlock:(HCPFileDownloadComplitionBlock)block {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSError *error = nil;
[self executeDownloadOfFiles:filesList fromURL:contentURL toFolder:folderURL error:&error];
block(error);
});
}

- (BOOL)downloadFilesSync:(NSArray *)filesList fromURL:(NSURL *)contentURL toFolder:(NSURL *)folderURL error:(NSError **)error {
[self executeDownloadOfFiles:filesList fromURL:contentURL toFolder:folderURL error:error];
- (void) downloadDataFromUrl:(NSURL*) url completionBlock:(HCPDataDownloadCompletionBlock) block {

return (*error == nil);
}

- (BOOL)downloadFileSyncFromUrl:(NSURL *)url saveToFile:(NSURL *)filePath checksum:(NSString *)checksum error:(NSError **)error {
[self executeFileDownloadFromURL:url saveToFile:filePath checksum:checksum error:error];
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration];

NSURLSessionDataTask* dowloadTask = [session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
block(data, error);
}];

return (*error == nil);
[dowloadTask resume];
}

#pragma mark Private API

/**
* Perform download of the list of files
*
* @param filesList list of files to download
* @param contentURL base url for all the loaded files
* @param folderURL where to put loaded files on the file system
* @param error error information if any occure; <code>nil</code> if all files are loaded
*/
- (void)executeDownloadOfFiles:(NSArray *)filesList fromURL:(NSURL *)contentURL toFolder:(NSURL *)folderURL error:(NSError **)error {
for (HCPManifestFile *file in filesList) {
NSURL *filePathOnFileSystem = [folderURL URLByAppendingPathComponent:file.name isDirectory:NO];
NSURL *fileUrlOnServer = [contentURL URLByAppendingPathComponent:file.name isDirectory:NO];
BOOL isDownloaded = [self executeFileDownloadFromURL:fileUrlOnServer saveToFile:filePathOnFileSystem checksum:file.md5Hash error:error];
if (!isDownloaded) {
break;
}
- (void) downloadFiles:(NSArray *)filesList fromURL:(NSURL *)contentURL toFolder:(NSURL *)folderURL completionBlock:(HCPFileDownloadCompletionBlock)block {
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration];

__block NSMutableSet* startedTasks = [NSMutableSet set];
for (HCPManifestFile *file in filesList)
{
NSURL *url = [contentURL URLByAppendingPathComponent:file.name];
__block NSURLSessionDataTask *downloadTask = [session dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSError* operationError = nil;
if (error) {
[session invalidateAndCancel];
operationError = error;
}

if (!error && ![self isDataCorrupted:data checksum:file.md5Hash error:&error]) {
NSURL *finalPath = [folderURL URLByAppendingPathComponent:file.name];
[self prepareFileForSaving:finalPath];

BOOL success = [data writeToURL:finalPath options:kNilOptions error:&error];
if (success) {
NSLog(@"Loaded file %@", file.name);
[startedTasks removeObject:downloadTask];
} else {
[session invalidateAndCancel];
operationError = error;
}
}
// operations finishes,
if (startedTasks.count == 0 || operationError) {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
block(operationError);
});
}

}];

NSLog(@"Loaded file %@", file.name);
[startedTasks addObject:downloadTask];
[downloadTask resume];
}
}

/**
* Perform download of the file from the provided url
*
* @param url url from which to downlaod the file
* @param fileURL where to save file on the external storage
* @param checksum file checksum to validate it after the download
* @param error error information if any occure; <code>nil</code> on download success
*
* @return <code>YES</code> if file is downloaded; <code>NO</code> if we failed to download
*/
- (BOOL)executeFileDownloadFromURL:(NSURL *)url saveToFile:(NSURL *)fileURL checksum:(NSString *)checksum error:(NSError **)error {
*error = nil;
NSData *downloadedContent = [NSData dataWithContentsOfURL:url];
if (downloadedContent == nil) {
NSString *message = [NSString stringWithFormat:@"Failed to load file: %@", url];
*error = [NSError errorWithCode:0 description:message];
return NO;
}

if (![self isDataCorrupted:downloadedContent checksum:checksum error:error]) {
[self prepareFileForSaving:fileURL];
[downloadedContent writeToURL:fileURL options:kNilOptions error:error];
}

return (*error == nil);
}


/**
* Check if data was corrupted during the download.
Expand Down
Loading

0 comments on commit e553c77

Please sign in to comment.