VLCHTTPFileDownloader.m 4.4 KB

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