KBDownloadFile.m 3.6 KB

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