VLCHTTPFileDownloader.m 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 "VLCAppDelegate.h"
  15. @interface VLCHTTPFileDownloader ()
  16. {
  17. NSString *_filePath;
  18. NSUInteger _expectedDownloadSize;
  19. NSUInteger _receivedDataSize;
  20. NSString *_fileName;
  21. NSURLConnection *_urlConnection;
  22. NSUInteger _statusCode;
  23. }
  24. @end
  25. @implementation VLCHTTPFileDownloader
  26. - (NSString *)userReadableDownloadName
  27. {
  28. return _fileName;
  29. }
  30. - (void)downloadFileFromURL:(NSURL *)url
  31. {
  32. NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  33. _fileName = url.lastPathComponent;
  34. _filePath = [searchPaths[0] stringByAppendingPathComponent:_fileName];
  35. _expectedDownloadSize = _receivedDataSize = 0;
  36. NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
  37. _urlConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
  38. if (!_urlConnection) {
  39. APLog(@"failed to establish connection");
  40. _downloadInProgress = NO;
  41. } else {
  42. _downloadInProgress = YES;
  43. [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
  44. [(VLCAppDelegate*)[UIApplication sharedApplication].delegate disableIdleTimer];
  45. }
  46. }
  47. - (void)downloadFileFromURLwithFileName:(NSURL *)url fileNameOfMedia:(NSString*) fileName
  48. {
  49. NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  50. _fileName = fileName;
  51. _filePath = [searchPaths[0] stringByAppendingPathComponent:_fileName];
  52. _expectedDownloadSize = _receivedDataSize = 0;
  53. NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
  54. _urlConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
  55. if (!_urlConnection) {
  56. APLog(@"failed to establish connection");
  57. _downloadInProgress = NO;
  58. } else {
  59. _downloadInProgress = YES;
  60. [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
  61. [(VLCAppDelegate*)[UIApplication sharedApplication].delegate disableIdleTimer];
  62. }
  63. }
  64. -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSHTTPURLResponse *)response
  65. {
  66. _statusCode = [response statusCode];
  67. if (_statusCode == 200) {
  68. _expectedDownloadSize = [response expectedContentLength];
  69. [self.delegate downloadStarted];
  70. APLog(@"expected download size: %i", _expectedDownloadSize);
  71. } else {
  72. APLog(@"unhandled status code %i", _statusCode);
  73. if ([self.delegate respondsToSelector:@selector(downloadFailedWithErrorDescription:)])
  74. [self.delegate downloadFailedWithErrorDescription:[NSString stringWithFormat:NSLocalizedString(@"HTTP_DOWNLOAD_FAILED",nil), _statusCode]];
  75. }
  76. }
  77. -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
  78. {
  79. NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:_filePath];
  80. if (!fileHandle && _statusCode != 404) {
  81. // create file
  82. [[NSFileManager defaultManager] createFileAtPath:_filePath contents:nil attributes:nil];
  83. fileHandle = [NSFileHandle fileHandleForWritingAtPath:_filePath];
  84. if (!fileHandle) {
  85. APLog(@"file creation failed, no data was saved");
  86. if ([self.delegate respondsToSelector:@selector(downloadFailedWithErrorDescription:)])
  87. [self.delegate downloadFailedWithErrorDescription:NSLocalizedString(@"HTTP_FILE_CREATION_FAILED",nil)];
  88. return;
  89. }
  90. }
  91. @try {
  92. [fileHandle seekToEndOfFile];
  93. [fileHandle writeData:data];
  94. _receivedDataSize = _receivedDataSize + [data length];
  95. if ([self.delegate respondsToSelector:@selector(progressUpdatedTo:receivedDataSize:expectedDownloadSize:)])
  96. [self.delegate progressUpdatedTo: (float)_receivedDataSize / (float)_expectedDownloadSize receivedDataSize:_receivedDataSize expectedDownloadSize:_expectedDownloadSize];
  97. }
  98. @catch (NSException * e) {
  99. APLog(@"exception when writing to file %@", _filePath);
  100. }
  101. [fileHandle closeFile];
  102. }
  103. -(void)connectionDidFinishLoading:(NSURLConnection *)connection {
  104. APLog(@"http file download complete");
  105. VLCAppDelegate * appDelegate = [UIApplication sharedApplication].delegate;
  106. [appDelegate performSelectorOnMainThread:@selector(updateMediaList) withObject:nil waitUntilDone:NO];
  107. [self _downloadEnded];
  108. }
  109. -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
  110. APLog(@"http file download failed (%i)", error.code);
  111. if ([self.delegate respondsToSelector:@selector(downloadFailedWithErrorDescription:)])
  112. [self.delegate downloadFailedWithErrorDescription:error.description];
  113. [self _downloadEnded];
  114. }
  115. - (void)cancelDownload
  116. {
  117. [_urlConnection cancel];
  118. /* remove partially downloaded content */
  119. NSFileManager *fileManager = [NSFileManager defaultManager];
  120. if ([fileManager fileExistsAtPath:_filePath])
  121. [fileManager removeItemAtPath:_filePath error:nil];
  122. if ([self.delegate respondsToSelector:@selector(downloadFailedWithErrorDescription:)])
  123. [self.delegate downloadFailedWithErrorDescription:NSLocalizedString(@"HTTP_DOWNLOAD_CANCELLED",nil)];
  124. [self _downloadEnded];
  125. }
  126. - (void)_downloadEnded
  127. {
  128. _downloadInProgress = NO;
  129. [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
  130. [(VLCAppDelegate*)[UIApplication sharedApplication].delegate activateIdleTimer];
  131. [self.delegate downloadEnded];
  132. }
  133. @end