123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- //
- // 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;
- [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
- }
- }
- -(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;
- [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
- VLCAppDelegate * appDelegate = [UIApplication sharedApplication].delegate;
- [appDelegate updateMediaList];
- [_mediaViewController dismiss:nil];
- }
- -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
- APLog(@"http file download failed (%i)", error.code);
- _downloadInProgress = NO;
- _progressIndicator.hidden = YES;
- [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
- [_mediaViewController dismiss:nil];
- }
- @end
|