123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- /*****************************************************************************
- * VLCHTTPFileDownloader.m
- * VLC for iOS
- *****************************************************************************
- * Copyright (c) 2013 VideoLAN. All rights reserved.
- * $Id$
- *
- * Authors: Felix Paul Kühne <fkuehne # videolan.org>
- * Pierre Sagaspe <pierre.sagaspe # me.com>
- *
- * Refer to the COPYING file of the official project for license.
- *****************************************************************************/
- #import "VLCHTTPFileDownloader.h"
- #import "VLCAppDelegate.h"
- @interface VLCHTTPFileDownloader ()
- {
- NSString *_filePath;
- NSUInteger _expectedDownloadSize;
- NSUInteger _receivedDataSize;
- NSString *_fileName;
- NSURLConnection *_urlConnection;
- }
- @end
- @implementation VLCHTTPFileDownloader
- - (NSString *)userReadableDownloadName
- {
- return _fileName;
- }
- - (void)downloadFileFromURL:(NSURL *)url
- {
- NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
- _fileName = url.lastPathComponent;
- _filePath = [searchPaths[0] stringByAppendingPathComponent:_fileName];
- _expectedDownloadSize = _receivedDataSize = 0;
- NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
- _urlConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
- if (!_urlConnection) {
- APLog(@"failed to establish connection");
- _downloadInProgress = NO;
- } else {
- _downloadInProgress = YES;
- [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
- [(VLCAppDelegate*)[UIApplication sharedApplication].delegate disableIdleTimer];
- }
- }
- - (void)downloadFileFromURLwithFileName:(NSURL *)url fileNameOfMedia:(NSString*) fileName
- {
- NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
- _fileName = fileName;
- _filePath = [searchPaths[0] stringByAppendingPathComponent:_fileName];
- _expectedDownloadSize = _receivedDataSize = 0;
- NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
- _urlConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
- if (!_urlConnection) {
- APLog(@"failed to establish connection");
- _downloadInProgress = NO;
- } else {
- _downloadInProgress = YES;
- [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
- [(VLCAppDelegate*)[UIApplication sharedApplication].delegate disableIdleTimer];
- }
- }
- -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSHTTPURLResponse *)response
- {
- NSUInteger statusCode = [response statusCode];
- if (statusCode == 200) {
- _expectedDownloadSize = [response expectedContentLength];
- [self.delegate downloadStarted];
- APLog(@"expected download size: %i", _expectedDownloadSize);
- } else {
- APLog(@"unhandled status code %i", statusCode);
- if ([self.delegate respondsToSelector:@selector(downloadFailedWithErrorDescription:)])
- [self.delegate downloadFailedWithErrorDescription:[NSString stringWithFormat:NSLocalizedString(@"HTTP_DOWNLOAD_FAILED",nil), statusCode]];
- }
- }
- -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
- {
- NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:_filePath];
- if (!fileHandle) {
- // create file
- [[NSFileManager defaultManager] createFileAtPath:_filePath contents:nil attributes:nil];
- fileHandle = [NSFileHandle fileHandleForWritingAtPath:_filePath];
- if (!fileHandle) {
- APLog(@"file creation failed, no data was saved");
- if ([self.delegate respondsToSelector:@selector(downloadFailedWithErrorDescription:)])
- [self.delegate downloadFailedWithErrorDescription:NSLocalizedString(@"HTTP_FILE_CREATION_FAILED",nil)];
- return;
- }
- }
- @try {
- [fileHandle seekToEndOfFile];
- [fileHandle writeData:data];
- _receivedDataSize = _receivedDataSize + [data length];
- if ([self.delegate respondsToSelector:@selector(progressUpdatedTo:receivedDataSize:expectedDownloadSize:)])
- [self.delegate progressUpdatedTo: (float)_receivedDataSize / (float)_expectedDownloadSize receivedDataSize:_receivedDataSize expectedDownloadSize:_expectedDownloadSize];
- }
- @catch (NSException * e) {
- APLog(@"exception when writing to file %@", _filePath);
- }
- [fileHandle closeFile];
- }
- -(void)connectionDidFinishLoading:(NSURLConnection *)connection {
- APLog(@"http file download complete");
- VLCAppDelegate * appDelegate = [UIApplication sharedApplication].delegate;
- [appDelegate updateMediaList];
- [self _downloadEnded];
- }
- -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
- APLog(@"http file download failed (%i)", error.code);
- if ([self.delegate respondsToSelector:@selector(downloadFailedWithErrorDescription:)])
- [self.delegate downloadFailedWithErrorDescription:error.description];
- [self _downloadEnded];
- }
- - (void)cancelDownload
- {
- [_urlConnection cancel];
- /* remove partially downloaded content */
- NSFileManager *fileManager = [NSFileManager defaultManager];
- if ([fileManager fileExistsAtPath:_filePath])
- [fileManager removeItemAtPath:_filePath error:nil];
- if ([self.delegate respondsToSelector:@selector(downloadFailedWithErrorDescription:)])
- [self.delegate downloadFailedWithErrorDescription:NSLocalizedString(@"HTTP_DOWNLOAD_CANCELLED",nil)];
- [self _downloadEnded];
- }
- - (void)_downloadEnded
- {
- _downloadInProgress = NO;
- [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
- [(VLCAppDelegate*)[UIApplication sharedApplication].delegate activateIdleTimer];
- [self.delegate downloadEnded];
- }
- @end
|