VLCHTTPFileDownloader.m 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. }
  37. }
  38. -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSHTTPURLResponse *)response
  39. {
  40. NSUInteger statusCode = [response statusCode];
  41. if (statusCode == 200) {
  42. _expectedDownloadSize = [response expectedContentLength];
  43. APLog(@"expected download size: %i", _expectedDownloadSize);
  44. }
  45. }
  46. -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
  47. {
  48. NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:_filePath];
  49. if (!fileHandle) {
  50. // create file
  51. [[NSFileManager defaultManager] createFileAtPath:_filePath contents:nil attributes:nil];
  52. fileHandle = [NSFileHandle fileHandleForWritingAtPath:_filePath];
  53. if (!fileHandle) {
  54. APLog(@"file creation failed, no data was saved");
  55. return;
  56. }
  57. }
  58. @try {
  59. [fileHandle seekToEndOfFile];
  60. [fileHandle writeData:data];
  61. _receivedDataSize = _receivedDataSize + data.length;
  62. _progressIndicator.progress = (float)_receivedDataSize / (float)_expectedDownloadSize;
  63. }
  64. @catch (NSException * e) {
  65. APLog(@"exception when writing to file %@", _filePath);
  66. }
  67. [fileHandle closeFile];
  68. }
  69. -(void)connectionDidFinishLoading:(NSURLConnection *)connection {
  70. APLog(@"http file download complete");
  71. _downloadInProgress = NO;
  72. _progressIndicator.hidden = YES;
  73. VLCAppDelegate * appDelegate = [UIApplication sharedApplication].delegate;
  74. [appDelegate updateMediaList];
  75. [_mediaViewController dismiss:nil];
  76. }
  77. -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
  78. APLog(@"http file download failed (%i)", error.code);
  79. _downloadInProgress = NO;
  80. _progressIndicator.hidden = YES;
  81. [_mediaViewController dismiss:nil];
  82. }
  83. @end