VLCHTTPFileDownloader.m 3.3 KB

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