VLCHTTPFileDownloader.m 3.29 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
//
//  VLCHTTPFileDownloader.m
//  VLC for iOS
//
//  Created by Felix Paul Kühne on 20.05.13.
//  Copyright (c) 2013 VideoLAN. All rights reserved.
//

#import "VLCHTTPFileDownloader.h"
#import "VLCAddMediaViewController.h"
#import "VLCCircularProgressIndicator.h"
#import "VLCAppDelegate.h"

@interface VLCHTTPFileDownloader ()
{
    VLCCircularProgressIndicator *_progressIndicator;
    NSString *_filePath;
    NSUInteger _expectedDownloadSize;
    NSUInteger _receivedDataSize;
}

@end

@implementation VLCHTTPFileDownloader

- (void)downloadFileFromURL:(NSURL *)url
{
    _progressIndicator = self.mediaViewController.httpDownloadProgressIndicator;
    _progressIndicator.progress = 0.;
    NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    _filePath = [searchPaths[0] stringByAppendingPathComponent:url.lastPathComponent];
    _expectedDownloadSize = _receivedDataSize = 0;
    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
    NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
    if (!theConnection) {
        APLog(@"failed to establish connection");
        _downloadInProgress = NO;
    } else {
        _downloadInProgress = YES;
        _progressIndicator.hidden = NO;
41
        [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
    }
}

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSHTTPURLResponse *)response
{
    NSUInteger statusCode = [response statusCode];
    if (statusCode == 200) {
        _expectedDownloadSize = [response expectedContentLength];
        APLog(@"expected download size: %i", _expectedDownloadSize);
    }
}

-(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");
            return;
        }
    }

    @try {
        [fileHandle seekToEndOfFile];
        [fileHandle writeData:data];

        _receivedDataSize = _receivedDataSize + data.length;
        _progressIndicator.progress = (float)_receivedDataSize / (float)_expectedDownloadSize;
    }
    @catch (NSException * e) {
        APLog(@"exception when writing to file %@", _filePath);
    }

    [fileHandle closeFile];
}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection {
    APLog(@"http file download complete");
    _downloadInProgress = NO;
    _progressIndicator.hidden = YES;
86
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
87 88 89

    VLCAppDelegate * appDelegate = [UIApplication sharedApplication].delegate;
    [appDelegate updateMediaList];
90 91

    [_mediaViewController dismiss:nil];
92 93 94 95 96 97
}

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    APLog(@"http file download failed (%i)", error.code);
    _downloadInProgress = NO;
    _progressIndicator.hidden = YES;
98
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
99 100

    [_mediaViewController dismiss:nil];
101 102 103
}

@end