Skip to content

Commit

Permalink
- added two helper methods for directory managing
Browse files Browse the repository at this point in the history
  • Loading branch information
AYastrebov committed Apr 29, 2015
1 parent 267b9aa commit 1419558
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
2 changes: 2 additions & 0 deletions TWRDownloadManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@
- (void)cancelAllDownloads;
- (void)cancelDownloadForUrl:(NSString *)fileIdentifier;

- (void)cleanDirectoryNamed:(NSString *)directory;

- (BOOL)isFileDownloadingForUrl:(NSString *)url withProgressBlock:(void(^)(CGFloat progress))block;
- (BOOL)isFileDownloadingForUrl:(NSString *)url withProgressBlock:(void(^)(CGFloat progress))block completionBlock:(void(^)(BOOL completed))completionBlock;

Expand Down
25 changes: 23 additions & 2 deletions TWRDownloadManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTas
TWRDownloadObject *download = [self.downloads objectForKey:fileIdentifier];

if (download.directoryName) {
[self createDirectoryNamed:download.directoryName];
destinationLocation = [[[self cachesDirectoryUrlPath] URLByAppendingPathComponent:download.directoryName] URLByAppendingPathComponent:download.fileName];
} else {
destinationLocation = [[self cachesDirectoryUrlPath] URLByAppendingPathComponent:download.fileName];
Expand Down Expand Up @@ -258,6 +259,18 @@ - (CGFloat)remainingTimeForDownload:(TWRDownloadObject *)download

#pragma mark - File Management

- (BOOL)createDirectoryNamed:(NSString *)directory {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachesDirectory = [paths objectAtIndex:0];
NSString *targetDirectory = [cachesDirectory stringByAppendingPathComponent:directory];

NSError *error;
return [[NSFileManager defaultManager] createDirectoryAtPath:targetDirectory
withIntermediateDirectories:YES
attributes:nil
error:&error];
}

- (NSURL *)cachesDirectoryUrlPath {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachesDirectory = [paths objectAtIndex:0];
Expand Down Expand Up @@ -375,7 +388,15 @@ - (BOOL)deleteFileWithName:(NSString *)fileName
return deleted;
}

#pragma mark - Clean tmp directory
#pragma mark - Clean directory

- (void)cleanDirectoryNamed:(NSString *)directory {
NSFileManager *fm = [NSFileManager defaultManager];
NSError *error = nil;
for (NSString *file in [fm contentsOfDirectoryAtPath:directory error:&error]) {
[fm removeItemAtPath:[directory stringByAppendingPathComponent:file] error:&error];
}
}

- (void)cleanTmpDirectory {
NSArray* tmpDirectory = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:NSTemporaryDirectory() error:NULL];
Expand All @@ -386,7 +407,7 @@ - (void)cleanTmpDirectory {

#pragma mark - Background download

-(void)URLSessionDidFinishEventsForBackgroundURLSession:(NSURLSession *)session {
- (void)URLSessionDidFinishEventsForBackgroundURLSession:(NSURLSession *)session {
// Check if all download tasks have been finished.
[session getTasksWithCompletionHandler:^(NSArray *dataTasks, NSArray *uploadTasks, NSArray *downloadTasks) {
if ([downloadTasks count] == 0) {
Expand Down

0 comments on commit 1419558

Please sign in to comment.