VLCHTTPFileDownloader.m 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*****************************************************************************
  2. * VLCHTTPFileDownloader.m
  3. * VLC for iOS
  4. *****************************************************************************
  5. * Copyright (c) 2013 VideoLAN. All rights reserved.
  6. * $Id$
  7. *
  8. * Authors: Felix Paul Kühne <fkuehne # videolan.org>
  9. * Pierre Sagaspe <pierre.sagaspe # me.com>
  10. *
  11. * Refer to the COPYING file of the official project for license.
  12. *****************************************************************************/
  13. #import "VLCHTTPFileDownloader.h"
  14. #import "NSString+SupportedMedia.h"
  15. #import "VLCAppDelegate.h"
  16. #import "UIDevice+VLC.h"
  17. @interface VLCHTTPFileDownloader ()
  18. {
  19. NSString *_filePath;
  20. NSUInteger _expectedDownloadSize;
  21. NSUInteger _receivedDataSize;
  22. NSString *_fileName;
  23. NSURLConnection *_urlConnection;
  24. NSMutableURLRequest *_originalRequest;
  25. NSUInteger _statusCode;
  26. }
  27. @end
  28. @implementation VLCHTTPFileDownloader
  29. - (NSString *)userReadableDownloadName
  30. {
  31. return _fileName;
  32. }
  33. - (void)downloadFileFromURL:(NSURL *)url
  34. {
  35. [self downloadFileFromURL:url withFileName:nil];
  36. }
  37. - (void)downloadFileFromURL:(NSURL *)url withFileName:(NSString*)fileName
  38. {
  39. NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  40. NSString *basePath = [searchPaths[0] stringByAppendingPathComponent:@"Upload"];
  41. if (fileName)
  42. _fileName = fileName;
  43. else
  44. _fileName = url.lastPathComponent;
  45. _filePath = [basePath stringByAppendingPathComponent:_fileName];
  46. NSFileManager *fileManager = [NSFileManager defaultManager];
  47. if (![fileManager fileExistsAtPath:basePath])
  48. [fileManager createDirectoryAtPath:basePath withIntermediateDirectories:YES attributes:nil error:nil];
  49. _expectedDownloadSize = _receivedDataSize = 0;
  50. NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
  51. [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"];
  52. _originalRequest = [theRequest mutableCopy];
  53. _urlConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
  54. if (!_urlConnection) {
  55. APLog(@"failed to establish connection");
  56. _downloadInProgress = NO;
  57. } else {
  58. _downloadInProgress = YES;
  59. [(VLCAppDelegate*)[UIApplication sharedApplication].delegate networkActivityStarted];
  60. [(VLCAppDelegate*)[UIApplication sharedApplication].delegate disableIdleTimer];
  61. }
  62. }
  63. - (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse
  64. {
  65. if (redirectResponse) {
  66. NSURL *URL = [request URL];
  67. NSFileManager *fileManager = [NSFileManager defaultManager];
  68. if ([fileManager fileExistsAtPath:_filePath])
  69. [fileManager removeItemAtPath:_filePath error:nil];
  70. NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  71. NSString *basePath = [searchPaths[0] stringByAppendingPathComponent:@"Upload"];
  72. _fileName = [URL lastPathComponent];
  73. _filePath = [basePath stringByAppendingPathComponent:_fileName];
  74. if (![fileManager fileExistsAtPath:basePath])
  75. [fileManager createDirectoryAtPath:basePath withIntermediateDirectories:YES attributes:nil error:nil];
  76. NSMutableURLRequest *newRequest = [_originalRequest mutableCopy];
  77. [newRequest setURL:URL];
  78. return newRequest;
  79. } else
  80. return request;
  81. }
  82. - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSHTTPURLResponse *)response
  83. {
  84. _statusCode = [response statusCode];
  85. if (_statusCode == 200) {
  86. if (![[response suggestedFilename] isSupportedFormat]) {
  87. 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];
  88. [alert show];
  89. [_urlConnection cancel];
  90. NSFileManager *fileManager = [NSFileManager defaultManager];
  91. if ([fileManager fileExistsAtPath:_filePath])
  92. [fileManager removeItemAtPath:_filePath error:nil];
  93. [self _downloadEnded];
  94. } else {
  95. _expectedDownloadSize = [response expectedContentLength];
  96. if (_expectedDownloadSize < [[UIDevice currentDevice] freeDiskspace].longLongValue)
  97. [self.delegate downloadStarted];
  98. else {
  99. [_urlConnection cancel];
  100. [self _downloadEnded];
  101. 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];
  102. [alert show];
  103. }
  104. APLog(@"expected download size: %lu", (unsigned long)_expectedDownloadSize);
  105. }
  106. } else {
  107. APLog(@"unhandled status code %lu", (unsigned long)_statusCode);
  108. if ([self.delegate respondsToSelector:@selector(downloadFailedWithErrorDescription:)])
  109. [self.delegate downloadFailedWithErrorDescription:[NSString stringWithFormat:NSLocalizedString(@"HTTP_DOWNLOAD_FAILED",nil), _statusCode]];
  110. }
  111. }
  112. -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
  113. {
  114. NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:_filePath];
  115. if (!fileHandle && _statusCode != 404) {
  116. // create file
  117. [[NSFileManager defaultManager] createFileAtPath:_filePath contents:nil attributes:nil];
  118. fileHandle = [NSFileHandle fileHandleForWritingAtPath:_filePath];
  119. if (!fileHandle) {
  120. APLog(@"file creation failed, no data was saved");
  121. if ([self.delegate respondsToSelector:@selector(downloadFailedWithErrorDescription:)])
  122. [self.delegate downloadFailedWithErrorDescription:NSLocalizedString(@"HTTP_FILE_CREATION_FAILED",nil)];
  123. return;
  124. }
  125. }
  126. @try {
  127. [fileHandle seekToEndOfFile];
  128. [fileHandle writeData:data];
  129. _receivedDataSize = _receivedDataSize + [data length];
  130. if ([self.delegate respondsToSelector:@selector(progressUpdatedTo:receivedDataSize:expectedDownloadSize:)])
  131. [self.delegate progressUpdatedTo: (float)_receivedDataSize / (float)_expectedDownloadSize receivedDataSize:_receivedDataSize expectedDownloadSize:_expectedDownloadSize];
  132. }
  133. @catch (NSException * e) {
  134. APLog(@"exception when writing to file %@", _filePath);
  135. }
  136. [fileHandle closeFile];
  137. }
  138. - (void)connectionDidFinishLoading:(NSURLConnection *)connection
  139. {
  140. APLog(@"http file download complete");
  141. [self _downloadEnded];
  142. }
  143. - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
  144. {
  145. APLog(@"http file download failed (%li)", (long)error.code);
  146. if ([self.delegate respondsToSelector:@selector(downloadFailedWithErrorDescription:)])
  147. [self.delegate downloadFailedWithErrorDescription:error.description];
  148. [self _downloadEnded];
  149. }
  150. - (void)cancelDownload
  151. {
  152. [_urlConnection cancel];
  153. /* remove partially downloaded content */
  154. NSFileManager *fileManager = [NSFileManager defaultManager];
  155. if ([fileManager fileExistsAtPath:_filePath])
  156. [fileManager removeItemAtPath:_filePath error:nil];
  157. if ([self.delegate respondsToSelector:@selector(downloadFailedWithErrorDescription:)])
  158. [self.delegate downloadFailedWithErrorDescription:NSLocalizedString(@"HTTP_DOWNLOAD_CANCELLED",nil)];
  159. [self _downloadEnded];
  160. }
  161. - (void)_downloadEnded
  162. {
  163. _downloadInProgress = NO;
  164. [(VLCAppDelegate*)[UIApplication sharedApplication].delegate networkActivityStopped];
  165. [(VLCAppDelegate*)[UIApplication sharedApplication].delegate activateIdleTimer];
  166. NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  167. NSString *libraryPath = searchPaths[0];
  168. NSFileManager *fileManager = [NSFileManager defaultManager];
  169. NSString *finalFilePath = [libraryPath stringByAppendingPathComponent:_fileName];
  170. if ([fileManager fileExistsAtPath:_filePath]) {
  171. [fileManager moveItemAtPath:_filePath toPath:finalFilePath error:nil];
  172. VLCAppDelegate * appDelegate = [UIApplication sharedApplication].delegate;
  173. [appDelegate performSelectorOnMainThread:@selector(updateMediaList) withObject:nil waitUntilDone:NO];
  174. }
  175. [self.delegate downloadEnded];
  176. }
  177. @end