VLCHTTPFileDownloader.m 8.6 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 "VLCActivityManager.h"
17
#import "UIDevice+VLC.h"
18
#import "VLCMediaFileDiscoverer.h"
19 20 21 22

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

@end

@implementation VLCHTTPFileDownloader

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

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

45
- (void)downloadFileFromURL:(NSURL *)url withFileName:(NSString*)fileName
Pierre SAGASPE's avatar
Pierre SAGASPE committed
46
{
47
    NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
48
    NSString *basePath = [searchPaths[0] stringByAppendingPathComponent:@"Upload"];
49 50 51
    if (fileName)
        _fileName = fileName;
    else
52
        _fileName = [url.lastPathComponent stringByRemovingPercentEncoding];
53 54 55 56 57

    if (_fileName.pathExtension.length == 0 || ![_fileName isSupportedFormat]) {
        _fileName = [_fileName stringByAppendingPathExtension:@"vlc"];
    }

58 59 60 61
    _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
62 63
    _expectedDownloadSize = _receivedDataSize = 0;
    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
64
    [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"];
65
    _originalRequest = [theRequest mutableCopy];
Pierre SAGASPE's avatar
Pierre SAGASPE committed
66 67 68 69 70 71
    _urlConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
    if (!_urlConnection) {
        APLog(@"failed to establish connection");
        _downloadInProgress = NO;
    } else {
        _downloadInProgress = YES;
72 73 74
        VLCActivityManager *activityManager = [VLCActivityManager defaultManager];
        [activityManager networkActivityStarted];
        [activityManager disableIdleTimer];
Pierre SAGASPE's avatar
Pierre SAGASPE committed
75 76 77
    }
}

78 79 80 81 82 83 84 85 86 87 88 89
- (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"];
90
        _fileName = [[URL lastPathComponent] stringByRemovingPercentEncoding];
91 92 93 94 95 96 97 98 99 100 101 102
        _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
103
{
104 105
    _statusCode = [response statusCode];
    if (_statusCode == 200) {
106 107 108 109 110 111 112 113 114
        _expectedDownloadSize = [response expectedContentLength];
        APLog(@"expected download size: %lli", _expectedDownloadSize);
        if (_expectedDownloadSize  < [[UIDevice currentDevice] freeDiskspace].longLongValue)
            [self.delegate downloadStarted];
        else {
            [_urlConnection cancel];
            [self _downloadEnded];
            VLCAlertView *alert = [[VLCAlertView alloc] initWithTitle:NSLocalizedString(@"DISK_FULL", nil)
                                                             message:[NSString stringWithFormat:NSLocalizedString(@"DISK_FULL_FORMAT", nil), _fileName, [[UIDevice currentDevice] model]]
115
                                                             delegate:self
116
                                                    cancelButtonTitle:NSLocalizedString(@"BUTTON_OK", nil)
117
                                                    otherButtonTitles:nil];
118 119
            [alert show];
        }
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 194 195
    VLCActivityManager *activityManager = [VLCActivityManager defaultManager];
    [activityManager networkActivityStopped];
    [activityManager activateIdleTimer];
196

197 198 199 200 201 202 203 204
    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];
205
        [[VLCMediaFileDiscoverer sharedInstance] performSelectorOnMainThread:@selector(updateMediaList) withObject:nil waitUntilDone:NO];
206 207
    }

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

@end