VLCHTTPFileDownloader.m 8.94 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
/*****************************************************************************
 * 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.
 *****************************************************************************/
13 14

#import "VLCHTTPFileDownloader.h"
15
#import "NSString+SupportedMedia.h"
16
#import "VLCAppDelegate.h"
17
#import "UIDevice+VLC.h"
18 19 20 21 22 23

@interface VLCHTTPFileDownloader ()
{
    NSString *_filePath;
    NSUInteger _expectedDownloadSize;
    NSUInteger _receivedDataSize;
24 25
    NSString *_fileName;
    NSURLConnection *_urlConnection;
26
    NSMutableURLRequest *_originalRequest;
27
    NSUInteger _statusCode;
28 29 30 31 32 33
}

@end

@implementation VLCHTTPFileDownloader

34 35 36 37 38
- (NSString *)userReadableDownloadName
{
    return _fileName;
}

39 40
- (void)downloadFileFromURL:(NSURL *)url
{
41
    [self downloadFileFromURL:url withFileName:nil];
42 43
}

44
- (void)downloadFileFromURL:(NSURL *)url withFileName:(NSString*)fileName
Pierre SAGASPE's avatar
Pierre SAGASPE committed
45
{
46
    NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
47
    NSString *basePath = [searchPaths[0] stringByAppendingPathComponent:@"Upload"];
48 49 50 51
    if (fileName)
        _fileName = fileName;
    else
        _fileName = url.lastPathComponent;
52 53 54 55
    _filePath = [basePath stringByAppendingPathComponent:_fileName];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if (![fileManager fileExistsAtPath:basePath])
        [fileManager createDirectoryAtPath:basePath withIntermediateDirectories:YES attributes:nil error:nil];
Pierre SAGASPE's avatar
Pierre SAGASPE committed
56 57
    _expectedDownloadSize = _receivedDataSize = 0;
    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
58
    [theRequest addValue:[NSString stringWithFormat:@"Mozilla/5.0 (%@; CPU OS 7_0 like Mac OS X) AppleWebKit/537.51.1 (KHTML, like Gecko) Version/%@ Mobile/11A465 Safari/9537.53 VLC for iOS/%@", UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad ? @"iPad" : @"iPhone", [[UIDevice currentDevice] systemVersion], [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"]] forHTTPHeaderField:@"User-Agent"];
59
    _originalRequest = [theRequest mutableCopy];
Pierre SAGASPE's avatar
Pierre SAGASPE committed
60 61 62 63 64 65
    _urlConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
    if (!_urlConnection) {
        APLog(@"failed to establish connection");
        _downloadInProgress = NO;
    } else {
        _downloadInProgress = YES;
66
        [(VLCAppDelegate*)[UIApplication sharedApplication].delegate networkActivityStarted];
Pierre SAGASPE's avatar
Pierre SAGASPE committed
67 68 69 70
        [(VLCAppDelegate*)[UIApplication sharedApplication].delegate disableIdleTimer];
    }
}

71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
- (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse
{
    if (redirectResponse) {
        NSURL *URL = [request URL];

        NSFileManager *fileManager = [NSFileManager defaultManager];

        if ([fileManager fileExistsAtPath:_filePath])
            [fileManager removeItemAtPath:_filePath error:nil];

        NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
        NSString *basePath = [searchPaths[0] stringByAppendingPathComponent:@"Upload"];
        _fileName = [URL lastPathComponent];
        _filePath = [basePath stringByAppendingPathComponent:_fileName];
        if (![fileManager fileExistsAtPath:basePath])
            [fileManager createDirectoryAtPath:basePath withIntermediateDirectories:YES attributes:nil error:nil];

        NSMutableURLRequest *newRequest = [_originalRequest mutableCopy];
        [newRequest setURL:URL];
        return newRequest;
    } else
        return request;
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSHTTPURLResponse *)response
96
{
97 98
    _statusCode = [response statusCode];
    if (_statusCode == 200) {
99
        if (![[response suggestedFilename] isSupportedFormat]) {
100
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"FILE_NOT_SUPPORTED", nil) message:[NSString stringWithFormat:NSLocalizedString(@"FILE_NOT_SUPPORTED_LONG", nil), [response suggestedFilename]] delegate:self cancelButtonTitle:NSLocalizedString(@"BUTTON_CANCEL", nil) otherButtonTitles:nil];
101 102 103 104 105 106 107 108 109
            [alert show];

            [_urlConnection cancel];
            NSFileManager *fileManager = [NSFileManager defaultManager];
            if ([fileManager fileExistsAtPath:_filePath])
                [fileManager removeItemAtPath:_filePath error:nil];
            [self _downloadEnded];
        } else {
            _expectedDownloadSize = [response expectedContentLength];
110 111 112 113 114 115 116 117
            if (_expectedDownloadSize  < [[UIDevice currentDevice] freeDiskspace].longLongValue)
                [self.delegate downloadStarted];
            else {
                [_urlConnection cancel];
                [self _downloadEnded];
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"DISK_FULL", nil) message:[NSString stringWithFormat:NSLocalizedString(@"DISK_FULL_FORMAT", nil), _fileName, [[UIDevice currentDevice] model]] delegate:self cancelButtonTitle:NSLocalizedString(@"BUTTON_OK", nil) otherButtonTitles:nil];
                [alert show];
            }
118 119
            APLog(@"expected download size: %lu", (unsigned long)_expectedDownloadSize);
        }
120
    } else {
121
        APLog(@"unhandled status code %lu", (unsigned long)_statusCode);
122
        if ([self.delegate respondsToSelector:@selector(downloadFailedWithErrorDescription:)])
123
            [self.delegate downloadFailedWithErrorDescription:[NSString stringWithFormat:NSLocalizedString(@"HTTP_DOWNLOAD_FAILED",nil), _statusCode]];
124 125 126 127 128 129
    }
}

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:_filePath];
130
    if (!fileHandle && _statusCode != 404) {
131 132 133 134 135 136
        // create file
        [[NSFileManager defaultManager] createFileAtPath:_filePath contents:nil attributes:nil];
        fileHandle = [NSFileHandle fileHandleForWritingAtPath:_filePath];

        if (!fileHandle) {
            APLog(@"file creation failed, no data was saved");
137
            if ([self.delegate respondsToSelector:@selector(downloadFailedWithErrorDescription:)])
138
                [self.delegate downloadFailedWithErrorDescription:NSLocalizedString(@"HTTP_FILE_CREATION_FAILED",nil)];
139 140 141 142 143 144 145 146
            return;
        }
    }

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

147 148 149
        _receivedDataSize = _receivedDataSize + [data length];
        if ([self.delegate respondsToSelector:@selector(progressUpdatedTo:receivedDataSize:expectedDownloadSize:)])
            [self.delegate progressUpdatedTo: (float)_receivedDataSize / (float)_expectedDownloadSize receivedDataSize:_receivedDataSize expectedDownloadSize:_expectedDownloadSize];
150 151 152 153 154 155 156 157
    }
    @catch (NSException * e) {
        APLog(@"exception when writing to file %@", _filePath);
    }

    [fileHandle closeFile];
}

158
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
159
{
160
    APLog(@"http file download complete");
161

162
    [self _downloadEnded];
163 164
}

165
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
166
{
167
    APLog(@"http file download failed (%li)", (long)error.code);
168

169 170
    if ([self.delegate respondsToSelector:@selector(downloadFailedWithErrorDescription:)])
        [self.delegate downloadFailedWithErrorDescription:error.description];
171

172
    [self _downloadEnded];
173 174 175 176 177
}

- (void)cancelDownload
{
    [_urlConnection cancel];
178 179 180 181 182 183

    /* remove partially downloaded content */
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:_filePath])
        [fileManager removeItemAtPath:_filePath error:nil];

184
    if ([self.delegate respondsToSelector:@selector(downloadFailedWithErrorDescription:)])
185
        [self.delegate downloadFailedWithErrorDescription:NSLocalizedString(@"HTTP_DOWNLOAD_CANCELLED",nil)];
186

187 188 189 190 191 192
    [self _downloadEnded];
}

- (void)_downloadEnded
{
    _downloadInProgress = NO;
193
    [(VLCAppDelegate*)[UIApplication sharedApplication].delegate networkActivityStopped];
194
    [(VLCAppDelegate*)[UIApplication sharedApplication].delegate activateIdleTimer];
195

196 197 198 199 200 201 202 203 204 205 206 207
    NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *libraryPath = searchPaths[0];

    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *finalFilePath = [libraryPath stringByAppendingPathComponent:_fileName];

    if ([fileManager fileExistsAtPath:_filePath]) {
        [fileManager moveItemAtPath:_filePath toPath:finalFilePath error:nil];
        VLCAppDelegate * appDelegate = [UIApplication sharedApplication].delegate;
        [appDelegate performSelectorOnMainThread:@selector(updateMediaList) withObject:nil waitUntilDone:NO];
    }

208
    [self.delegate downloadEnded];
209 210 211
}

@end