VLCHTTPFileDownloader.m 9.28 KB
Newer Older
1 2 3 4
/*****************************************************************************
 * VLCHTTPFileDownloader.m
 * VLC for iOS
 *****************************************************************************
5
 * Copyright (c) 2013-2018 VideoLAN. All rights reserved.
6 7 8 9 10 11 12
 * $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
#import "VLC-Swift.h"
20

21
@interface VLCHTTPFileDownloader () <NSURLSessionDelegate>
22 23
{
    NSString *_filePath;
24
    long long _expectedDownloadSize;
25
    NSUInteger _receivedDataSize;
26
    NSString *_fileName;
27
    NSURLSessionTask *_sessionTask;
28
    NSMutableURLRequest *_originalRequest;
29
    NSUInteger _statusCode;
30 31 32 33 34 35
}

@end

@implementation VLCHTTPFileDownloader

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

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

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

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

59 60 61 62
    _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
63 64
    _expectedDownloadSize = _receivedDataSize = 0;
    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
65
    [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"];
66
    _originalRequest = [theRequest mutableCopy];
67 68 69 70 71

    NSURLSession *urlSession = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:[NSOperationQueue mainQueue]];
    _sessionTask = [urlSession dataTaskWithRequest:theRequest];
    [_sessionTask resume];
    if (!_sessionTask) {
Pierre SAGASPE's avatar
Pierre SAGASPE committed
72 73 74 75
        APLog(@"failed to establish connection");
        _downloadInProgress = NO;
    } else {
        _downloadInProgress = YES;
76 77 78
        VLCActivityManager *activityManager = [VLCActivityManager defaultManager];
        [activityManager networkActivityStarted];
        [activityManager disableIdleTimer];
Pierre SAGASPE's avatar
Pierre SAGASPE committed
79 80 81
    }
}

82 83 84 85 86 87 88 89 90 91 92 93
- (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"];
94
        _fileName = [[URL lastPathComponent] stringByRemovingPercentEncoding];
95 96 97 98 99 100 101 102 103 104 105
        _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;
}

106
- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler
107
{
108 109 110
    completionHandler(NSURLSessionResponseAllow);
    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
    _statusCode = [httpResponse statusCode];
111
    if (_statusCode == 200) {
112 113
        _expectedDownloadSize = [response expectedContentLength];
        APLog(@"expected download size: %lli", _expectedDownloadSize);
114
        if (_expectedDownloadSize  > [[UIDevice currentDevice] VLCFreeDiskSpace].longLongValue) { //handle too big a download
115 116
            [VLCAlertViewController alertViewManagerWithTitle:NSLocalizedString(@"DISK_FULL", nil)
                                                 errorMessage:[NSString stringWithFormat:NSLocalizedString(@"DISK_FULL_FORMAT", nil), _fileName, [[UIDevice currentDevice] model]]
117
                                               viewController:self.delegate];
118
            [_sessionTask cancel];
119 120
            [self _downloadEnded];
            return;
121
        }
122
        [self.delegate downloadStarted];
123
    } else {
124
        APLog(@"unhandled status code %lu", (unsigned long)_statusCode);
125
        if ([self.delegate respondsToSelector:@selector(downloadFailedWithErrorDescription:)])
126
            [self.delegate downloadFailedWithErrorDescription:[NSString stringWithFormat:NSLocalizedString(@"HTTP_DOWNLOAD_FAILED",nil), _statusCode]];
127 128 129
    }
}

130
- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data
131 132
{
    NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:_filePath];
133
    if (!fileHandle && _statusCode != 404) {
134 135 136 137 138 139
        // create file
        [[NSFileManager defaultManager] createFileAtPath:_filePath contents:nil attributes:nil];
        fileHandle = [NSFileHandle fileHandleForWritingAtPath:_filePath];

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

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

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

    [fileHandle closeFile];
}

161
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error
162
{
163 164 165 166 167 168 169 170 171 172 173 174
    if (error.code != -999) {
        if (error) {
            APLog(@"http file download failed (%li)", (long)error.code);
            if ([self.delegate respondsToSelector:@selector(downloadFailedWithErrorDescription:)])
                [self.delegate downloadFailedWithErrorDescription:error.description];
        } else {
            APLog(@"http file download complete");
        }
        [self _downloadEnded];
    } else {
        APLog(@"http file download canceled");
    }
175 176 177 178
}

- (void)cancelDownload
{
179
    [_sessionTask cancel];
180 181 182 183 184
    /* remove partially downloaded content */
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:_filePath])
        [fileManager removeItemAtPath:_filePath error:nil];

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

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

- (void)_downloadEnded
{
    _downloadInProgress = NO;
194 195 196
    VLCActivityManager *activityManager = [VLCActivityManager defaultManager];
    [activityManager networkActivityStopped];
    [activityManager activateIdleTimer];
197

198 199 200 201 202 203 204 205
    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];
206
        [[VLCMediaFileDiscoverer sharedInstance] performSelectorOnMainThread:@selector(updateMediaList) withObject:nil waitUntilDone:NO];
207
#if TARGET_OS_IOS
208 209 210
        // FIXME: Replace notifications by cleaner observers
        [[NSNotificationCenter defaultCenter] postNotificationName:NSNotification.VLCNewFileAddedNotification
                                                            object:self];
211
#endif
212 213
    }

214
    [self.delegate downloadEnded];
215 216 217
}

@end