123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- //
- // KBYTDownloadStream.m
- // Seas0nPass
- //
- // Created by Kevin Bradley on 3/9/07.
- // Copyright 2007 nito, LLC. All rights reserved.
- //
- /*
-
- 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.
-
- */
- #import "KBDownloadFile.h"
- @implementation KBDownloadFile
- @synthesize downloadLocation;
- #pragma mark -
- #pragma mark •• URL code
- - (void)dealloc
- {
- downloadLocation = nil;
- }
- - (void)cancel
- {
- NSError *theError = nil;
- [self download:urlDownload didFailWithError:theError];
- [urlDownload cancel];
- }
- - (long long)updateFrequency
- {
- return updateFrequency;
- }
- - (void)setUpdateFrequency:(long long)newUpdateFrequency
- {
- updateFrequency = newUpdateFrequency;
- }
- - (id)init
- {
- if(self = [super init]) {
- [self setUpdateFrequency:1];
-
- }
-
- return self;
- }
- //deprecated / obsolete, SHOULD still work but should never be used.
- - (void)downloadFileWithURL:(NSURL *)url
- toLocation:(NSString *)dlLocation
- progress:(DownloadProgressBlock)progressBlock
- completed:(DownloadCompletedBlock)completedBlock
- {
- self.CompletedBlock = completedBlock;
- self.ProgressBlock = progressBlock;
- self.downloadLocation = dlLocation;
- NSURLRequest *theRequest = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
- urlDownload = [[NSURLDownload alloc] initWithRequest:theRequest delegate:self];
- [urlDownload setDestination:downloadLocation allowOverwrite:YES];
-
- }
- - (void)download:(NSURLDownload *)download didFailWithError:(NSError *)error
- {
- NSLog(@"error: %@", error);
- //[handler downloadFailed:downloadLocation];
- }
- - (void)downloadDidFinish:(NSURLDownload *)download
- {
- if(download == urlDownload) {
-
-
- if (self.CompletedBlock != nil)
- {
- self.CompletedBlock(downloadLocation);
- }
- }
- }
- - (void)download:(NSURLDownload *)download didReceiveResponse:(NSURLResponse *)response
- {
- bytesReceived=0;
- [self setDownloadResponse:response];
- }
- - (void)setDownloadResponse:(NSURLResponse *)response
- {
- myResponse = response;
- }
- - (NSURLResponse *)downloadResponse
- {
- return myResponse;
- }
- - (void)download:(NSURLDownload *)download didReceiveDataOfLength:(NSUInteger)length
- {
- long long expectedLength = [[self downloadResponse] expectedContentLength];
- bytesReceived=bytesReceived+length;
-
- if (expectedLength != NSURLResponseUnknownLength) {
-
- double percentComplete=(bytesReceived/(float)expectedLength)*100.0;
- // NSLog(@"Percent complete - %f",percentComplete);
-
- if((freq%updateFrequency) == 0){
-
- if (self.ProgressBlock != nil)
- {
- self.ProgressBlock(percentComplete);
- }
-
- if (self.FancyProgressBlock != nil)
- {
- NSString *mediaType = @"media";
- NSString *pathExt = downloadLocation.pathExtension;
- if ([pathExt isEqualToString:@"m4v"])
- {
- mediaType = @"video";
- } else if ([pathExt isEqualToString:@"aac"])
- {
- mediaType = @"audio";
- }
- self.FancyProgressBlock(percentComplete, [NSString stringWithFormat:@"Downloading %@ file...", mediaType]);
- }
- }
- freq++;
-
- } else {
-
- NSLog(@"Bytes received - %f",bytesReceived);
-
- }
-
- }
- @end
|