KBDownloadFile.m 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. //
  2. // KBYTDownloadStream.m
  3. // Seas0nPass
  4. //
  5. // Created by Kevin Bradley on 3/9/07.
  6. // Copyright 2007 nito, LLC. All rights reserved.
  7. //
  8. /*
  9. class adapted from hawkeye's KBYTDownloadStream class for downloading youtube files, largely pruned to remove irrelevant sections + updated to cancel the xfer + remodified/updated to use blocks instead of antiquated delegate methods.
  10. */
  11. #import "KBDownloadFile.h"
  12. @implementation KBDownloadFile
  13. @synthesize downloadLocation;
  14. #pragma mark -
  15. #pragma mark •• URL code
  16. - (void)dealloc
  17. {
  18. downloadLocation = nil;
  19. }
  20. - (void)cancel
  21. {
  22. NSError *theError = nil;
  23. [self download:urlDownload didFailWithError:theError];
  24. [urlDownload cancel];
  25. }
  26. - (long long)updateFrequency
  27. {
  28. return updateFrequency;
  29. }
  30. - (void)setUpdateFrequency:(long long)newUpdateFrequency
  31. {
  32. updateFrequency = newUpdateFrequency;
  33. }
  34. - (id)init
  35. {
  36. if(self = [super init]) {
  37. [self setUpdateFrequency:1];
  38. }
  39. return self;
  40. }
  41. //deprecated / obsolete, SHOULD still work but should never be used.
  42. - (void)downloadFileWithURL:(NSURL *)url
  43. toLocation:(NSString *)dlLocation
  44. progress:(DownloadProgressBlock)progressBlock
  45. completed:(DownloadCompletedBlock)completedBlock
  46. {
  47. self.CompletedBlock = completedBlock;
  48. self.ProgressBlock = progressBlock;
  49. self.downloadLocation = dlLocation;
  50. NSURLRequest *theRequest = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
  51. urlDownload = [[NSURLDownload alloc] initWithRequest:theRequest delegate:self];
  52. [urlDownload setDestination:downloadLocation allowOverwrite:YES];
  53. }
  54. - (void)download:(NSURLDownload *)download didFailWithError:(NSError *)error
  55. {
  56. NSLog(@"error: %@", error);
  57. //[handler downloadFailed:downloadLocation];
  58. }
  59. - (void)downloadDidFinish:(NSURLDownload *)download
  60. {
  61. if(download == urlDownload) {
  62. if (self.CompletedBlock != nil)
  63. {
  64. self.CompletedBlock(downloadLocation);
  65. }
  66. }
  67. }
  68. - (void)download:(NSURLDownload *)download didReceiveResponse:(NSURLResponse *)response
  69. {
  70. bytesReceived=0;
  71. [self setDownloadResponse:response];
  72. }
  73. - (void)setDownloadResponse:(NSURLResponse *)response
  74. {
  75. myResponse = response;
  76. }
  77. - (NSURLResponse *)downloadResponse
  78. {
  79. return myResponse;
  80. }
  81. - (void)download:(NSURLDownload *)download didReceiveDataOfLength:(NSUInteger)length
  82. {
  83. long long expectedLength = [[self downloadResponse] expectedContentLength];
  84. bytesReceived=bytesReceived+length;
  85. if (expectedLength != NSURLResponseUnknownLength) {
  86. double percentComplete=(bytesReceived/(float)expectedLength)*100.0;
  87. // NSLog(@"Percent complete - %f",percentComplete);
  88. if((freq%updateFrequency) == 0){
  89. if (self.ProgressBlock != nil)
  90. {
  91. self.ProgressBlock(percentComplete);
  92. }
  93. if (self.FancyProgressBlock != nil)
  94. {
  95. NSString *mediaType = @"media";
  96. NSString *pathExt = downloadLocation.pathExtension;
  97. if ([pathExt isEqualToString:@"m4v"])
  98. {
  99. mediaType = @"video";
  100. } else if ([pathExt isEqualToString:@"aac"])
  101. {
  102. mediaType = @"audio";
  103. }
  104. self.FancyProgressBlock(percentComplete, [NSString stringWithFormat:@"Downloading %@ file...", mediaType]);
  105. }
  106. }
  107. freq++;
  108. } else {
  109. NSLog(@"Bytes received - %f",bytesReceived);
  110. }
  111. }
  112. @end