2015-11-19 00:20:09 +00:00
# import "Downloader.h"
2015-11-23 16:29:25 +00:00
@ implementation DownloadParams
@ end
2015-11-19 00:20:09 +00:00
@ interface Downloader ( )
2015-11-23 16:29:25 +00:00
@ property ( copy ) DownloadParams * params ;
2015-11-19 00:20:09 +00:00
2015-11-23 16:29:25 +00:00
@ property ( retain ) NSURLConnection * connection ;
2015-11-19 00:20:09 +00:00
@ property ( retain ) NSNumber * statusCode ;
@ property ( retain ) NSNumber * contentLength ;
@ property ( retain ) NSNumber * bytesWritten ;
@ property ( retain ) NSFileHandle * fileHandle ;
@ end
@ implementation Downloader
2015-11-23 16:29:25 +00:00
- ( void ) downloadFile : ( DownloadParams * ) params
2015-11-19 00:20:09 +00:00
{
2015-11-23 16:29:25 +00:00
_params = params ;
2015-11-19 00:20:09 +00:00
_bytesWritten = 0 ;
2015-11-23 16:29:25 +00:00
NSURL * url = [ NSURL URLWithString : _params . fromUrl ] ;
2015-11-19 00:20:09 +00:00
NSMutableURLRequest * downloadRequest = [ NSMutableURLRequest requestWithURL : url
cachePolicy : NSURLRequestUseProtocolCachePolicy
timeoutInterval : 30 ] ;
2015-11-23 16:29:25 +00:00
[ [ NSFileManager defaultManager ] createFileAtPath : _params . toFile contents : nil attributes : nil ] ;
2015-11-19 00:20:09 +00:00
2015-11-23 16:29:25 +00:00
_fileHandle = [ NSFileHandle fileHandleForWritingAtPath : _params . toFile ] ;
2015-11-19 00:20:09 +00:00
if ( ! _fileHandle ) {
2015-11-23 16:29:25 +00:00
NSError * error = [ NSError errorWithDomain : @ "Downloader" code : NSURLErrorFileDoesNotExist userInfo : @ { NSLocalizedDescriptionKey : [ NSString stringWithFormat : @ "Failed to create target file at path: %@" , _params . toFile ] } ] ;
2015-11-19 00:20:09 +00:00
2015-11-23 16:29:25 +00:00
return _params . errorCallback ( error ) ;
2015-11-19 00:20:09 +00:00
}
2015-11-23 16:29:25 +00:00
_connection = [ [ NSURLConnection alloc ] initWithRequest : downloadRequest delegate : self startImmediately : NO ] ;
2015-11-19 00:20:09 +00:00
2015-11-23 16:29:25 +00:00
[ _connection scheduleInRunLoop : [ NSRunLoop mainRunLoop ] forMode : NSDefaultRunLoopMode ] ;
2015-11-19 00:20:09 +00:00
2015-11-23 16:29:25 +00:00
[ _connection start ] ;
2015-11-19 00:20:09 +00:00
}
- ( void ) connection : ( NSURLConnection * ) connection didFailWithError : ( NSError * ) error
{
[ _fileHandle closeFile ] ;
2015-11-23 16:29:25 +00:00
return _params . errorCallback ( error ) ;
2015-11-19 00:20:09 +00:00
}
- ( void ) connection : ( NSURLConnection * ) connection didReceiveResponse : ( NSURLResponse * ) response
{
NSHTTPURLResponse * httpUrlResponse = ( NSHTTPURLResponse * ) response ;
_statusCode = [ NSNumber numberWithLong : httpUrlResponse . statusCode ] ;
_contentLength = [ NSNumber numberWithLong : httpUrlResponse . expectedContentLength ] ;
2015-11-23 16:29:25 +00:00
return _params . beginCallback ( _statusCode , _contentLength , httpUrlResponse . allHeaderFields ) ;
2015-11-19 00:20:09 +00:00
}
- ( void ) connection : ( NSURLConnection * ) connection didReceiveData : ( NSData * ) data
{
if ( [ _statusCode isEqualToNumber : [ NSNumber numberWithInt : 200 ] ] ) {
[ _fileHandle writeData : data ] ;
_bytesWritten = [ NSNumber numberWithUnsignedInteger : [ _bytesWritten unsignedIntegerValue ] + data . length ] ;
2015-11-23 16:29:25 +00:00
return _params . progressCallback ( _contentLength , _bytesWritten ) ;
2015-11-19 00:20:09 +00:00
}
}
- ( void ) connectionDidFinishLoading : ( NSURLConnection * ) connection
{
[ _fileHandle closeFile ] ;
2015-11-23 16:29:25 +00:00
return _params . callback ( _statusCode , _bytesWritten ) ;
}
- ( void ) stopDownload
{
[ _connection cancel ] ;
2015-11-19 00:20:09 +00:00
}
@ end