Added method for cleaning up temporary file downloads, and tests for this
Showing
4 changed files
with
49 additions
and
9 deletions
| @@ -223,6 +223,10 @@ typedef enum _ASINetworkErrorType { | @@ -223,6 +223,10 @@ typedef enum _ASINetworkErrorType { | ||
| 223 | // Cancel loading and clean up | 223 | // Cancel loading and clean up |
| 224 | - (void)cancelLoad; | 224 | - (void)cancelLoad; |
| 225 | 225 | ||
| 226 | +// Call to delete the temporary file used during a file download (if it exists) | ||
| 227 | +// No need to call this if the request succeeds - it is removed automatically | ||
| 228 | +- (void)removeTemporaryDownloadFile; | ||
| 229 | + | ||
| 226 | #pragma mark upload/download progress | 230 | #pragma mark upload/download progress |
| 227 | 231 | ||
| 228 | // Called on main thread to update progress delegates | 232 | // Called on main thread to update progress delegates |
| @@ -458,6 +458,16 @@ static NSError *ASIUnableToCreateRequestError; | @@ -458,6 +458,16 @@ static NSError *ASIUnableToCreateRequestError; | ||
| 458 | } | 458 | } |
| 459 | 459 | ||
| 460 | 460 | ||
| 461 | +- (void)removeTemporaryDownloadFile | ||
| 462 | +{ | ||
| 463 | + //Remove the temporary file | ||
| 464 | + NSError *removeError = nil; | ||
| 465 | + [[NSFileManager defaultManager] removeItemAtPath:temporaryFileDownloadPath error:&removeError]; | ||
| 466 | + if (removeError) { | ||
| 467 | + [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]]]; | ||
| 468 | + } | ||
| 469 | +} | ||
| 470 | + | ||
| 461 | 471 | ||
| 462 | #pragma mark upload/download progress | 472 | #pragma mark upload/download progress |
| 463 | 473 | ||
| @@ -1151,12 +1161,7 @@ static NSError *ASIUnableToCreateRequestError; | @@ -1151,12 +1161,7 @@ static NSError *ASIUnableToCreateRequestError; | ||
| 1151 | fileError = [NSError errorWithDomain:NetworkRequestErrorDomain code:ASIFileManagementError userInfo:[NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"Decompression of %@ failed with code %hi",temporaryFileDownloadPath,decompressionStatus],NSLocalizedDescriptionKey,nil]]; | 1161 | fileError = [NSError errorWithDomain:NetworkRequestErrorDomain code:ASIFileManagementError userInfo:[NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"Decompression of %@ failed with code %hi",temporaryFileDownloadPath,decompressionStatus],NSLocalizedDescriptionKey,nil]]; |
| 1152 | } | 1162 | } |
| 1153 | 1163 | ||
| 1154 | - //Remove the temporary file | 1164 | + [self removeTemporaryDownloadFile]; |
| 1155 | - NSError *removeError = nil; | ||
| 1156 | - [[NSFileManager defaultManager] removeItemAtPath:temporaryFileDownloadPath error:&removeError]; | ||
| 1157 | - if (removeError) { | ||
| 1158 | - fileError = [NSError errorWithDomain:NetworkRequestErrorDomain code:ASIFileManagementError userInfo:[NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"Failed to delete file at %@ with error: %@",temporaryFileDownloadPath,removeError],NSLocalizedDescriptionKey,removeError,NSUnderlyingErrorKey,nil]]; | ||
| 1159 | - } | ||
| 1160 | } else { | 1165 | } else { |
| 1161 | 1166 | ||
| 1162 | //Remove any file at the destination path | 1167 | //Remove any file at the destination path |
| @@ -192,7 +192,6 @@ | @@ -192,7 +192,6 @@ | ||
| 192 | [requestThatShouldFail release]; | 192 | [requestThatShouldFail release]; |
| 193 | } | 193 | } |
| 194 | 194 | ||
| 195 | - | ||
| 196 | 195 | ||
| 197 | - (void)requestFailedCancellingOthers:(ASIHTTPRequest *)request | 196 | - (void)requestFailedCancellingOthers:(ASIHTTPRequest *)request |
| 198 | { | 197 | { |
| @@ -372,9 +371,42 @@ | @@ -372,9 +371,42 @@ | ||
| 372 | success = (progress == 1.0); | 371 | success = (progress == 1.0); |
| 373 | GHAssertTrue(success,@"Failed to increment progress properly"); | 372 | GHAssertTrue(success,@"Failed to increment progress properly"); |
| 374 | 373 | ||
| 375 | - [networkQueue release]; | 374 | + [networkQueue release]; |
| 375 | + | ||
| 376 | + | ||
| 377 | + //Test the temporary file cleanup | ||
| 378 | + complete = NO; | ||
| 379 | + progress = 0; | ||
| 380 | + networkQueue = [[ASINetworkQueue alloc] init]; | ||
| 381 | + [networkQueue setDownloadProgressDelegate:self]; | ||
| 382 | + [networkQueue setShowAccurateProgress:YES]; | ||
| 383 | + [networkQueue setDelegate:self]; | ||
| 384 | + [networkQueue setQueueDidFinishSelector:@selector(queueFinished:)]; | ||
| 385 | + | ||
| 386 | + request = [[[ASIHTTPRequest alloc] initWithURL:downloadURL] autorelease]; | ||
| 387 | + [request setDownloadDestinationPath:downloadPath]; | ||
| 388 | + [request setTemporaryFileDownloadPath:temporaryPath]; | ||
| 389 | + [request setAllowResumeForFileDownloads:YES]; | ||
| 390 | + [networkQueue addOperation:request]; | ||
| 391 | + [networkQueue go]; | ||
| 392 | + | ||
| 393 | + // Let the download run for 5 seconds | ||
| 394 | + timeoutTimer = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(stopQueue:) userInfo:nil repeats:NO]; | ||
| 395 | + while (!complete) { | ||
| 396 | + [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:1]]; | ||
| 397 | + } | ||
| 398 | + [networkQueue cancelAllOperations]; | ||
| 399 | + | ||
| 400 | + success = ([[NSFileManager defaultManager] fileExistsAtPath:temporaryPath]); | ||
| 401 | + GHAssertTrue(success,@"Temporary download file doesn't exist"); | ||
| 402 | + | ||
| 403 | + [request removeTemporaryDownloadFile]; | ||
| 404 | + | ||
| 405 | + success = (![[NSFileManager defaultManager] fileExistsAtPath:temporaryPath]); | ||
| 406 | + GHAssertTrue(success,@"Temporary download file should have been deleted"); | ||
| 376 | 407 | ||
| 377 | timeoutTimer = nil; | 408 | timeoutTimer = nil; |
| 409 | + [networkQueue release]; | ||
| 378 | 410 | ||
| 379 | } | 411 | } |
| 380 | 412 |
-
Please register or login to post a comment