VLCHTTPFileDownloader.m 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. VLCCircularProgressIndicator *_progressIndicator;
  17. NSString *_filePath;
  18. NSUInteger _expectedDownloadSize;
  19. NSUInteger _receivedDataSize;
  20. }
  21. @end
  22. @implementation VLCHTTPFileDownloader
  23. - (void)downloadFileFromURL:(NSURL *)url
  24. {
  25. _progressIndicator = self.mediaViewController.httpDownloadProgressIndicator;
  26. _progressIndicator.progress = 0.;
  27. NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  28. _filePath = [searchPaths[0] stringByAppendingPathComponent:url.lastPathComponent];
  29. _expectedDownloadSize = _receivedDataSize = 0;
  30. NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
  31. NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
  32. if (!theConnection) {
  33. APLog(@"failed to establish connection");
  34. _downloadInProgress = NO;
  35. } else {
  36. _downloadInProgress = YES;
  37. _progressIndicator.hidden = NO;
  38. [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
  39. }
  40. }
  41. -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSHTTPURLResponse *)response
  42. {
  43. NSUInteger statusCode = [response statusCode];
  44. if (statusCode == 200) {
  45. _expectedDownloadSize = [response expectedContentLength];
  46. APLog(@"expected download size: %i", _expectedDownloadSize);
  47. }
  48. }
  49. -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
  50. {
  51. NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:_filePath];
  52. if (!fileHandle) {
  53. // create file
  54. [[NSFileManager defaultManager] createFileAtPath:_filePath contents:nil attributes:nil];
  55. fileHandle = [NSFileHandle fileHandleForWritingAtPath:_filePath];
  56. if (!fileHandle) {
  57. APLog(@"file creation failed, no data was saved");
  58. return;
  59. }
  60. }
  61. @try {
  62. [fileHandle seekToEndOfFile];
  63. [fileHandle writeData:data];
  64. _receivedDataSize = _receivedDataSize + data.length;
  65. _progressIndicator.progress = (float)_receivedDataSize / (float)_expectedDownloadSize;
  66. }
  67. @catch (NSException * e) {
  68. APLog(@"exception when writing to file %@", _filePath);
  69. }
  70. [fileHandle closeFile];
  71. }
  72. -(void)connectionDidFinishLoading:(NSURLConnection *)connection {
  73. APLog(@"http file download complete");
  74. _downloadInProgress = NO;
  75. _progressIndicator.hidden = YES;
  76. [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
  77. VLCAppDelegate * appDelegate = [UIApplication sharedApplication].delegate;
  78. [appDelegate updateMediaList];
  79. [_mediaViewController dismiss:nil];
  80. }
  81. -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
  82. APLog(@"http file download failed (%i)", error.code);
  83. _downloadInProgress = NO;
  84. _progressIndicator.hidden = YES;
  85. [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
  86. [_mediaViewController dismiss:nil];
  87. }
  88. @end