Skip to content

Commit

Permalink
File download changes (#344)
Browse files Browse the repository at this point in the history
* Return after completion if error

* Ensure dispatch_group_leave is called once for enter

* Reuse filepath

* Remove unused var
  • Loading branch information
nzagorchev authored Jun 26, 2024
1 parent 6a94ad3 commit 2b147be
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
13 changes: 8 additions & 5 deletions CleverTapSDK/FileDownload/CTFileDownloadManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,10 @@ - (void)downloadFiles:(nonnull NSArray<NSURL *> *)urls
_downloadInProgressHandlers[url] = [NSMutableArray array];
}
[_downloadInProgressHandlers[url] addObject:^(NSURL *completedURL, BOOL success) {
[filesDownloadStatus setObject:[NSNumber numberWithBool:success] forKey:[completedURL absoluteString]];
dispatch_group_leave(group);
@synchronized (self) {
[filesDownloadStatus setObject:[NSNumber numberWithBool:success] forKey:[completedURL absoluteString]];
dispatch_group_leave(group);
}
}];
continue;
}
Expand Down Expand Up @@ -104,7 +106,7 @@ - (void)downloadFiles:(nonnull NSArray<NSURL *> *)urls
}

- (BOOL)isFileAlreadyPresent:(NSURL *)url {
NSString* filePath = [self.documentsDirectory stringByAppendingPathComponent:[self hashedFileNameForURL:url]];
NSString* filePath = [self filePath:url];
BOOL fileExists = [self.fileManager fileExistsAtPath:filePath];
return fileExists;
}
Expand Down Expand Up @@ -158,6 +160,7 @@ - (void)removeAllFilesWithCompletionBlock:(CTFilesRemoveCompletedBlock)completio
if (error) {
CleverTapLogInternal(self.config.logLevel, @"%@ Failed to get contents of directory %@ - %@", self, path, error);
completion(filesDeleteStatus);
return;
}
for (NSURL *file in files) {
dispatch_group_enter(deleteGroup);
Expand Down Expand Up @@ -210,7 +213,7 @@ - (void)downloadSingleFile:(NSURL *)url
}
}

NSString *destinationPath = [self.documentsDirectory stringByAppendingPathComponent:[self hashedFileNameForURL:url]];
NSString *destinationPath = [self filePath:url];
NSURL *destinationURL = [NSURL fileURLWithPath:destinationPath];
NSError *fileError;
// Check if the file exists at the destination
Expand Down Expand Up @@ -246,7 +249,7 @@ - (void)deleteSingleFile:(NSURL *)url
return;
}

NSString *filePath = [self.documentsDirectory stringByAppendingPathComponent:[self hashedFileNameForURL:url]];
NSString *filePath = [self filePath:url];
NSError *error;
BOOL success = [self.fileManager removeItemAtPath:filePath error:&error];
if (!success) {
Expand Down
3 changes: 1 addition & 2 deletions CleverTapSDK/FileDownload/CTFileDownloader.m
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,13 @@ - (void)clearFileAssets:(BOOL)expiredOnly {
}

- (nullable NSString *)fileDownloadPath:(NSString *)url {
NSString *filePath = nil;
if ([self isFileAlreadyPresent:url]) {
NSURL *fileURL = [NSURL URLWithString:url];
return [self.fileDownloadManager filePath:fileURL];
} else {
CleverTapLogInternal(self.config.logLevel, @"%@ File %@ is not present.", self, url);
}
return filePath;
return nil;
}

- (nullable UIImage *)loadImageFromDisk:(NSString *)imageURL {
Expand Down

0 comments on commit 2b147be

Please sign in to comment.