VLCHTTPFileDownloader.m 4.4 KB

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