Ben Copsey

Added method for cleaning up temporary file downloads, and tests for this

... ... @@ -223,6 +223,10 @@ typedef enum _ASINetworkErrorType {
// Cancel loading and clean up
- (void)cancelLoad;
// Call to delete the temporary file used during a file download (if it exists)
// No need to call this if the request succeeds - it is removed automatically
- (void)removeTemporaryDownloadFile;
#pragma mark upload/download progress
// Called on main thread to update progress delegates
... ...
... ... @@ -458,6 +458,16 @@ static NSError *ASIUnableToCreateRequestError;
}
- (void)removeTemporaryDownloadFile
{
//Remove the temporary file
NSError *removeError = nil;
[[NSFileManager defaultManager] removeItemAtPath:temporaryFileDownloadPath error:&removeError];
if (removeError) {
[self failWithError:[NSError errorWithDomain:NetworkRequestErrorDomain code:ASIFileManagementError userInfo:[NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"Failed to delete file at %@ with error: %@",temporaryFileDownloadPath,removeError],NSLocalizedDescriptionKey,removeError,NSUnderlyingErrorKey,nil]]];
}
}
#pragma mark upload/download progress
... ... @@ -1151,12 +1161,7 @@ static NSError *ASIUnableToCreateRequestError;
fileError = [NSError errorWithDomain:NetworkRequestErrorDomain code:ASIFileManagementError userInfo:[NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"Decompression of %@ failed with code %hi",temporaryFileDownloadPath,decompressionStatus],NSLocalizedDescriptionKey,nil]];
}
//Remove the temporary file
NSError *removeError = nil;
[[NSFileManager defaultManager] removeItemAtPath:temporaryFileDownloadPath error:&removeError];
if (removeError) {
fileError = [NSError errorWithDomain:NetworkRequestErrorDomain code:ASIFileManagementError userInfo:[NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"Failed to delete file at %@ with error: %@",temporaryFileDownloadPath,removeError],NSLocalizedDescriptionKey,removeError,NSUnderlyingErrorKey,nil]];
}
[self removeTemporaryDownloadFile];
} else {
//Remove any file at the destination path
... ...
... ... @@ -534,5 +534,4 @@
GHAssertTrue(success,@"Failed to correctly display increment progress for a partial download");
}
@end
... ...
... ... @@ -193,7 +193,6 @@
}
- (void)requestFailedCancellingOthers:(ASIHTTPRequest *)request
{
complete = YES;
... ... @@ -374,7 +373,40 @@
[networkQueue release];
//Test the temporary file cleanup
complete = NO;
progress = 0;
networkQueue = [[ASINetworkQueue alloc] init];
[networkQueue setDownloadProgressDelegate:self];
[networkQueue setShowAccurateProgress:YES];
[networkQueue setDelegate:self];
[networkQueue setQueueDidFinishSelector:@selector(queueFinished:)];
request = [[[ASIHTTPRequest alloc] initWithURL:downloadURL] autorelease];
[request setDownloadDestinationPath:downloadPath];
[request setTemporaryFileDownloadPath:temporaryPath];
[request setAllowResumeForFileDownloads:YES];
[networkQueue addOperation:request];
[networkQueue go];
// Let the download run for 5 seconds
timeoutTimer = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(stopQueue:) userInfo:nil repeats:NO];
while (!complete) {
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:1]];
}
[networkQueue cancelAllOperations];
success = ([[NSFileManager defaultManager] fileExistsAtPath:temporaryPath]);
GHAssertTrue(success,@"Temporary download file doesn't exist");
[request removeTemporaryDownloadFile];
success = (![[NSFileManager defaultManager] fileExistsAtPath:temporaryPath]);
GHAssertTrue(success,@"Temporary download file should have been deleted");
timeoutTimer = nil;
[networkQueue release];
}
... ...