VLCHTTPFileDownloader.m 5.8 KB

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