VLCHTTPFileDownloader.m 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. //
  2. // VLCHTTPFileDownloader.m
  3. // VLC for iOS
  4. //
  5. // Created by Felix Paul Kühne on 20.05.13.
  6. // Copyright (c) 2013 VideoLAN. All rights reserved.
  7. //
  8. // Refer to the COPYING file of the official project for license.
  9. //
  10. #import "VLCHTTPFileDownloader.h"
  11. #import "VLCAppDelegate.h"
  12. @interface VLCHTTPFileDownloader ()
  13. {
  14. NSString *_filePath;
  15. NSUInteger _expectedDownloadSize;
  16. NSUInteger _receivedDataSize;
  17. NSString *_fileName;
  18. NSURLConnection *_urlConnection;
  19. }
  20. @end
  21. @implementation VLCHTTPFileDownloader
  22. - (NSString *)userReadableDownloadName
  23. {
  24. return _fileName;
  25. }
  26. - (void)downloadFileFromURL:(NSURL *)url
  27. {
  28. NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  29. _fileName = url.lastPathComponent;
  30. _filePath = [searchPaths[0] stringByAppendingPathComponent:_fileName];
  31. _expectedDownloadSize = _receivedDataSize = 0;
  32. NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
  33. _urlConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
  34. if (!_urlConnection) {
  35. APLog(@"failed to establish connection");
  36. _downloadInProgress = NO;
  37. } else {
  38. _downloadInProgress = YES;
  39. [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
  40. [(VLCAppDelegate*)[UIApplication sharedApplication].delegate disableIdleTimer];
  41. }
  42. }
  43. - (void)downloadFileFromURLwithFileName:(NSURL *)url fileNameOfMedia:(NSString*) fileName
  44. {
  45. NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  46. _fileName = fileName;
  47. _filePath = [searchPaths[0] stringByAppendingPathComponent:_fileName];
  48. _expectedDownloadSize = _receivedDataSize = 0;
  49. NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
  50. _urlConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
  51. if (!_urlConnection) {
  52. APLog(@"failed to establish connection");
  53. _downloadInProgress = NO;
  54. } else {
  55. _downloadInProgress = YES;
  56. [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
  57. [(VLCAppDelegate*)[UIApplication sharedApplication].delegate disableIdleTimer];
  58. }
  59. }
  60. -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSHTTPURLResponse *)response
  61. {
  62. NSUInteger statusCode = [response statusCode];
  63. if (statusCode == 200) {
  64. _expectedDownloadSize = [response expectedContentLength];
  65. [self.delegate downloadStarted];
  66. APLog(@"expected download size: %i", _expectedDownloadSize);
  67. } else {
  68. APLog(@"unhandled status code %i", statusCode);
  69. if ([self.delegate respondsToSelector:@selector(downloadFailedWithErrorDescription:)])
  70. [self.delegate downloadFailedWithErrorDescription:[NSString stringWithFormat:@"Download failed with HTTP code %i", statusCode]];
  71. }
  72. }
  73. -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
  74. {
  75. NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:_filePath];
  76. if (!fileHandle) {
  77. // create file
  78. [[NSFileManager defaultManager] createFileAtPath:_filePath contents:nil attributes:nil];
  79. fileHandle = [NSFileHandle fileHandleForWritingAtPath:_filePath];
  80. if (!fileHandle) {
  81. APLog(@"file creation failed, no data was saved");
  82. if ([self.delegate respondsToSelector:@selector(downloadFailedWithErrorDescription:)])
  83. [self.delegate downloadFailedWithErrorDescription:@"File creation failed"];
  84. return;
  85. }
  86. }
  87. @try {
  88. [fileHandle seekToEndOfFile];
  89. [fileHandle writeData:data];
  90. _receivedDataSize = _receivedDataSize + [data length];
  91. if ([self.delegate respondsToSelector:@selector(progressUpdatedTo:receivedDataSize:expectedDownloadSize:)])
  92. [self.delegate progressUpdatedTo: (float)_receivedDataSize / (float)_expectedDownloadSize receivedDataSize:_receivedDataSize expectedDownloadSize:_expectedDownloadSize];
  93. }
  94. @catch (NSException * e) {
  95. APLog(@"exception when writing to file %@", _filePath);
  96. }
  97. [fileHandle closeFile];
  98. }
  99. -(void)connectionDidFinishLoading:(NSURLConnection *)connection {
  100. APLog(@"http file download complete");
  101. VLCAppDelegate * appDelegate = [UIApplication sharedApplication].delegate;
  102. [appDelegate updateMediaList];
  103. [self _downloadEnded];
  104. }
  105. -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
  106. APLog(@"http file download failed (%i)", error.code);
  107. if ([self.delegate respondsToSelector:@selector(downloadFailedWithErrorDescription:)])
  108. [self.delegate downloadFailedWithErrorDescription:error.description];
  109. [self _downloadEnded];
  110. }
  111. - (void)cancelDownload
  112. {
  113. [_urlConnection cancel];
  114. /* remove partially downloaded content */
  115. NSFileManager *fileManager = [NSFileManager defaultManager];
  116. if ([fileManager fileExistsAtPath:_filePath])
  117. [fileManager removeItemAtPath:_filePath error:nil];
  118. if ([self.delegate respondsToSelector:@selector(downloadFailedWithErrorDescription:)])
  119. [self.delegate downloadFailedWithErrorDescription:@"Download canceled by user"];
  120. [self _downloadEnded];
  121. }
  122. - (void)_downloadEnded
  123. {
  124. _downloadInProgress = NO;
  125. [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
  126. [(VLCAppDelegate*)[UIApplication sharedApplication].delegate activateIdleTimer];
  127. [self.delegate downloadEnded];
  128. }
  129. @end